0

I'm developing a JDialog with some components inside as JLabels, JButtons and so on... these stuff are filled in through a file. Ther's also a JComboBox that define which file should be read and every time this JComboBox is changed, I need to refresh my JDialog. Found this but I haven't understood how to deal with it.

This is what I'm doing now:

public class ConfDialog extends JDialog
{

     public ConfDialog(JFrame parent, String title, int whidth, int eight)
     {
         super(parent, title, Dialog.ModalityType.APPLICATION_MODAL);

         this.setSize(new Dimension(whidth, eight));
         this.setLocation(200, 200);
         this.setResizable(false);
         this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

         components();
     }

     public void components()
     {

         //JLabels, JButtons, ecc... declarations

         final JComboBox<File> box = new JComboBox<File>(stuffInFolder);

         box.addActionListener(new ActionListener()
         {
             public void actionPerformed(ActionEvent e)
             {
                 validate();//<----
                 repaint(); //<----
             }
         });

         //...something else...

     }

     @Override
     public void repaint()
     {
         components();
         super.repaint();
     }

 }
Community
  • 1
  • 1
Andrea Grimandi
  • 631
  • 2
  • 8
  • 32
  • Read the file, apply the changes you need to the components that need to, in almost all cases, they will update them selves. If you "really" have to, you could call `revalidate` followed by `repaint`. Oh, and kill the `repaint` method, that's going to cause no end of issues.... – MadProgrammer Jul 03 '15 at 06:31

1 Answers1

2

When the ActionListener for the JComboBox is triggered, read the selected file, then based on the information from the file, update the state/properties of the UI components as required, for example, call setText on your labels, fields and buttons.

public class ConfDialog extends JDialog {

    public ConfDialog(JFrame parent, String title, int whidth, int eight) {
        super(parent, title, Dialog.ModalityType.APPLICATION_MODAL);

        this.setSize(new Dimension(whidth, eight));
        this.setLocation(200, 200);
        this.setResizable(false);
        this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

        components();
    }

    public void components() {

        //JLabels, JButtons, ecc... declarations
        final JComboBox<File> box = new JComboBox<File>(stuffInFolder);

        box.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                File file = (File) box.getSelectedItem();
                if (file != null) {
                // Read file
                    // Update the fields on your UI, using things like setText 
                    // and other methods which change the output of the UI
                }
            }
        });

        //...something else...
    }

}

Most of the components are smart enough to update themselves. If you still have some issues, you could call revalidate followed by repaint

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366