ListBox (JList) not visible on application start up

  • Context: Java 
  • Thread starter Thread starter zak100
  • Start date Start date
  • Tags Tags
    Application
Click For Summary
SUMMARY

The forum discussion addresses an issue with a Java Swing application where a JList (listbox) is not visible upon startup. The user, Zulfi, initially implemented the layout using BorderLayout, which prioritized the button over the listbox. A solution was provided to change the listbox's position to BorderLayout.EAST and to invoke the GUI construction on the Event Dispatch Thread using SwingUtilities.invokeLater. This approach ensured that both components are displayed correctly without requiring window resizing.

PREREQUISITES
  • Understanding of Java Swing components, specifically JList and JButton
  • Familiarity with layout managers in Java, particularly BorderLayout
  • Knowledge of thread safety in GUI applications using SwingUtilities
  • Basic Java programming skills, including event handling with ActionListener
NEXT STEPS
  • Explore advanced layout managers in Java Swing, such as GridBagLayout
  • Learn about Java Swing threading models and best practices for GUI updates
  • Investigate the use of DefaultListModel for dynamic data management in JLists
  • Study Java Swing event handling and custom action listeners for enhanced interactivity
USEFUL FOR

Java developers, particularly those working with GUI applications, and anyone seeking to improve their understanding of Swing layout management and event handling.

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   Reactions: 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.
 

Similar threads

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