Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JButton actionListener

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

like image 880
JaX Avatar asked Apr 11 '26 03:04

JaX


1 Answers

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();
like image 190
camickr Avatar answered Apr 12 '26 17:04

camickr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!