• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Text box to Label & Centering Text

 
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm making a score keeping program, and above each score, I have a text box for the team name. What I want is for the text box to change into a label when a user hits enter when editing the text or clicks away from the text box. Likewise, when a user clicks back onto the label, I want the label to turn into a text box.

Next, I would like to know how to center the text of a text box and label.

My program is

 
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain about changing from a text box to a label when text is entered. Is that desirable? You may be able to inactivate the text box; look for a setEnabled method in its documentation.

I think this discussion would sit better on our GUIs forum, so I shall move it.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please explain about changing from a text box to a label when text is entered. Is that desirable?


That's the desired result, yes. Currently, it's just a text box.
 
Campbell Ritchie
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still seems odd to me, but . . .

Not sure about this; you will have to go through the Java™ tutorials about layouts I think.
Try putting your label and text box in the same location, which you can do with some layouts (eg GridBag) and not with others (eg Grid). See if you can set the text box visible, and set the label not visible. I think you can add an ActionListener to a text component, which would be fired by the enter key.
When that happens, set the text box invisible, and disable it. Make the label visible.

I think that way you get a one-way system. You enter a score, push enter, and can then never correct the score if you find you made a mistake.

Actually, I think you can only bring top-level components to the front or back, so for text boxes you will have to stick to visible and not visible.

Anybody else got a better suggestion? There is bound to be a better way to do this.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You enter a score, push enter, and can then never correct the score if you find you made a mistake.



Thanks for the help. Sorry for not making myself clear; I meant that the text boxes for the team names I wanted to change. The score is changed by buttons like these: "+10", "-5". The scores are then blanked by a reset button.

The reason that I want to have the team names like that is because this program is going to be projected onto a large screen during a quizzing meet. There are multiple quizzes in a day, and back-to-back, so it would be nice to be able to change the names into labels (looks better) and then click on the label to have it change back into a text box to change the names before the next quiz. Otherwise, the program would have to be exited and restarted.

And do you know how to center text?
 
Sheriff
Posts: 28328
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way I would do that is this:

(1) Put both the label and the text-box into your layout, next to each other.

(2) Initialize the label to invisible and the text-box to visible.

(3) When switching modes, copy the data from the text-box to the label, set the text-box to invisible, and set the label to visible.

If you chose a reasonable layout manager then it will rearrange the display so it appears that the text-box is replaced by the label, although for some layout managers you might have to tinker with the preferred widths of the components a bit.

Also, text in a JLabel is automatically centred, isn't it?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

(3) When switching modes, copy the data from the text-box to the label, set the text-box to invisible, and set the label to visible.


Or place both the label and the text field in a JPanel with CardLayout, and swap the cards.
 
A timing clock, fuse wire, high explosives and a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic