Forum Stats

  • 3,727,536 Users
  • 2,245,406 Discussions
  • 7,852,848 Comments

Discussions

Problem with Displaying ListBox (i.e. JList)

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited July 2016 in New To Java

Hi,

I am 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.

My code is:

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);

@";

        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");

        }

}

Some body please guide me.

Zulfi.

Best Answer

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited July 2016 Accepted Answer

    Hi,

    Thanks. Actually problem was with BorderLayout.CENTER. I replaced it with BorderLayout.EAST. Also my setVisible(..) statement was misplaced. The complete code is:

    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);

    @";

    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

             }

          });

        }

    }

        Zulfi.

Answers

  • 836548
    836548 Member Posts: 286
    edited July 2016

    Hi Zulfi,

    replace the below code

    <code>

            frame.setVisible(true);

            frame.setSize(500,200);

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    <code>

    after adding the listbox to panel.

    example

    <code>

    panel.add( listbox, BorderLayout.EAST );

    frame.setVisible(true);

    frame.setSize(500,200);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    pack();

    <code>

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited July 2016 Accepted Answer

    Hi,

    Thanks. Actually problem was with BorderLayout.CENTER. I replaced it with BorderLayout.EAST. Also my setVisible(..) statement was misplaced. The complete code is:

    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);

    @";

    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

             }

          });

        }

    }

        Zulfi.

This discussion has been closed.