Question

i want to refresh a JList when i push a button in another JFrame.

So i have a JFrame GuiBoss that manages employees (add,delete,update).When i press the button add, another Jframe opens, in wich i create a new employee.

//Open the "add_form" where i give details about a new employee.

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       
    GuiBoss gb = new GuiBoss(contrb,boss);
    Add_form af = new Add_form(gb,contrb,boss);
    af.setVisible(true);

}

//refresh the list with the new employee added.

public void refresh(Employee e){
    System.out.println("I reach this point!");
    //if i print e.getName() it works, printing the right name that i give in the    "add_form"
    listModel.addElement(e);
    //listModel.clear(); //don't work either.

}

My problem is that when i submit the details about the new employee i call the function refresh(Employee e) from the GuiBoss frame , the message ("I reach this point!") shows up on the console, the size of the listModel changes, but the list it doesen't refresh. Also i must say that i set the model properly for the list.

//take data from form and call refresh(Employee e) from the main frame("GuiBoss")

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       
    //String Id = txtID.getText();
    String UserName = txtName.getText();
    txtHour.setVisible(false);
    boolean b = false;
    if(rbtnYes.isSelected() == true){
        b = true;
    }
    if(rbtnNo.isSelected() == true){
        b = false;
    }
    if(rbtnYes.isSelected()==false && rbtnNo.isSelected() == false){
        System.out.println("Select the presence!");
    }
    else{
        txtOra.setVisible(true);
        String Hour = txtHour.getText();
        e = new Employee(UserName,b,Hour,boss);  //boss i get from main frame when i start this add new employee form
        contrb.addEmployee(e);
        gb.refresh(e);  //gb is of type GuiBoss were i have the function that does      
        // the refresh
    }  
}

Please let me know if u have any ideeas.Thanks.

Was it helpful?

Solution

Instead of popping up another frame, why not use a modal JDialog to collect the information about the new employee. When the dialog is closed, you can then extract the details from the dialog and refresh the list from within the current frame.

This prevents the need to expose portions of your API unnecessarily.

Check out How to use Dialogs for details.

Updated

Assuming you've set the model correctly, then your code should work...as per this example...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestList03 {

    public static void main(String[] args) {
        new TestList03();
    }

    public TestList03() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private DefaultListModel model;

        public TestPane() {
            setLayout(new BorderLayout());
            model = new DefaultListModel();
            JList list = new JList(model);
            add(new JScrollPane(list));
            JButton btn = new JButton("Add");
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    model.addElement("New Element");
                }
            });
            add(btn, BorderLayout.SOUTH);
        }

    }

}

That would suggest that there is something else wrong that you're not showing us...

Updated with possible fix for reference issues

This basically demonstrates passing a reference of the main panel to a sub factory that is responsible for actually adding the value back into the main panel. Normally I'd use a interface of some kind instead of exposing the entire panel to simply provide access to a single method, but this was a quick example.

It uses both a normal implements and inner class as ActionListener to demonstrate the two most common means for passing a reference of "self" to another class.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestList03 {

    public static void main(String[] args) {
        new TestList03();
    }

    public TestList03() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel implements ActionListener {

        private DefaultListModel model;

        public TestPane() {
            setLayout(new BorderLayout());
            model = new DefaultListModel();
            JList list = new JList(model);
            add(new JScrollPane(list));

            JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER));

            JButton btn1 = new JButton("Add 1");
            btn1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    new Factory(TestPane.this, "Added by Button 1");
                }
            });
            buttons.add(btn1);

            JButton btn2 = new JButton("Add 2");
            btn2.addActionListener(this);
            buttons.add(btn2);

            add(buttons, BorderLayout.SOUTH);
        }

        public void addItem(String text) {
            model.addElement(text);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            new Factory(TestPane.this, "Added by Button 2");
        }
    }

    public class Factory {

        public Factory(TestPane testPane, String text) {
            testPane.addItem(text);
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top