再次为不同的自定义列表重用JScrollPane时出现问题

时间:2010-11-14 05:44:48

标签: java swing jlist

我正在尝试创建一个可以重用的JScrollPane组件,具体取决于传递的字符串参数。 我创建了以下代码,如果我使用JFrame将JScrollPane嵌入其中,它可以工作, 但是当我尝试在创建不同的JScrollPane时重用代码时,scrollPane根本不显示.. 我正在java swing项目中实现它.. 请让我知道在这应该做什么..任何建议都非常感谢。

我遇到与JComboBox相同的问题,它没有显示......

public class ListComponent extends JScrollPane {

    private String[] selectedNames;
    private String[] listNames;
    private JButton submit, reset;
    private JPanel subPanel = new JPanel();
    private JList list;
    private JScrollPane scroll;

    public String getFirstSelectionInList() {
        return selectedNames[0];
    }

    ListComponent() {

        /*submit = new JButton("Submit");
        reset = new JButton("Reset");*/
        subPanel.setLayout(new FlowLayout());
        /*subPanel.add(submit);
        subPanel.add(reset);*/
    }

    ListComponent(String[] listNames) {
        this();
        /*JFrame frame = new JFrame("Creating a JList Component");*/

        this.listNames = new String[listNames.length];
        this.listNames = listNames;
        //this.setLayout(new BorderLayout());
        //this.setLayout(new ScrollPaneLayout());
        JPanel panel = new JPanel();
        list = new JList(listNames);
        scroll = new JScrollPane(list);
        scroll.setVerticalScrollBarPolicy(
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        /*panel.add(scroll);
        panel.add(subPanel,BorderLayout.SOUTH);*/

        /*frame.add(panel);
        frame.pack();
        frame.setVisible(true);*/

        this.setVisible(true);
        list.addListSelectionListener(listSelectionListener);
    }

    ListSelectionListener listSelectionListener = new ListSelectionListener() {

        public void valueChanged(ListSelectionEvent listSelectionEvent) {

            boolean adjust = listSelectionEvent.getValueIsAdjusting();
            //System.out.println(", Adjusting? " + adjust);
            if (!adjust) {
                JList list = (JList) listSelectionEvent.getSource();
                int selections[] = list.getSelectedIndices();
                Object selectionValues[] = list.getSelectedValues();
                int n = selections.length;
                selectedNames = new String[n];
                for (int i = 0; i < n; i++) {
                    if (i == 0) {
                        System.out.print("  Selections: ");
                    }
                    selectedNames[i] = (String) selectionValues[i];
                    System.out.print(selectedNames[i]
                        + "/" + selectionValues[i]);
                }
            }
        }
    };

    /** @return the selectedNames */
    public String[] getSelectedNames() {
        return selectedNames;
    }

    /** @param selectedNames the selectedNames to set */
    public void setSelectedNames(String[] selectedNames) {
        this.selectedNames = selectedNames;
    }

    public static void main(String args[]) {
        String subject[] = {"Math", "Computer", "Phisics", "Chemestry"};
        new ListComponent(subject);
    }
}

2 个答案:

答案 0 :(得分:4)

@camickr是对的; JPanel是一个更好的“轻量级组件的通用容器”。您可以将其他组件添加到以下示例中使用的BorderLayout的其他区域。在event dispatch thread上构建GUI也是一个好习惯。

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

/** @see http://stackoverflow.com/questions/4176343 */
public class ListPanel extends JPanel {

    private JList list;

    public ListPanel(String[] data) {
        super(new BorderLayout());
        list = new JList(data);
        list.addListSelectionListener(new SelectionHandler());
        JScrollPane jsp = new JScrollPane(list);
        this.add(jsp, BorderLayout.CENTER);
    }

    private class SelectionHandler implements ListSelectionListener {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                System.out.println(Arrays.toString(list.getSelectedValues()));
            }
        }
    }

    private void display() {
        JFrame f = new JFrame("ListPanel");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                String[] data = {"Math", "Computer", "Physics", "Chemistry"};
                new ListPanel(data).display();
            }
        });
    }
}

答案 1 :(得分:1)

  

public class ListComponent扩展了JScrollPane

什么?您不应该扩展JScrollPane。您没有向滚动窗格添加新功能。

如果您正在尝试将JList添加到JScrollPane,那么我建议您阅读JList API并按照“如何使用列表”的Swing教程链接获取工作示例。