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

发布于 2024-10-01 20:02:42 字数 3063 浏览 1 评论 0原文

我正在尝试创建一个 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);
    }
}

I am trying to create a JScrollPane component which can be reused depending upon the string parameter that is being passed..
I have created the following code, it works if I use JFrame to embed the JScrollPane inside it,
but when I try to reuse the code in creating different JScrollPane the scrollPane is not displayed at all..
I am implementing it in a java swing project..
Please let me know what should be done in this..any suggestions are highly appreciated.

The same problem I have with regards to the JComboBox, it doesn't display...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恰似旧人归 2024-10-08 20:02:42

@camickr 是正确的; JPanel 是一个更好的“通用” -用于轻质部件的专用容器。”您可以将其他组件添加到下例中使用的 BorderLayout 的其他区域。在事件调度线程。

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

@camickr is correct; JPanel is a better "general-purpose container for lightweight components." You can add additional components to the other areas of the BorderLayout used in the example below. It's also a good habit to build your GUI on the event dispatch thread.

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();
            }
        });
    }
}
肥爪爪 2024-10-08 20:02:42

公共类ListComponent扩展了JScrollPane

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

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

public class ListComponent extends JScrollPane

What? You should not be extending a JScrollPane. You are not adding new functionality to the scroll pane.

If you are trying to add a JList to a JScrollPane, then I suggest you read the JList API and follow the link to the Swing tutorial on "How to Use Lists" for a working example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文