I am trying to build a quiz game that rerender itself after user click on the button with answer.
I have added an action listener to 4 buttons. When the button is clicked, it is suppose to reach the outer class which extend JFrame and remove the QuestionPanel that extends the JPanel. And then create a new QuestionPanel and add it back to the frame.
The hierarchy in goes like this :
MainFrame (JFrame) -> QuestionPanel (JPanel) -> optionPanel (JPanel) -> button (JButton)
MainFrame(outer class)
-> QuestionPanel (inner class)
-> OptionPanel (inner class)
But it just freeze during execution
button.addActionListener(e->{
boolean result = false;
JButton target = (JButton) e.getSource();
result = MainFrame.this.questions[currentQuestion].checkAnswer(target.getText());
System.out.println(questions.length);
if(currentQuestion != (questions.length - 1)){
MainFrame.this.remove(qPanel);
//qPanel is the instance of QuestionPanel
currentQuestion++;
qPanel = new QuestionPanel(questions[currentQuestion]);
MainFrame.this.add(qPanel);
}
});
Like many pointed out, you would be better off changing the text inside a component like a Label rather than removing a Jpanel and replacing it. There is simply no need for that, especially just to display text to a user.
Related
I quite new in java and I encounter a problem with repaint a TextFile in my JPanel, this is code:
JPanel paneldol = new JPanel();
paneldol.add(new JButton(new AbstractAction("Oblicz pole i obwód")
{
#Override
public void actionPerformed(ActionEvent e) {
paneldol.repaint();
paneldol.revalidate();
}
}
));
paneldol.add(new TextField(model.getPole(), 10));
paneldol.add(new TextField(model.getObwod(), 10));
this.add(paneldol, BorderLayout.PAGE_END);
As you see TextField have metod that generate string, so when i click in button I want to repain panel to have new value in my Textfield, Is this possibly?
so when i click in button I want to repain panel to have new value in my Textfield, Is this possibly?"`
If all you want to do is to change the text in text field on button push, then you should give your class a JTextField variable (or multiple JTextField variables), assign a JTextField object to the variable, and this to the GUI. Then when within your button's listener, simply set the text of the JTextField via its setText(...) method. There's no need to call repaint() or revalidate() as they will do nothing useful in this situation.
Also don't mix AWT with Swing components. Use JTextFields not TextFields.
I have a swing java application on which I have a 114 button
I have created the buttons by a loop
Container pane = getContentPane();
JPanel panel = new JPanel();
JButton b;
for(int i=1;i<115;i++)
{
b = new JButton(""+i);
panel.add(b);
}
So all buttons will take the name b !!!
that is the problem here
I want to give each button a different name to execute different action for each button.
by ActionListener class
JButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
No, your buttons don't have the name b. You are using a local variable b inside the for loop. You can for instance create an array of buttons and store your JButton instances there. Later, you can loop through that array to change the buttons text.
Perhaps you could add your buttons to a Map using what you want to call the button as the key. Then you can access the button by calling the get() on the Map with whatever you want to call the button.
I've got a JPanel, which I want to respond to a mouse click and then open a JDialog. The JDialog constructor needs an instance of JFrame and not JPanel - how do I work around this?
You should really try to attach the JDialog to a parent Dialog or Frame, especially if you want it modal (by passing a parent Window, the dialog will be attached to your Window and bringing the parent will bring the child dialog as well). Otherwise, the user experience can really go wrong: lost dialogs, blocking windows without seeing the modal dialog, etc...
To find your JPanel parent Window, all you need is this code:
JPanel panel = new JPanel();
Window parentWindow = SwingUtilities.windowForComponent(panel);
// or pass 'this' if you are inside the panel
Frame parentFrame = null;
if (parentWindow instanceof Frame) {
parentFrame = (Frame)parentWindow;
}
JDialog dialog = new JDialog(parentFrame);
...
If you don't know if you are in a Frame or Dialog, make the "instanceof" test for both classes.
Using the parameter free constructor will make the dialog owner-less. I think that the best thing to do would be to make the Frame that owns your Panel the owner of the dialog.
By that, I mean that you should use the getParent() from your JPanel to find its owner and then send this object found as the owner of your JFrame.
A crude code for that would be
java.awt.Container c = myPanel.getParent();
while (!(c instanceof javax.swing.JFrame) && (c!=null)) {
c = c.getParent();
}
if (c!=null) {
JFrame owner=(javax.swing.JFrame) c;
JDialog myDialog=new JDialog(owner);
}
I have not tested this code, but it is good enought for you to understand the idea.
If you decided to go with a JOptionPane, you could add a MouseListener to the JPanel with a mouseAdapter inner class to handle mouseClicked events. You would have to declare the panel final in order to access the panel from within the inner class.
final JPanel testPanel = new JPanel();
testPanel.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
JOptionPane.showMessageDialog(testPanel,"Title","InformationMessage",JOptionPane.INFORMATION_MESSAGE);
}});//end of mouseClicked method
There's a constructor that don't need argument:
JDialog dialog = new JDialog();
If what you want is to make the dialog modal, maybe you can get a static reference of you main JFrame, something like:
JDialog dialog = new JDialog(MyMainJFrame.getInstance());
how can i add components dynamically in a jpanel?
I am having add button when i click the button the components should be added to the JPanel.
my question is that adding a textfield and button to jpanel when i click on the add button the user can click on the add button any number of times according to that i have to add them to the jpanel. i have added to scrollerpane to my jpanel,and jpanel layout manager is set to null.
Just as you always do, except that you have to call:
panel.revalidate();
when you are done, since the container is already realized.
Use an ActionListener, you can use an anonymous class like this:
JPanel myJPanel = new JPanel();
...
b = new Button("Add Component");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JLabel someLabel = new JLabel("Some new Label");
myJPanel.add(someLabel);
myJPanel.revalidate();
}
});
I'm trying to close a frame yet open a new frame.
My application has page A, a JPanel with some controls and a specific button, and when the user clicks the button, I want page A to disappear and page B to appear (page B has controls that depend on the choices that are made by the user on page A).
This has been asked before, but there was no satisfactory answer. Inside the ActionListener implementation, namely public void ActionPerformed(ActionEvent e) from my jpanelForPageA class, I can comfortably write this.setVisible(false), but how can I set page B to a visible state?
You can do the removal of panel a and then the addition of panel b trick. Another is to use a CardLayout.
When you create your panels, you add them to a containing JPanel that you initialize with a CardLayout:
JPanel container = new JPanel(new CardLayout());
containter.add(getPanelA(), "PANEL_A");
containter.add(getPanelB(), "PANEL_B");
Then, in your actionPerformed, when you want to show panelB, you do this:
CardLayout cl = (CardLayout) container.getLayout();
cl.show("PANEL_B");
Take a look at this tutorial for some more ideas.
For some reason, I can never to get setVisible() to work for me to do what you're describing. Instead, I do this:
frame.remove(panelA);
frame.add(panelB);
"frame" is just the JFrame you want to put the panels in. Try this if the setVisible() method doesn't work :)
To your original question, all you have to do is (like aioobe said):
panelB.setVisible(true);
((btw, posting some of your code would help me figure out what you're trying to ask))
And this is just a guess as to what you're trying to do -- I'm guessing your JPanels are in different classes. Then, you'll need to do this:
class pages extends JFrame implements ActionListener
{
public pages()
{
panelA a = new panelA(this)
}
changeToA(panelB b)
{
remove(panelB);
add(new panelA(this));
}
changeToB(panelA a)
{
remove(panelA);
add(new panelB(this));
}
}
class panelA extends JPanel implements ActionListener
{
pages p;
public panelA(pages p)
{
this.p = p
}
// all that actionlistener code stuff
p.changeToB(this);
}
class panelB extends JPanel implements ActionListener
{
pages p;
public panelB(pages p)
{
this.p = p
}
// all that actionlistener code stuff
p.changeToA(this);
}
You pass the pages class to the panels so the panels can tell the pages class to remove themselves.
((I don't know if there is an easier way, but this is what I do all the time))
I hope I helped :)
You have to remove Panel A from the frame, add Panel B to the frame, and call invalidate on the frame (or containing panel). At least in Swing, I'm not sure about AWT, there you might need repaint or revalidate instead of invalidate.
You could also just create a whole new JFrame and dispose the one containing panel A.