zoltix
asked on
my button do not appear but when I resize my window the button appears
I am beginner in java(SWING) but I have got a small problem with repaint() or refresh(). When I try to Add a new JButton during execution, my button do not appear but when I resize my window the button appears. can I execute this refresh manually (howto)? Thank in advance. bye
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I can't find revalidate() because i use component and Jframe. In Jframe I found validate and it s Ok. What is the diffenrence between validate, revalidate?
You should use JComponent and not Component.
For what object you use Component?
Mix of awt and swing objects is a bad idea...
For what object you use Component?
Mix of awt and swing objects is a bad idea...
s_lavie is correct, that you shouldn't mix Components and JComponents. However, when you say you are using a Component, is this the contentPane of the JFrame (java.awt.Component->java. awt.Contai ner)?
If so, you can create a new JPanel and set the contentPane to be the JPanel you created.
Have a look at the following code example. It shows a JFrame and adds a button which allows you to add another button which adds others ad infinitum...
Disclaimer: this is just an example it is not production worthy code :^)
------
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListe ner;
import java.awt.event.WindowAdapt er;
import java.awt.event.WindowEvent ;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AddButtonExample extends JFrame {
JPanel mainPanel;
JButton addButton;
public AddButtonExample(String title) {
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
mainPanel = new JPanel();
setContentPane(mainPanel);
addButton = new JButton("Add JButton");
addButton.addActionListene r(new ActionListener() {
public void actionPerformed(ActionEven t e) {
JButton anotherButton = new JButton("Add another");
mainPanel.add(anotherButto n);
anotherButton.addActionLis tener(this );
// mainPanel.repaint();
mainPanel.revalidate();
}
});
mainPanel.add(addButton);
}
public static void main(String[] args) {
AddButtonExample example = new AddButtonExample("Add Button Example");
example.setSize(400, 200);
example.setVisible(true);
}
}
Note that repaint doesn't cause the new JButton to be shown - revalidate does
--
flumpman
If so, you can create a new JPanel and set the contentPane to be the JPanel you created.
Have a look at the following code example. It shows a JFrame and adds a button which allows you to add another button which adds others ad infinitum...
Disclaimer: this is just an example it is not production worthy code :^)
------
import java.awt.event.ActionEvent
import java.awt.event.ActionListe
import java.awt.event.WindowAdapt
import java.awt.event.WindowEvent
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AddButtonExample extends JFrame {
JPanel mainPanel;
JButton addButton;
public AddButtonExample(String title) {
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
mainPanel = new JPanel();
setContentPane(mainPanel);
addButton = new JButton("Add JButton");
addButton.addActionListene
public void actionPerformed(ActionEven
JButton anotherButton = new JButton("Add another");
mainPanel.add(anotherButto
anotherButton.addActionLis
// mainPanel.repaint();
mainPanel.revalidate();
}
});
mainPanel.add(addButton);
}
public static void main(String[] args) {
AddButtonExample example = new AddButtonExample("Add Button Example");
example.setSize(400, 200);
example.setVisible(true);
}
}
Note that repaint doesn't cause the new JButton to be shown - revalidate does
--
flumpman
Make the component invisible and visible
//
component.setVisible(false );
component.setVisible(true) ;
//
//
component.setVisible(false
component.setVisible(true)
//
use setVisible(true), show().
repaint();