Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - How to wire one pane to another

How do I wire output to paneWithList?

PaneWithList has a listener on its JList so that the selected row is output to the console. How can I direct that output to the JTextPane on output?

Could PaneWithList fire an event which Main picks up? Would PropertyChangeSupport suffice?

Main.java:

package dur.bounceme.net;

import javax.swing.JTabbedPane;

public class Main {

    private static JTabbedPane tabs;
    private static PaneWithList paneWithList;
    private static PaneWithTable paneWithTable;
    private static Output output;

    public static void main(String[] args) {
        tabs = new javax.swing.JTabbedPane();
        paneWithList = new PaneWithList();
        paneWithTable = new PaneWithTable();
        tabs.addTab("list", paneWithList);
        tabs.addTab("table", paneWithTable);
        tabs.addTab("output", output);
    }
}
Question&Answers:os

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

1 Answer

0 votes
by (71.8m points)

Here's an example using the observer pattern, also seen here, here and here. Note that it would also be possible to listen to the combo's model.

enter image description here

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;

/**
* @see http://en.wikipedia.org/wiki/Observer_pattern
* @see https://stackoverflow.com/a/10523401/230513
*/
public class PropertyChangeDemo {

    public PropertyChangeDemo() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setResizable(false);
        f.add(new ObserverPanel());
        f.pack();
        f.setLocationByPlatform(true);
        f.setVisible(true);
    }

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

            @Override
            public void run() {
                PropertyChangeDemo example = new PropertyChangeDemo();
            }
        });
    }
}

class ObserverPanel extends JPanel {

    private JLabel title = new JLabel("Value received: ");
    private JLabel label = new JLabel("null", JLabel.CENTER);

    public ObserverPanel() {
        this.setBorder(BorderFactory.createTitledBorder("ObserverPanel"));
        JPanel panel = new JPanel(new GridLayout(0, 1));
        panel.add(title);
        panel.add(label);
        this.add(panel);
        ObservedPanel observed = new ObservedPanel();
        observed.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent e) {
                if (e.getPropertyName().equals(ObservedPanel.PHYSICIST)) {
                    String value = e.getNewValue().toString();
                    label.setText(value);
                }
            }
        });
        this.add(observed);
    }
}

class ObservedPanel extends JPanel {

    public static final String PHYSICIST = "Physicist";
    private static final String[] items = new String[]{
        "Alpher", "Bethe", "Gamow", "Dirac", "Einstein"
    };
    private JComboBox combo = new JComboBox(items);
    private String oldValue;

    public ObservedPanel() {
        this.setBorder(BorderFactory.createTitledBorder("ObservedPanel"));
        combo.addActionListener(new ComboBoxListener());
        this.add(combo);
    }

    private class ComboBoxListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            String newValue = (String) combo.getSelectedItem();
            firePropertyChange(PHYSICIST, oldValue, newValue);
            oldValue = newValue;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...