Java Swing JList, can't set minimum width

2019-06-11 04:34发布

问题:

Trying to simple create a JList with a specified visible row count, but a minimum width. It seems "setMinimumSize()" does nothing though...

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



public class UserInterface 
{   
    final static private int HEIGHT = 400;
    final static private int WIDTH = 650;

    public static void main(String[] args) {

    JPanel content = new JPanel();

    String[] entries = { "Entry 1", "Entry 2", "Entry 3",
    "Entry 4", "Entry 5", "Entry 6" };

    DefaultListModel sampleModel = new DefaultListModel();

    for(int i=0; i<entries.length; i++)
        sampleModel.addElement(entries[i]);

    JList sampleList = new JList(sampleModel);

    sampleList.setMinimumSize(new Dimension(1000,1000));
    sampleList.setMaximumSize(new Dimension(1000,1000));

    content.add(sampleList);

    //main window frame
    JFrame window = new JFrame("NAD Assignment 1");
    window.setSize(WIDTH, HEIGHT);
    window.setContentPane(content);
    window.setLocationRelativeTo(null);
    window.setVisible(true);

    window.addWindowListener(new WindowAdapter()
    {
        public void windowClosing(WindowEvent e)
        {
            System.exit(0); 
        }
    });
}
}

setPreferredSize() seems to work, but it overrides whatever I set up in setVisibleRowCount():

public static void main(String[] args) {

    JPanel content = new JPanel();

    String[] entries = { "Entry 1", "Entry 2", "Entry 3",
    "Entry 4", "Entry 5", "Entry 6" };

    DefaultListModel sampleModel = new DefaultListModel();

    for(int i=0; i<entries.length; i++)
        sampleModel.addElement(entries[i]);

    JList sampleList = new JList(sampleModel);

    sampleList.setPreferredSize(new Dimension(200,10));
    sampleList.setVisibleRowCount(8);

    content.add(sampleList);

    //main window frame
    JFrame window = new JFrame("NAD Assignment 1");
    window.setSize(WIDTH, HEIGHT);
    window.setContentPane(content);
    window.setLocationRelativeTo(null);
    window.setVisible(true);

    window.addWindowListener(new WindowAdapter()
    {
        public void windowClosing(WindowEvent e)
        {
            System.exit(0); 
        }
    });
}

How can I set a minimum width on my JList and specify the height with setVisibleRowCount? Thanks.

回答1:

There were several things wrong with your code.

  1. Always invoke a Swing application with SwingUtilities. This ensures that the Swing components are created and executed on the Event Dispatch thread (EDT).

  2. Separate out the data creation from the GUI creation. This allows you to focus on one part of the code at a time. Also, put your code into class methods, rather than trying to write everything in the main method.

  3. Break your application up into smaller and smaller pieces until you can code each piece without much thought. The thinking is in breaking up your application.

  4. You must use a Swing layout. I chose the BorderLayout, but another layout may work better for what you want. Study Oracle's Visual Guide to Layout Managers until you can describe the Swing layout managers in your sleep.

  5. Finally, JList works better when you enclose it in a JScrollPane.

.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class UserInterface implements Runnable {
    final static private int    HEIGHT  = 400;
    final static private int    WIDTH   = 650;

    private String[]            entries = { "Entry 1", "Entry 2", "Entry 3",
            "Entry 4", "Entry 5", "Entry 6" };

    private DefaultListModel    sampleModel;

    public UserInterface() {
        this.sampleModel = new DefaultListModel();
    }

    @Override
    public void run() {
        createPartControl();
    }

    protected void createPartControl() {
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());

        for (int i = 0; i < entries.length; i++) {
            sampleModel.addElement(entries[i]);
        }

        JList sampleList = new JList(sampleModel);
        sampleList.setMinimumSize(new Dimension(1000, 1000));
        sampleList.setMaximumSize(new Dimension(1000, 1000));

        JScrollPane scrollPane = new JScrollPane(sampleList);

        content.add(scrollPane, BorderLayout.CENTER);

        // main window frame
        JFrame window = new JFrame("NAD Assignment 1");
        window.setSize(WIDTH, HEIGHT);
        window.setContentPane(content);
        window.setLocationRelativeTo(null);
        window.setVisible(true);

        window.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new UserInterface());
    }

}


回答2:

// assuming Strings are being used:    
sampleList.setPrototypeCellValue("1234567890");  // or whatever

See: JList.setPrototypeCellValue(Object)