JDialog中的JList大小

时间:2019-04-12 20:14:35

标签: java swing jscrollpane jlist jdialog

我正在遵循previous suggestion,采用JList并在我的setVisibleRowCount()中调用JDialog

我试图将行数限制为2;但是,对话框包装在一个大的,未滚动的JList中。我希望它只显示2行,而其余部分将通过滚动到达,从而为设计中将来的第三个元素(订单列表)留出空间。

the oversized dialog

请查看SSCCE。

TestDialog.java

package testdialog;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class TestDialog {
    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton button = new JButton("Click me");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new JDViewCustomer(frame, true).setVisible(true);
            }
        });

        frame.getContentPane().add(button, BorderLayout.CENTER);

        frame.pack();
        frame.setVisible(true);
    }

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

CustomerAddress.java

package testdialog;

public class CustomerAddress {

    private final String title;

    public CustomerAddress(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }
}

JDViewCustomer.java

package testdialog;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class JDViewCustomer extends javax.swing.JDialog {

    private static final long serialVersionUID = 1L;

    private final JPanel jPanelCustomerInfo;
    private final JPanel jPanelCustomerAddresses;
    private final JPanel jPanelCustomerOrders;
    private JTextField txtarea_1;
    private JPanel panelNomeDoCliente;
    private JPanel panelDadosDoCliente;
    private JPanel panelAbaixoDeCustomerAddresses;
    private JPanel panelTituloEnderecos;
    private JPanel panelContendoOsEnderecos;
    private JLabel lblEnderecos;
    private JPanel panel;
    private JScrollPane scrollPane;
    private JList<CustomerAddress> jListCustomerAddresses;

    public JDViewCustomer(java.awt.Frame parent, boolean modal) {
        super(parent, modal);

        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

                panel = new JPanel();

                getContentPane().add(panel);

                /*scrollPane = new JScrollPane(panel);
                getContentPane().add(scrollPane);*/

                panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

                jPanelCustomerInfo = new JPanel();
                panel.add(jPanelCustomerInfo);
                jPanelCustomerInfo.setBackground(new Color(255, 255, 255));
                jPanelCustomerInfo.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 1, true), new EmptyBorder(10, 10, 10, 10)));

                jPanelCustomerInfo.setLayout(new BoxLayout(jPanelCustomerInfo, BoxLayout.Y_AXIS));

                panelNomeDoCliente = new JPanel();
                panelNomeDoCliente.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 1, true), new EmptyBorder(10, 10, 10, 10)));
                panelNomeDoCliente.setBackground(Color.LIGHT_GRAY);
                jPanelCustomerInfo.add(panelNomeDoCliente);
                GridBagLayout gbl_panelNomeDoCliente = new GridBagLayout();
                gbl_panelNomeDoCliente.columnWidths = new int[]{73, 376, 45, 53, 0};
                gbl_panelNomeDoCliente.rowHeights = new int[]{31, 0};
                gbl_panelNomeDoCliente.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
                gbl_panelNomeDoCliente.rowWeights = new double[]{0.0, Double.MIN_VALUE};
                panelNomeDoCliente.setLayout(gbl_panelNomeDoCliente);

                panelDadosDoCliente = new JPanel();
                panelDadosDoCliente.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 1, true), new EmptyBorder(10, 10, 10, 10)));
                panelDadosDoCliente.setBackground(Color.WHITE);
                jPanelCustomerInfo.add(panelDadosDoCliente);
                GridBagLayout gbl_panelDadosDoCliente = new GridBagLayout();
                gbl_panelDadosDoCliente.columnWidths = new int[]{58, 199, 38, 102, 27, 123, 0};
                gbl_panelDadosDoCliente.rowHeights = new int[]{0, 14, 0};
                gbl_panelDadosDoCliente.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
                gbl_panelDadosDoCliente.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
                panelDadosDoCliente.setLayout(gbl_panelDadosDoCliente);

                jPanelCustomerAddresses = new JPanel();


                scrollPane = new JScrollPane(jPanelCustomerAddresses);
                getContentPane().add(scrollPane);

                //panel.add(jPanelCustomerAddresses);
                jPanelCustomerAddresses.setBackground(new Color(255, 255, 255));
                jPanelCustomerAddresses.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 1, true), new EmptyBorder(10, 10, 10, 10)));
                jPanelCustomerAddresses.setLayout(new BoxLayout(jPanelCustomerAddresses, BoxLayout.Y_AXIS));

                panelAbaixoDeCustomerAddresses = new JPanel();
                panelAbaixoDeCustomerAddresses.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
                jPanelCustomerAddresses.add(panelAbaixoDeCustomerAddresses);
                panelAbaixoDeCustomerAddresses.setLayout(new BoxLayout(panelAbaixoDeCustomerAddresses, BoxLayout.Y_AXIS));

                panelTituloEnderecos = new JPanel();
                panelTituloEnderecos.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
                FlowLayout fl_panelTituloEnderecos = (FlowLayout) panelTituloEnderecos.getLayout();
                fl_panelTituloEnderecos.setAlignment(FlowLayout.LEFT);
                panelTituloEnderecos.setBackground(Color.LIGHT_GRAY);
                panelAbaixoDeCustomerAddresses.add(panelTituloEnderecos);

                lblEnderecos = new JLabel("Endereço");
                panelTituloEnderecos.add(lblEnderecos);

                        panelContendoOsEnderecos = new JPanel();
                        panelContendoOsEnderecos.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 1, true), new EmptyBorder(10, 10, 10, 10)));
                        panelContendoOsEnderecos.setBackground(Color.WHITE);
                        panelAbaixoDeCustomerAddresses.add(panelContendoOsEnderecos);
                        panelContendoOsEnderecos.setLayout(new BoxLayout(panelContendoOsEnderecos, BoxLayout.Y_AXIS));

                        jPanelCustomerOrders = new JPanel();
                        panel.add(jPanelCustomerOrders);

                        txtarea_1 = new JTextField();
                        txtarea_1.setText("Área 3");
                        jPanelCustomerOrders.add(txtarea_1);
                        txtarea_1.setColumns(10);

        initComponents();
        pack();
        setLocationRelativeTo(parent);

        LoadCustomerDataWorker worker = new LoadCustomerDataWorker(this);
        ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();
        singleThreadPool.execute(worker);
    }

    public void addCustomerAddresses(List<CustomerAddress> customerAddresses) {
        Objects.requireNonNull(customerAddresses);

        DefaultListModel<CustomerAddress> listModel = new DefaultListModel<>();
        for (CustomerAddress customerAddress : customerAddresses) {
            listModel.addElement(customerAddress);
        }

        jListCustomerAddresses = new JList<>(listModel);
        jListCustomerAddresses.setCellRenderer(new PanelIndividualCustomerAddress());
        jListCustomerAddresses.setVisibleRowCount(2);
        panelContendoOsEnderecos.add(jListCustomerAddresses);

        pack();
    }

    private void initComponents() {
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Ver Cliente");
    }
}

LoadCustomerDataWorker.java

package testdialog;
import java.util.ArrayList;
import java.util.List;

import javax.swing.SwingWorker;

import testdialog.JDViewCustomer;
import testdialog.CustomerAddress;

public class LoadCustomerDataWorker extends SwingWorker<Void, Void> {

    private final JDViewCustomer dialog;
    private List<CustomerAddress> customerAddresses = null;


    public LoadCustomerDataWorker(JDViewCustomer dialog) {
        this.dialog = dialog;
    }

    @Override
    protected Void doInBackground() throws Exception {
        customerAddresses = new ArrayList<>();
        customerAddresses.add(new CustomerAddress("1. Casa"));
        customerAddresses.add(new CustomerAddress("2. Trabalho"));
        customerAddresses.add(new CustomerAddress("3. Casa"));
        customerAddresses.add(new CustomerAddress("4. Trabalho"));

        return null;
    }

    @Override
    protected void done() {
        if (customerAddresses != null && customerAddresses.size() > 0) {
            dialog.addCustomerAddresses(customerAddresses);
        }
    }
}

PanelIndividualCustomerAddress.java

package testdialog;

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import testdialog.CustomerAddress;

public class PanelIndividualCustomerAddress implements ListCellRenderer<CustomerAddress> {

    @Override
    public Component getListCellRendererComponent(JList<? extends CustomerAddress> list, CustomerAddress value, int index, boolean isSelected,
            boolean cellHasFocus) {

        JPanel panelEnderecoIndividual = new JPanel();
        panelEnderecoIndividual.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 1, true), new EmptyBorder(3, 3, 3, 3)));
        panelEnderecoIndividual.setBackground(Color.WHITE);
        panelEnderecoIndividual.setLayout(new BoxLayout(panelEnderecoIndividual, BoxLayout.Y_AXIS));

        JPanel panelTituloEndereco = new JPanel();
        panelTituloEndereco.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 1, true), new EmptyBorder(10, 10, 10, 10)));
        FlowLayout fl_panelTituloEndereco = (FlowLayout) panelTituloEndereco.getLayout();
        fl_panelTituloEndereco.setAlignment(FlowLayout.LEFT);
        panelTituloEndereco.setBackground(new Color(220, 220, 220));
        panelEnderecoIndividual.add(panelTituloEndereco);

        JLabel lblCasa = new JLabel(value.getTitle()); // "Casa"
        panelTituloEndereco.add(lblCasa);

        JPanel panelDadosDoEndereco = new JPanel();
        panelDadosDoEndereco.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 1, true), new EmptyBorder(10, 10, 10, 10)));
        panelDadosDoEndereco.setBackground(Color.WHITE);
        panelEnderecoIndividual.add(panelDadosDoEndereco);
        GridBagLayout gbl_panelDadosDoEndereco = new GridBagLayout();
        gbl_panelDadosDoEndereco.columnWidths = new int[]{83, 184, 68, 102, 65, 134, 0};
        gbl_panelDadosDoEndereco.rowHeights = new int[]{0, 0, 20, 0};
        gbl_panelDadosDoEndereco.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        gbl_panelDadosDoEndereco.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
        panelDadosDoEndereco.setLayout(gbl_panelDadosDoEndereco);

        JLabel lblEndereco = new JLabel("Endereço:");
        lblEndereco.setFont(new Font("Tahoma", Font.BOLD, 11));
        GridBagConstraints gbc_lblEndereo = new GridBagConstraints();
        gbc_lblEndereo.anchor = GridBagConstraints.WEST;
        gbc_lblEndereo.insets = new Insets(0, 0, 5, 5);
        gbc_lblEndereo.gridx = 0;
        gbc_lblEndereo.gridy = 0;
        panelDadosDoEndereco.add(lblEndereco, gbc_lblEndereo);

        JLabel lblNomeDaRua = new JLabel("Nome da rua");
        GridBagConstraints gbc_lblNomeDaRua = new GridBagConstraints();
        gbc_lblNomeDaRua.anchor = GridBagConstraints.WEST;
        gbc_lblNomeDaRua.gridwidth = 3;
        gbc_lblNomeDaRua.insets = new Insets(0, 0, 5, 5);
        gbc_lblNomeDaRua.gridx = 1;
        gbc_lblNomeDaRua.gridy = 0;
        panelDadosDoEndereco.add(lblNomeDaRua, gbc_lblNomeDaRua);

        JLabel lblNumero = new JLabel("Número:");
        lblNumero.setFont(new Font("Tahoma", Font.BOLD, 11));
        GridBagConstraints gbc_lblNumero = new GridBagConstraints();
        gbc_lblNumero.anchor = GridBagConstraints.WEST;
        gbc_lblNumero.insets = new Insets(0, 0, 5, 5);
        gbc_lblNumero.gridx = 4;
        gbc_lblNumero.gridy = 0;
        panelDadosDoEndereco.add(lblNumero, gbc_lblNumero);

        JLabel lblValorDoNumero = new JLabel("Valor do número");
        GridBagConstraints gbc_lblValorDoNumero = new GridBagConstraints();
        gbc_lblValorDoNumero.anchor = GridBagConstraints.WEST;
        gbc_lblValorDoNumero.insets = new Insets(0, 0, 5, 0);
        gbc_lblValorDoNumero.gridx = 5;
        gbc_lblValorDoNumero.gridy = 0;
        panelDadosDoEndereco.add(lblValorDoNumero, gbc_lblValorDoNumero);

        JLabel lblComplemento = new JLabel("Complemento:");
        lblComplemento.setFont(new Font("Tahoma", Font.BOLD, 11));
        GridBagConstraints gbc_lblComplemento = new GridBagConstraints();
        gbc_lblComplemento.anchor = GridBagConstraints.WEST;
        gbc_lblComplemento.insets = new Insets(0, 0, 5, 5);
        gbc_lblComplemento.gridx = 0;
        gbc_lblComplemento.gridy = 1;
        panelDadosDoEndereco.add(lblComplemento, gbc_lblComplemento);

        JLabel lblTextoDoComplemento = new JLabel("Texto do complemento");
        GridBagConstraints gbc_lblTextoDoComplemento = new GridBagConstraints();
        gbc_lblTextoDoComplemento.anchor = GridBagConstraints.WEST;
        gbc_lblTextoDoComplemento.insets = new Insets(0, 0, 5, 5);
        gbc_lblTextoDoComplemento.gridx = 1;
        gbc_lblTextoDoComplemento.gridy = 1;
        panelDadosDoEndereco.add(lblTextoDoComplemento, gbc_lblTextoDoComplemento);

        JLabel lblBairro = new JLabel("Bairro:");
        lblBairro.setFont(new Font("Tahoma", Font.BOLD, 11));
        GridBagConstraints gbc_lblBairro = new GridBagConstraints();
        gbc_lblBairro.anchor = GridBagConstraints.WEST;
        gbc_lblBairro.insets = new Insets(0, 0, 5, 5);
        gbc_lblBairro.gridx = 2;
        gbc_lblBairro.gridy = 1;
        panelDadosDoEndereco.add(lblBairro, gbc_lblBairro);

        JLabel lblNomeDoBairro = new JLabel("Nome do bairro");
        GridBagConstraints gbc_lblNomeDoBairro = new GridBagConstraints();
        gbc_lblNomeDoBairro.anchor = GridBagConstraints.WEST;
        gbc_lblNomeDoBairro.insets = new Insets(0, 0, 5, 5);
        gbc_lblNomeDoBairro.gridx = 3;
        gbc_lblNomeDoBairro.gridy = 1;
        panelDadosDoEndereco.add(lblNomeDoBairro, gbc_lblNomeDoBairro);

        JLabel lblCidade = new JLabel("Cidade:");
        lblCidade.setFont(new Font("Tahoma", Font.BOLD, 11));
        GridBagConstraints gbc_lblCidade = new GridBagConstraints();
        gbc_lblCidade.anchor = GridBagConstraints.WEST;
        gbc_lblCidade.insets = new Insets(0, 0, 5, 5);
        gbc_lblCidade.gridx = 4;
        gbc_lblCidade.gridy = 1;
        panelDadosDoEndereco.add(lblCidade, gbc_lblCidade);

        JLabel lblNomeDaCidade = new JLabel("Nome da cidade");
        GridBagConstraints gbc_lblNomeDaCidade = new GridBagConstraints();
        gbc_lblNomeDaCidade.anchor = GridBagConstraints.WEST;
        gbc_lblNomeDaCidade.insets = new Insets(0, 0, 5, 0);
        gbc_lblNomeDaCidade.gridx = 5;
        gbc_lblNomeDaCidade.gridy = 1;
        panelDadosDoEndereco.add(lblNomeDaCidade, gbc_lblNomeDaCidade);

        JLabel lblCep = new JLabel("CEP:");
        lblCep.setFont(new Font("Tahoma", Font.BOLD, 11));
        GridBagConstraints gbc_lblCep = new GridBagConstraints();
        gbc_lblCep.anchor = GridBagConstraints.WEST;
        gbc_lblCep.insets = new Insets(0, 0, 0, 5);
        gbc_lblCep.gridx = 0;
        gbc_lblCep.gridy = 2;
        panelDadosDoEndereco.add(lblCep, gbc_lblCep);

        JLabel lblNumeroDoCep = new JLabel("Número do CEP");
        GridBagConstraints gbc_lblNumeroDoCep = new GridBagConstraints();
        gbc_lblNumeroDoCep.anchor = GridBagConstraints.WEST;
        gbc_lblNumeroDoCep.insets = new Insets(0, 0, 0, 5);
        gbc_lblNumeroDoCep.gridx = 1;
        gbc_lblNumeroDoCep.gridy = 2;
        panelDadosDoEndereco.add(lblNumeroDoCep, gbc_lblNumeroDoCep);

        JLabel lblPontoRef = new JLabel("Ponto ref.:");
        lblPontoRef.setFont(new Font("Tahoma", Font.BOLD, 11));
        GridBagConstraints gbc_lblPontoRef = new GridBagConstraints();
        gbc_lblPontoRef.anchor = GridBagConstraints.WEST;
        gbc_lblPontoRef.insets = new Insets(0, 0, 0, 5);
        gbc_lblPontoRef.gridx = 2;
        gbc_lblPontoRef.gridy = 2;
        panelDadosDoEndereco.add(lblPontoRef, gbc_lblPontoRef);

        JLabel lblPertoDeTalLugar = new JLabel("Perto de tal lugar");
        GridBagConstraints gbc_lblPertoDeTalLugar = new GridBagConstraints();
        gbc_lblPertoDeTalLugar.anchor = GridBagConstraints.WEST;
        gbc_lblPertoDeTalLugar.gridwidth = 3;
        gbc_lblPertoDeTalLugar.gridx = 3;
        gbc_lblPertoDeTalLugar.gridy = 2;
        panelDadosDoEndereco.add(lblPertoDeTalLugar, gbc_lblPertoDeTalLugar);


        return panelEnderecoIndividual;
    }
}

1 个答案:

答案 0 :(得分:2)

setVisibleRowCount()仅在JList是JScrollPane的视图(即JScrollPane的主要子视图)时才有效。在您的程序中,JScrollPane的视图是JPanel,而不是JList。