Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java ListBox (JList) not visible on application start up

  1. Jul 16, 2016 #1
    Hi,

    I am trying to store values in a listbox. I have created frame & a panel inside a frame & i am using BorderLayout inside panel. I have created a button & a listBox & added them to the panel. But when i run the application, it displays the button only & when i increase the size of window then it displays the listbox neighboring to the button.
    Code (Java):
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    /**  the panel
    */

    public class ListBoxEg extends JFrame{
         private JButton button;
         JList<String> listbox;

         DefaultListModel<String> model;

         public ListBoxEg() {
            JFrame frame = new JFrame("Test");
            frame.setVisible(true);
            frame.setSize(500,200);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JPanel panel = new JPanel();
            frame.add(panel);

            panel.setLayout( new BorderLayout() );
            button=new JButton("Hello");
            panel.add(button, BorderLayout.WEST);
         
            String    listData = "ABCDE";
         
         
            // Create a new DefaultListModel
            model = new DefaultListModel<> ( );

            //Adding Elements in the model

            model.addElement(listData);
        listData ="12345";
            model.addElement(listData);
            listData ="!!!!!!";
            model.addElement(listData);
            listData ="@@@@@@";
            model.addElement(listData);

            //Creating a new ListBox
        listbox= new JList<>();

            //Adding Elements in the ListBox
            listbox.setModel(model);
        panel.add( listbox, BorderLayout.CENTER );
            pack();
         
            MyActionListener listener=new MyActionListener();       //added
            button.addActionListener(listener);
        }
        public static void main(String args[ ]) {
           ListBoxEg obj = new ListBoxEg( );
        }

    }
     

    class MyActionListener implements ActionListener{
            public void actionPerformed (ActionEvent e) {
                    System.out.println("pressed button");
            }
    }
     
    Its showing the button when application is started but when i slightly increase the window it shows the listbox also. I feel it can display the listbox without incrementing the window. Some body please guide me what's the problem.

    Zulfi.
     
  2. jcsd
  3. Jul 16, 2016 #2

    jedishrfu

    Staff: Mentor

    BorderLayout.CENTER is a funny area. The layout gives precedence to the other choices: EAST, WEST, NORTH and SOUTH. Components in these choices will always squeeze out the CENTER components.

    I once tried to make a drawing program and placed the canvas in the CENTER component and it completely disappeared until I switched to grdbag where I had full control of the layout of my components.

    I ran your code as is and with EAST instead of CENTER. Try EAST to see if that what you are looking for.

    Also place the setVisible() call as the last one you do that how I've sen it used in other examples.

    Also you should use the invokeLater() in your main as that is a better way to launch a Java GUI application. (see section 10.5 Java Swing GUI Example SwingCounter)

    https://www.ntu.edu.sg/home/ehchua/programming/java/J4a_GUI.html
     
    Last edited: Jul 16, 2016
  4. Jul 17, 2016 #3
    Hi,
    Thanks my friend. It has improved. I found one advantage of thread safety mechanism is that now i cant kill the program externally by pressing Ctrl-C.
    Improved code is:

    Code (Java):
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    /**  the panel
    */

    public class ListBoxEg2 extends JFrame{
         private JButton button;
         JList<String> listbox;

         DefaultListModel<String> model;

         public ListBoxEg2() {
            JFrame frame = new JFrame("Test");
         
            frame.setSize(500,200);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JPanel panel = new JPanel();
            frame.add(panel);

            panel.setLayout( new BorderLayout() );
            button=new JButton("Hello");
            panel.add(button, BorderLayout.WEST);
         
            String    listData = "ABCDE";
         
         
            // Create a new DefaultListModel
            model = new DefaultListModel<> ( );

            //Adding Elements in the model

            model.addElement(listData);
        listData ="12345";
            model.addElement(listData);
            listData ="!!!!!!";
            model.addElement(listData);
            listData ="@@@@@@";
            model.addElement(listData);

            //Creating a new ListBox
        listbox= new JList<>();

            //Adding Elements in the ListBox
            listbox.setModel(model);
        panel.add( listbox, BorderLayout.EAST );
            pack();
         
            MyActionListener listener=new MyActionListener();       //added
            button.addActionListener(listener);
            frame.setVisible(true);
        }
        public static void main(String args[ ]) {
           // Run the GUI construction in the Event-Dispatching thread for thread-safety
          SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
                new ListBoxEg2(); // Let the constructor do the job
             }
          });
        }

    }
     
     
    Problem Solved.
    Zulfi.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: ListBox (JList) not visible on application start up
  1. C#: Where to start? (Replies: 2)

  2. Starting porgramming (Replies: 4)

Loading...