Java ListBox (JList) not visible on application start up

  • Thread starter Thread starter zak100
  • Start date Start date
  • Tags Tags
    Application
Click For Summary
The discussion revolves around an issue with a JList (listbox) not being visible when a Java Swing application starts. The user initially placed the listbox in the CENTER of a BorderLayout, which caused it to be obscured by other components. Suggestions included using the EAST position for the listbox and ensuring the setVisible() method is called last in the constructor. The user implemented these changes, along with using SwingUtilities.invokeLater for thread safety, which resolved the visibility issue. The problem was successfully solved, leading to a functional application.
zak100
Messages
462
Reaction score
11
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.
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.
 
Technology news on Phys.org
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:
  • Like
Likes zak100
Hi,
Thanks my friend. It has improved. I found one advantage of thread safety mechanism is that now i can't kill the program externally by pressing Ctrl-C.
Improved code is:

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.
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
1
Views
2K
  • · Replies 0 ·
Replies
0
Views
643
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 6 ·
Replies
6
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K