My birthday was Yesterday, can someone look at my code. Trouble with updating JTEXT
Code Java:
class Gamehandler extends JFrame implements ActionListener {
//questions
JPanel qpanel = new JPanel();
//answers
JPanel aPanel = new JPanel();
JRadioButton[] responses;
ButtonGroup group = new ButtonGroup();
//bottom
JPanel botPanel = new JPanel();
JButton next = new JButton("next");
JButton finish = new JButton("Finish");
static String guess = " ";
readExcel re = new readExcel();
JLabel label = new JLabel();
JFrame frame = new JFrame("Test");
public void actionPerformed(ActionEvent event) {
re.readExcel2();
Random rm = new Random();
int radiobuttonsize = readExcel.ColumnTwo2.size();
System.out.print("this is the size ofthe radiobuttonsize"+radiobuttonsize);
radioButtons rbs = new radioButtons();
scoreTest sr = new scoreTest();
responses = new JRadioButton[readExcel.ColumnTwo2.size()];
//adds my radio buttons
//adds action listeneners
//adds get actions
for (int i = 0; i < readExcel.ColumnTwo2.size(); i++) {
responses[i] = new JRadioButton(readExcel.ColumnTwo2.get(i));
group.add(responses[i]);
aPanel.add(responses[i]);
responses[i].addActionListener(rbs);
responses[i].setActionCommand(String.valueOf(i));
Object selection = responses[0].getAction();
}
int randomguessArray = rm.nextInt(radiobuttonsize);
guess = readExcel.ColumnOne1.get(randomguessArray);
label = new JLabel();
label.setText("What does this mean " + " " + guess);
qpanel.add(label);
frame = new JFrame("Test");
frame.setSize(400, 300);
frame.setResizable(true);
frame.setVisible(true);
frame.add(aPanel);
botPanel.add(next);
botPanel.add(finish);
frame.getContentPane().add(qpanel, BorderLayout.NORTH);
frame.getContentPane().add(aPanel, BorderLayout.CENTER);
frame.getContentPane().add(botPanel, BorderLayout.SOUTH);
next.addActionListener(sr);
String event1 = event.getActionCommand();
System.out.println("This is the event1"+event1);
}
public void changeText() {
label.setText("Work");
label.revalidate();
}
}
//next button changes the label
class scoreTest extends JFrame implements ActionListener {
public void actionPerformed(ActionEvent event) {
Gamehandler gr = new Gamehandler();
gr.changeText();
}
}
I hope I didn't add to much of my code. So I added an actionlistener to the JButton and when it's pressed I want it to update the JLabel in the JFrame. I am unsure why this is not working correctly. I looked through a lot of notes and could not find anything that would help me. There is no errors, it just doesn't update. I am new to programming and thought I this part would be easy.
I have tried to update the panel with the new label and still nothing. I hope you can point me in the right direction.
Re: My birthday was Yesterday, can someone look at my code. Trouble with updating JTEXT
It should be easy, but you made it difficult.
It's okay to have another class like scoreTest (should be named ScoreTest) be the ActionListener, that's even a good thing, but there's no reason for it to extend JFrame. But the real problem is that scoreTest creates a new instance of Gamehandler and calls that instance's changeText() method. The new instance may never be visible (it's not clear when Gamehandler is setVisible()), so you don't see the change in the new instance, but it's happening somewhere in memory.
You might rewrite ScoreTest as:
Code java:
//next button changes the label
class ScoreTest implements ActionListener
{
Gamehandler gh;
// default constructor
public ScoreTest( Gamehandler gh )
{
this.gh = gh;
}
public void actionPerformed(ActionEvent event)
{
gh.changeText();
}
}
Then when you create the action listener instance, pass the current instance of Gamehandler to its constructor:
ScoreTest sr = new ScoreTest( this );
I couldn't test this approach because of the missing parts of your code, but the concept is sound. Minor adjustments may be necessary.
Re: My birthday was Yesterday, can someone look at my code. Trouble with updating JTEXT
revalidate() is used to update changes made to the layout.
After changing the text you should only need to call repaint()
Re: My birthday was Yesterday, can someone look at my code. Trouble with updating JTEXT
Hello.
Instead of revalidate(), can you invoke validate() on the layout object and let me know if it worked.
Syed.
Re: My birthday was Yesterday, can someone look at my code. Trouble with updating JTEXT
HI,
Thank you the fix worked, but I have to make sure I understand exactly from this situation,so I can grow from this situation and correct my thinking pattern. So in this analogy, if Objects represent remote controls my controller was pointed at an entirely different TV,so what you did to fix this problem, was to keep my remote pointed at the same TV so it speak. You achieved this my passing the instance of the class through the constructor.
I would have never came to that conclusion on my own. Hopefully, my explanation is correct on this, because it seems to make sense in my head at 2:30am in the morning. Keep in mind, I have to wake up early tomorrow to go hang with the family early in the AM. Nothing in the world is more important than overcoming a programming problem. Thank you my kind sir.
Re: My birthday was Yesterday, can someone look at my code. Trouble with updating JTEXT
I'm not a huge fan of the TV analogy, but as one who constantly has one on, I can see the attraction, so I'll do my best:
The object is the TV, the action listener the remote control. You had 2 TVs (two Gamehandler objects), one visible and one not - maybe in another room entirely - and the action listener (remote control) was set for or pointed at the wrong TV.
Have a great family day!
Edit: Oh! And a belated Happy Birthday! (belated is a weird word)