Kevin Schultz
Kevin Schultz

Reputation: 884

JList does not appear inside JPanel

I have made some changes to a program I am working on. Upon great suggestion, I have changed a JTextArea within a JScrollPanel to a JList. For some reason, even though I have coded the contents of the JList, the area appears blank and about 1 row high. I have the default size for each JList set to (200, 200). Any ideas?

Code:

package SwingAssign2;

import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;

import java.awt.BorderLayout;

import javax.swing.JComboBox;
import javax.swing.JTextField;

import java.awt.FlowLayout;

import javax.swing.SwingConstants;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JList;
import javax.swing.AbstractListModel;

public class SwingAssign2 {

    private JFrame frame;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SwingAssign2 window = new SwingAssign2();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public SwingAssign2() {
        initialize();
        frame.pack();
    }

    /**
     * Initialize the contents of the frame.
     */
    @SuppressWarnings("unchecked")
    private void initialize() {
        frame = new JFrame();
        frame.setSize(600, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panelTop = new JPanel();
        FlowLayout fl_panelTop = (FlowLayout) panelTop.getLayout();
        fl_panelTop.setAlignment(FlowLayout.LEFT);
        frame.getContentPane().add(panelTop, BorderLayout.NORTH);
        //panel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

        final JComboBox comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"Select Something", "Selection 1", "Selection 2", "Selection 3", "Selection 4"}));
        panelTop.add(comboBox);
        comboBox.setPreferredSize(new Dimension(160, 20));

        textField = new JTextField();
        panelTop.add(textField);
        textField.setColumns(40);
        textField.setBorder(BorderFactory.createLoweredBevelBorder());

        JPanel panelBottom = new JPanel();
        frame.getContentPane().add(panelBottom, BorderLayout.SOUTH);
        panelBottom.setLayout(new BorderLayout(0, 0));

        final JLabel status = new JLabel("Status");
        status.setHorizontalAlignment(SwingConstants.CENTER);
        panelBottom.add(status, BorderLayout.SOUTH);
        status.setBorder(BorderFactory.createLoweredBevelBorder());

        JScrollPane scrollPaneB = new JScrollPane();  
        scrollPaneB.setSize(200, 200);
        String[] listB = { "Some content on the right panel", "More content", "Some more content", "More and more content", "More and more content", "More and more content",
                "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", 
                "More and more content" }; 
        JList listBottom = new JList(listB);
        listBottom.setVisibleRowCount(12);
        scrollPaneB = new JScrollPane(listBottom);
        panelBottom.add(scrollPaneB);
        scrollPaneB.add(listBottom);
        scrollPaneB.setBorder(BorderFactory.createTitledBorder("Bottom Panel"));
        scrollPaneB.setVisible(true);
        listBottom.setVisible(true);

        JPanel panelCenter = new JPanel();
        frame.getContentPane().add(panelCenter, BorderLayout.CENTER);
        panelCenter.setLayout(new GridLayout(0, 2, 0, 0));

        JScrollPane scrollPaneL = new JScrollPane();  
        scrollPaneL.setSize(200, 200);
        String[] listL = { "Some content on the right panel", "More content", "Some more content", "More and more content", "More and more content", "More and more content",
                "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", 
                "More and more content" }; 
        JList listLeft = new JList(listL);
        listLeft.setVisibleRowCount(12);
        scrollPaneL = new JScrollPane(listLeft);
        panelCenter.add(scrollPaneL);
        scrollPaneL.add(listLeft);
        scrollPaneL.setBorder(BorderFactory.createTitledBorder("Top Left Panel"));
        scrollPaneL.setVisible(true);
        listLeft.setVisible(true);

        JScrollPane scrollPaneR = new JScrollPane();  
        scrollPaneR.setSize(200, 200);
        String[] listR = { "Some content on the right panel", "More content", "Some more content", "More and more content", "More and more content", "More and more content",
                "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", 
                "More and more content" }; 
        JList listRight = new JList(listR);
        listRight.setVisibleRowCount(12);
        scrollPaneR = new JScrollPane(listRight);
        panelCenter.add(scrollPaneR);
        scrollPaneR.add(listRight);
        scrollPaneR.setBorder(BorderFactory.createTitledBorder("Top Right Panel"));
        scrollPaneR.setVisible(true);
        listRight.setVisible(true);






        comboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange() == ItemEvent.SELECTED) {
                    textField.setText((String) comboBox.getSelectedItem());
                    status.setText("comboBox Action Listener Fired");
                }
            }
        });


    }

}

Upvotes: 1

Views: 1265

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You're adding your JLists to both a JScrollPane and the JPanel, and you're not allowed to do that as you can add a component to only one container. Here you will want to add your JLists to their respective JScrollPanes and then add the JScrollPane to your JPanel.


Also, you never add a JList to a JScrollPane per se, but rather you want to add the list to the scrollpane's viewport. This is usually done in one of two ways:

  1. Via the JScrollPane's constructor: new JScrollPane(myList)
  2. Or via a method: myScrollPane.setViewportView(myList)

Also

  • I see you calling setSize(...) in your code, and you'll want to avoid that.
  • You seem to be re-constructing components such as the JScrollPane for no apparent purpose. Again, you don't want to do this either.

Edit
For example:

  //!! JScrollPane scrollPaneB = new JScrollPane();
  //!! scrollPaneB.setSize(200, 200);
  String[] listB = { "Some content on the right panel", "More content",
        "Some more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content" };
  JList listBottom = new JList(listB);
  listBottom.setVisibleRowCount(12);
  JScrollPane scrollPaneB = new JScrollPane(listBottom);
  panelBottom.add(scrollPaneB);
  //!! scrollPaneB.add(listBottom);
  scrollPaneB.setBorder(BorderFactory.createTitledBorder("Bottom Panel"));
  scrollPaneB.setVisible(true);
  // !! listBottom.setVisible(true);

  JPanel panelCenter = new JPanel();
  frame.getContentPane().add(panelCenter, BorderLayout.CENTER);
  panelCenter.setLayout(new GridLayout(0, 2, 0, 0));

  //!! JScrollPane scrollPaneL = new JScrollPane();
  // !! scrollPaneL.setSize(200, 200);
  String[] listL = { "Some content on the right panel", "More content",
        "Some more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content" };
  JList listLeft = new JList(listL);
  listLeft.setVisibleRowCount(12);
  JScrollPane scrollPaneL = new JScrollPane(listLeft);
  panelCenter.add(scrollPaneL);
  // !! scrollPaneL.add(listLeft);
  scrollPaneL.setBorder(BorderFactory.createTitledBorder("Top Left Panel"));
  // !! scrollPaneL.setVisible(true);
  // !! listLeft.setVisible(true);

  //!! JScrollPane scrollPaneR = new JScrollPane();
  //!! scrollPaneR.setSize(200, 200);
  String[] listR = { "Some content on the right panel", "More content",
        "Some more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content", "More and more content",
        "More and more content" };
  JList listRight = new JList(listR);
  listRight.setVisibleRowCount(12);
  JScrollPane scrollPaneR = new JScrollPane(listRight); //!! 
  panelCenter.add(scrollPaneR);
  //!! scrollPaneR.add(listRight);
  scrollPaneR.setBorder(BorderFactory.createTitledBorder("Top Right Panel"));
  //!! scrollPaneR.setVisible(true);
  //!! listRight.setVisible(true);

Upvotes: 6

Related Questions