Jump to content

Question about swing

- - - - -

  • Please log in to reply
4 replies to this topic

#1
qutazs

qutazs

    Newbie

  • Members
  • PipPip
  • 23 posts
Hello,

i'm trying to make a yahtzee scoreboard, and right now, I just wanna know the basics of working with javax.swing..
I'm using the GUI editor in netbeans to ease the pain..

Lets say I got a ToggleButton and a JTextField.. I want the textfield to change between T or F (true/false) depending on the buttons state (pressed = T, not pressed = F).

But I'm having trouble even updating the textfield with some text (except for the initial text).. Any hints on where to read up on this?

---------- Post added at 09:05 PM ---------- Previous post was at 09:03 PM ----------

Scoreboard will later be used for actual yahtzee game, so it got 5xJTextField which represents each dice, a Roll Dice button + the hold buttons.

When Roll Dice is pressed, it should random a number to the textfield, but lets say field 3 is on "Hold", it should only update 1 2 4 5.

[1] [2] [3] [4] [5]
[H] [H] [HPRESSED] [H] [H]

Update [1] [2] [4] [5].

This is just the start, once I know how to do this, it should not really be an issue to do the rest (for now) :P

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas

qutazs said:

But I'm having trouble even updating the textfield with some text (except for the initial text).. Any hints on where to read up on this?

Since you haven't posted any code here, I can only offer limited help, but you can assign the text value of a text field with the following statement:

jTextField.setText("Some text"); // Assuming your JTextField is named "jTextField".


That should get you started.

Edit: Here's a good place to read up on this: How to Use Text Fields (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
qutazs

qutazs

    Newbie

  • Members
  • PipPip
  • 23 posts
I know I dont have any code, I usually do post code.. But since this is something very different that what I usually do (commandline based stuff) I dont really have anything :P

I can easily get a text into a textbox, since I use netbeans java GUI maker, its more how to add a text depending on the state of a ToggleButton :/

If ToggleButton is pressed down update text in textbox to T
if its not, update text in textbox to F

#4
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Setting the text in a textbox programmatically is done just as I have posted above. Doing it conditionally just depends on how you write your business logic. I would use an 'if' statement.

Do you know how to get the state of a JToggleButton? Hint: it's not in the JToggleButton class. The method you're looking for is in the ButtonModel class: ButtonModel (Java Platform SE 6)

You need to get the toggle button's model, then get its selected state.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#5
qutazs

qutazs

    Newbie

  • Members
  • PipPip
  • 23 posts
Thanks, ill look into it :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users