Reputation: 1707
I have a button which runs a function in another class.The button click event handling goes like this:
JPanel ne=new JPanel();
JButton startButton=new JButton("START");
public void actionPerformed(ActionEvent e)
{
System.out.println("Welcome to Guess the number Game");
System.out.println("You have 3 chances to guess a number between 0 and 10 excluding 10");
ne.remove(startButton);
gamer2 game=new gamer2();
game.generatenum();
}
The JButton startButton
is added to a JPanel ne
.Once the button is clicked,actionPerformed(ActionEvent e)
runs and the button is supposed to be removed from the JPanel.But the button remains there until the whole program finishes running.Can someone help me with this?
Complete code for the JFrame:
package test;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;
public class test3 implements ActionListener {
JButton p=new JButton("START");
JPanel ne=new JPanel();
public void create()
{
Dimension s=new Dimension(400,400);
JFrame l=new JFrame();
l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l.setSize(s);
l.setResizable(true);
Dimension s1=new Dimension(400,200);
Dimension s2=new Dimension(400,100);
JPanel me=new JPanel();
JLabel kingsman=new JLabel ("GUESS THE KING'S NUMBER!");
kingsman.setFont(new Font("Serif", Font.BOLD, 45));
JPanel commonPane=new JPanel();
BoxLayout n1=new BoxLayout(commonPane,1);
commonPane.setLayout(n1);
p.setFont(new Font("Serif", Font.BOLD, 40));
p.setPreferredSize(s1);
JTextField tf=new JTextField();
tf.setFont(new Font("Serif", Font.BOLD, 45));
Border border = BorderFactory.createLineBorder(Color.YELLOW, 5);
tf.setBorder(border);
tf.setPreferredSize(s2);
JPanel cn=new JPanel();
cn.add(tf);
//l.add(p);
me.add(kingsman);
ne.add(p);
commonPane.add(me);
commonPane.add(ne);
commonPane.add(cn);
l.add(commonPane);
l.setVisible(true);
p.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
System.out.println("Welcome to Guess the number Game");
System.out.println("You have 3 chances to guess a number between 0 and 10 excluding 10");
ne.remove(p);
ne.updateUI();
gamer2 game=new gamer2();
game.generatenum();
}
public static void main(String args[])
{
test3 ob=new test3();
ob.create();
}
}
Upvotes: 1
Views: 52
Reputation: 21640
All the updates on the GUI (initiated by ne.remove(p);
and ne.updateUI();
) will be done after the actionPerformed(..)
method finishes. Therefore game.generatenum();
should only bring up the new GUI and return as fast as possible.
Upvotes: 2
Reputation: 347334
Under normal circumstances, something like...
ne.remove(startButton);
ne.revalidate();
ne.repaint();
should work. If this doesn't work, that would suggest that you're doing something else wrong in your code which you're not showing us and you should consider providing a runnable example which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses
So having look at the source code, I updated the actionPeformed
method to look like...
public void actionPerformed(ActionEvent e) {
System.out.println("Welcome to Guess the number Game");
System.out.println("You have 3 chances to guess a number between 0 and 10 excluding 10");
ne.remove(p);
ne.revalidate();
ne.repaint();
//ne.updateUI();
//gamer2 game = new gamer2();
//game.generatenum();
}
And that worked fine for me.
I would, however, encourage you to have a look at How to Use CardLayout for a better alternative
Upvotes: 2
Reputation: 4755
After removing the button, revalidate the UI, then repaint it.
ne.revalidate();
ne.repaint();
This should invalidate the UI and request that it be redrawn.
Upvotes: 1
Reputation: 6089
You can call the UI update method,
en.updateUI();
This will update all the UI inside this JPanel
.
Upvotes: 0