Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

java - Problem with reusing the JScrollPane again for different custom Lists

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);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...