My JFrame won't repaint correctly please help me out!
Hi I've been working on my final project for CSC 190 for many hours now. I have hit a problem that I cannot solve, and my teacher couldn't help me solve. I would like to find a fix for this problem but I guess I'm having trouble figuring out what isn't working. You see I'm doing a who wants to be a millionaire program in java for my final project && I have set up the buttons to accurately check wether the answer submitted was wright or wrong, but what I cannot get the program to do is the following:
Remove the JPanel which houses the 4 answer buttons from the JFrame && repaint the next questions buttons.
Included below is the main source code. The code will compile for me but when I press one of the buttons the frame repaints without any buttons. I am willing to change the logic behind my coding if someone could present to me a more valid way to set up this program.. this was just the best way I could think to do it with my limited knowledge of java.
Just incase some of you are lazy and don't want to download the whole program I will include the main body so that you can examine that. Please let me know what you think && any tips or suggestions for my coding. I know that it is a little sloppy and needs to be divded into more classes but I wanted to get it functioning before I did that.
Here is my full source code Hotfile.com: One click file hosting: finalproject.zip
Thanks again for any help!
Code java:
public class MillionaireFrame extends JFrame
{
private static final int FRAME_WIDTH = 500;
private static final int FRAME_HEIGHT = 400;
private JLabel sampleField;
private JPanel buttonPanel;
private JPanel mainPanel;
private JPanel hotlinePanel;
private JPanel answerPanel;
private ActionListener buttonUpdater1;
private ActionListener buttonUpdater2;
private ActionListener buttonUpdater3;
private JButton button5;
private JButton button6;
private JButton button7;
private Question [] quiz;
private int counter;
/**
Constructs the frame.
*/
public MillionaireFrame()
{
//question counter
counter = 0;
//instantiate questionbuilder object && construct question array
QuestionDemo builder = new QuestionDemo();
quiz = builder.QuestionsBuilder();
// Construct question text panel & object && set params
mainPanel = new JPanel();
sampleField = new JLabel(quiz[counter].display());
mainPanel.add(sampleField);
mainPanel.setBorder(new TitledBorder(new EtchedBorder(), "Question #" + (getCounter() + 1)));
//add the text panel to our millionaireframe
add(mainPanel, BorderLayout.CENTER);
//call createcontrolpanel method
addPanels();
//set our millionaireframe class dimensions
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
/**
calls a method which calls 2 more method which in turn
make our answer panel && hotline panel. These panels are then added to our main frame.
*/
public void addPanels()
{
answerPanel = makeAnswers();
hotlinePanel = createHotlines();
// Add panels to our main frame
add(answerPanel, BorderLayout.SOUTH);
add(hotlinePanel, BorderLayout.EAST);
}
/**
Creates the answer grid with the answer choices.
@return the panel containing the answers box
*/
public JPanel makeAnswers()
{
JButton button1 = buttonMaker(quiz[counter].getChoice(0));
JButton button2 = buttonMaker(quiz[counter].getChoice(1));
JButton button3 = buttonMaker(quiz[counter].getChoice(2));
JButton button4 = buttonMaker(quiz[counter].getChoice(3));
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(2, 2));
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
buttonPanel.add(button4);
buttonPanel.setBorder(new TitledBorder(new EtchedBorder(), "Answers"));
return buttonPanel;
}
public JPanel createHotlines()
{
button5 = new JButton("hotline1");
button5.setBackground(Color.WHITE);
button6 = new JButton("hotline2");
button6.setBackground(Color.WHITE);
button7 = new JButton("hotline3");
button7.setBackground(Color.WHITE);
button5.setPreferredSize(new Dimension(110, 45));
button6.setPreferredSize(new Dimension(110, 45));
button7.setPreferredSize(new Dimension(110, 45));
hotlinePanel = new JPanel();
hotlinePanel.setLayout(new GridLayout(3, 1));
hotlinePanel.add(button5);
hotlinePanel.add(button6);
hotlinePanel.add(button7);
buttonUpdater1 = new buttonUpdate1();
buttonUpdater2 = new buttonUpdate2();
buttonUpdater3 = new buttonUpdate3();
button5.addActionListener(buttonUpdater1);
button6.addActionListener(buttonUpdater2);
button7.addActionListener(buttonUpdater3);
hotlinePanel.setBorder(new TitledBorder(new EtchedBorder(), "HotLines"));
return hotlinePanel;
}
public void incrementCounter()
{
counter++;
System.out.println(counter);
}
public void frameRepaint()
{
answerPanel.repaint();
revalidate();
}
public int getCounter()
{
return counter;
}
public void createSampleField()
{
remove(mainPanel);
mainPanel.remove(sampleField);
JLabel tempLabel = new JLabel(quiz[getCounter()].display());
mainPanel.setBorder(new TitledBorder(new EtchedBorder(), "Question #" + (getCounter() + 1)));
mainPanel.add(tempLabel);
add(mainPanel, BorderLayout.CENTER);
}
/* These are our button listeners listed below. The first 4 are for our answer buttons.
The last 3 are for our hotline buttons*/
public JButton buttonMaker(final String passedName)
{
String thisName = passedName;
JButton button = new JButton(thisName);
class ChoiceListener1 implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String s = passedName;
System.out.println(s);
if (quiz[getCounter()].checkAnswer(s)==true)
{
incrementCounter();
createSampleField();
remove(answerPanel);
frameRepaint();
answerPanel = makeAnswers();
System.out.println("before add");
add(answerPanel, BorderLayout.SOUTH);
frameRepaint();
}
else
{
sampleField.setText("Sorry game over!");
}
}
}
ActionListener answerListener = new ChoiceListener1();
button.addActionListener(answerListener);
button.setPreferredSize(new Dimension(250, 75));
return button;
}
class buttonUpdate1 implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
hotlinePanel.remove(button5);
repaint();
}
}
class buttonUpdate2 implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
hotlinePanel.remove(button6);
repaint();
}
}
class buttonUpdate3 implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
hotlinePanel.remove(button7);
repaint();
}
}
}
Re: My JFrame won't repaint correctly please help me out!
You've misused the code tags, I've edited your post to use them but see my signature for how to use them properly.
I'm not sure what your question truly is...it helps to break things down (for both you and us) into an SSCCE - remove all the extra details to concentrate on what exactly your problem is.
Re: My JFrame won't repaint correctly please help me out!
Thanks. My problem I feel may be from a lack of fully understanding the GUI interface. But essentially what I am hung up on is the following.
MY buttons are dynamically generated from an array, and in the action listener for all of my answer buttons I have it set to increment a counter.
This counter is used to reference which question for my answer buttons to point to in my array.
The piece of the puzzle I cant seem to combine is the refreshing or updating or repainting aspect after the completion of the first question.
I've tried removing the answerbutton panel && recalling it, then calling a repaint on the whole frame. However when I implement this the frame comes back without any new buttons. However I know that the incrementing is working correctly because the border of my button panel will update as the program increments.
I guess my question is vague because I'm not entirely sure how to set up my program to do what im describing here.
Here is the listener that is attached to all my answers in my button panel.
This thread has been cross posted here:
public JButton buttonMaker(final String passedName)
{
String thisName = passedName;
JButton button = new JButton(thisName);
class ChoiceListener1 implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String s = passedName;
System.out.println(s);
if (quiz[getCounter()].checkAnswer(s)==true)
{
incrementCounter();
createSampleField();
remove(answerPanel);
frameRepaint();
answerPanel = makeAnswers();
System.out.println("before add");
add(answerPanel, BorderLayout.SOUTH);
frameRepaint();
}
else
{
sampleField.setText("Sorry game over!");
}
}
}
ActionListener answerListener = new ChoiceListener1();
button.addActionListener(answerListener);
button.setPreferredSize(new Dimension(250, 75));
return button;
}
Although cross posting is allowed,
for everyone's benefit, please read:
Java Programming Forums Cross Posting Rules
The Problems With Cross Posting