ListBox (JList) not visible on application start up

  • Java
  • Thread starter zak100
  • Start date
  • Tags
    Application
In summary: 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.
  • #1
zak100
462
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
  • #2
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
  • #3
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.
 

Related to ListBox (JList) not visible on application start up

1. Why is my ListBox (JList) not showing up when I start my application?

There could be several reasons why your ListBox is not visible on application start up. Some possible causes are incorrect placement or sizing of the ListBox, incorrect data binding, or an error in the code that is preventing the ListBox from being displayed.

2. How can I troubleshoot why my ListBox is not visible?

To troubleshoot this issue, you can try resizing and repositioning the ListBox to make sure it is not hidden behind another component. You can also check the code for any errors, and make sure the data binding is correctly set up. Additionally, try adding a temporary label or text field above the ListBox to see if the issue is with the ListBox itself or its placement within the application.

3. My ListBox is visible when I manually add items to it, but not when I add items programmatically. What could be causing this?

If your ListBox is visible when you manually add items to it, but not when you add items programmatically, it is possible that the code responsible for adding items to the ListBox is not being executed properly. Check for any errors in this code, and make sure the ListBox is properly bound to the data source.

4. Can the visibility of a ListBox be affected by the layout manager used in my application?

Yes, the visibility of a ListBox (JList) can be affected by the layout manager used in your application. Some layout managers may not display components that are not explicitly added to the layout. Make sure the ListBox is added to the layout either through code or by using a GUI builder tool.

5. Why is my ListBox not visible even though I have set its visibility to true?

If you have set the visibility of your ListBox to true, but it is still not visible, it is possible that the ListBox is being hidden by another component or a container. Check the placement and sizing of the ListBox, and make sure it is not being covered by another component. You can also try resizing the ListBox to make sure it is large enough to be visible.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
989
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
909
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
6K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top