I programmed a JFrame that have a Button and JList , and when i click on the Button , the JList list will be displayed . Instead it shows nothing unless i click on maximaze , or refresh the frame. the button listener class
class b0listener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
MessageList.removeAll();
Messages = new JList(lireRepertoire("C:/Documents and Settings/Java/eclipse data file"));
Pane =new JScrollPane(Messages);
Pane.setPreferredSize(new Dimension(400,400));
//Messages.setMaximumSize(MessageList.getPreferredSize()) ;
MessageList.add( Pane);
}}
the class constructor code
Fframe.setTitle("Boite Message");
Fframe.setSize(800,300);
Fframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Fframe.setVisible(true);
// panels Layout
ButtonMenu.setLayout(new BoxLayout(ButtonMenu, BoxLayout.Y_AXIS));//Jpanel
MessageList.setLayout(new FlowLayout());//JPanel
ButtonMenu.setBackground(Color.LIGHT_GRAY);
MessageList.setBackground(Color.orange);
MessageList.setPreferredSize(new Dimension(400, 400));
Fframe.add(ButtonMenu,BorderLayout.WEST);
Fframe.add(MessageList,BorderLayout.CENTER);
ButtonMenu.add(b0);
b0.addActionListener(new b0listener());
Pane =new JScrollPane(Messages);
Messages.setPreferredSize(new Dimension(800,250));
//Pane.setMaximumSize(MessageList.getSize()) ;
MessageList.add( Pane);
I already declared the Jpanels and Jframe , button as class members
First of all, learn an use the proper Java naming conventions. Variables should not start with an upper case character.
When dynamically adding/removing components from a panel you need to revalidate() the panel:
messageList.add( pane);
messageList.revalidate();
messageList.repaint();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With