How to pass data between two JInternalFrame?

纵饮孤独 提交于 2019-12-25 02:47:26

问题


i'm making a program, and I have 1 JFrame with JDesktopPane in this frame i open two JInternalFrame and I want to pass data between this two JInternalFrame but with JTextField. I'm just made to pass the data but it doesn't update the JInternalFrame that i want to show. But if i choose to open again it show me the data. Please Help me! THANKS

In this JInternalFrame 2 i sent the data to another JInternalFrame 1

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String word = jTxtIDACA.getText();
    DatosPersonales frame = new DatosPersonales();
    frame.getData(word);
    frame.setVisible(true);
    this.getDesktopPane().add(frame);
    this.dispose();

} 

this is JInternalFrame 1 and i have

public void getData(String word){
    initComponents();
    this.word = word;
    jTxtIDACA.setText(word);

}


回答1:


The basic idea is you want some kind of model, which holds the data and use Observer Pattern to provide notification to interested parties when the model changes in some way.

The two JInternalFrames would then be able to share that model, one could update it and the other could monitor for changes to it (strictly speaking the relationship can work both ways, but we'll leave that alone for the moment)...

Now, because I never know what or how people might like to use the model, I always start with an interface and the provide an abstract implementation and some kind of default implementation if I feel it's required....

public interface FruitBowl {

    public void addFruit(String fruit);
    public void removeFruit(String fruit);
    public List<String> getFruit();

    public void addFruitBowlListener(FruitBowlListener listener);
    public void removeFruitBowlListener(FruitBowlListener listener);

}

public abstract class AbstractFruitBowl implements FruitBowl {

    private EventListenerList listenerList;

    public AbstractFruitBowl() {
    }

    protected EventListenerList getEventListenerList() {
        if (listenerList == null) {
            listenerList = new EventListenerList();
        }
        return listenerList;
    }

    @Override
    public void addFruitBowlListener(FruitBowlListener listener) {
        getEventListenerList().add(FruitBowlListener.class, listener);
    }

    @Override
    public void removeFruitBowlListener(FruitBowlListener listener) {
        getEventListenerList().remove(FruitBowlListener.class, listener);
    }

    protected void fireFruitAdded(String fruit) {
        FruitBowlListener[] listeners = getEventListenerList().getListeners(FruitBowlListener.class);
        if (listeners.length > 0) {
            FruitBowlEvent evt = new FruitBowlEvent(this, fruit);
            for (FruitBowlListener listener : listeners) {
                listener.fruitAdded(evt);
            }
        }
    }

    protected void fireFruitRemoved(String fruit) {
        FruitBowlListener[] listeners = getEventListenerList().getListeners(FruitBowlListener.class);
        if (listeners.length > 0) {
            FruitBowlEvent evt = new FruitBowlEvent(this, fruit);
            for (FruitBowlListener listener : listeners) {
                listener.fruitRemoved(evt);
            }
        }
    }
}

public class DefaultFruitBowl extends AbstractFruitBowl {

    private List<String> fruits;

    public DefaultFruitBowl() {
        fruits = new ArrayList<>(25);
    }

    @Override
    public void addFruit(String fruit) {
        fruits.add(fruit);
        fireFruitAdded(fruit);
    }

    @Override
    public void removeFruit(String fruit) {
        fruits.remove(fruit);
        fireFruitRemoved(fruit);
    }

    @Override
    public List<String> getFruit() {
        return Collections.unmodifiableList(fruits);
    }

}

Next we need to define the observer pattern contract through the use of listeners...

public class FruitBowlEvent extends EventObject {
    private String fruit;

    public FruitBowlEvent(FruitBowl fruitBowl, String fruit) {
        super(fruitBowl);
        this.fruit = fruit;
    }

    public FruitBowl getFruitBowl() {
        return (FruitBowl)getSource();
    }

    public String getFruit() {
        return fruit;
    }

}

public interface FruitBowlListener extends EventListener {

    public void fruitAdded(FruitBowlEvent evt);
    public void fruitRemoved(FruitBowlEvent evt);

}

Next, we define the UI...

public class TestModel {

    public static void main(String[] args) {
        new TestModel();
    }

    public TestModel() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                FruitBowl fb = new DefaultFruitBowl();

                JDesktopPane dp = new JDesktopPane() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(400, 200);
                    }
                };
                JInternalFrame manager = new JInternalFrame("Fruit Bowl Manager", true, true, true, true);
                manager.add(new FruitBowelManagerPane(fb));
                manager.setVisible(true);
                manager.setBounds(0, 0, 200, 200);

                JInternalFrame monitor = new JInternalFrame("Fruit Bowl Monitor", true, true, true, true);
                monitor.add(new FruitBowelMonitorPane(fb));
                monitor.setVisible(true);
                monitor.setBounds(200, 0, 200, 200);

                dp.add(manager);
                dp.add(monitor);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(dp);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public abstract class AbstractFruitPane extends JPanel {

        private FruitBowl fruitBowl;

        public AbstractFruitPane(FruitBowl fruitBowl) {
            this.fruitBowl = fruitBowl;
        }

        public FruitBowl getFruitBowl() {
            return fruitBowl;
        }

    }

    public class FruitBowelManagerPane extends AbstractFruitPane {

        private String[] fruits = {"Banana", "Strewberry", "Pear", "Peach", "Orange"};

        private JButton giver;
        private JButton taker;

        public FruitBowelManagerPane(FruitBowl fruitBowl) {
            super(fruitBowl);
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = 1;

            giver = new JButton("Add fruit");
            taker = new JButton("Remove fruit");
            taker.setEnabled(false);

            giver.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String fruit = fruits[(int)(fruits.length * Math.random())];
                    getFruitBowl().addFruit(fruit);
                    taker.setEnabled(true);
                }
            });
            taker.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    List<String> fruits = getFruitBowl().getFruit();
                    String eat = fruits.get((int)(fruits.size() * Math.random()));
                    getFruitBowl().removeFruit(eat);
                    if (getFruitBowl().getFruit().isEmpty()) {
                        taker.setEnabled(false);
                    }
                }
            });

            add(giver, gbc);
            add(taker, gbc);
        }

    }

    public class FruitBowelMonitorPane extends AbstractFruitPane {

        private JLabel label;

        public FruitBowelMonitorPane(FruitBowl fruitBowl) {
            super(fruitBowl);
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();

            label = new JLabel("I'm watching you...");
            add(label);

            fruitBowl.addFruitBowlListener(new FruitBowlListener() {
                @Override
                public void fruitAdded(FruitBowlEvent evt) {
                    label.setText("You added " + evt.getFruit());
                }

                @Override
                public void fruitRemoved(FruitBowlEvent evt) {
                    if (getFruitBowl().getFruit().isEmpty()) {
                        label.setText("You ate all the fruit!");
                    } else {
                        label.setText("You ate my " + evt.getFruit());
                    }
                }
            });

        }

    }

}

Updated with combined example

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EventListener;
import java.util.EventObject;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.EventListenerList;

public class TestModel {

    public static void main(String[] args) {
        new TestModel();
    }

    public TestModel() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                FruitBowl fb = new DefaultFruitBowl();

                JDesktopPane dp = new JDesktopPane() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(400, 200);
                    }
                };
                JInternalFrame manager = new JInternalFrame("Fruit Bowl Manager", true, true, true, true);
                manager.add(new FruitBowelManagerPane(fb));
                manager.setVisible(true);
                manager.setBounds(0, 0, 200, 200);

                JInternalFrame monitor = new JInternalFrame("Fruit Bowl Monitor", true, true, true, true);
                monitor.add(new FruitBowelMonitorPane(fb));
                monitor.setVisible(true);
                monitor.setBounds(200, 0, 200, 200);

                dp.add(manager);
                dp.add(monitor);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(dp);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public abstract class AbstractFruitPane extends JPanel {

        private FruitBowl fruitBowl;

        public AbstractFruitPane(FruitBowl fruitBowl) {
            this.fruitBowl = fruitBowl;
        }

        public FruitBowl getFruitBowl() {
            return fruitBowl;
        }

    }

    public class FruitBowelManagerPane extends AbstractFruitPane {

        private String[] fruits = {"Banana", "Strewberry", "Pear", "Peach", "Orange"};

        private JButton giver;
        private JButton taker;

        public FruitBowelManagerPane(FruitBowl fruitBowl) {
            super(fruitBowl);
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = 1;

            giver = new JButton("Add fruit");
            taker = new JButton("Remove fruit");
            taker.setEnabled(false);

            giver.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String fruit = fruits[(int) (fruits.length * Math.random())];
                    getFruitBowl().addFruit(fruit);
                    taker.setEnabled(true);
                }
            });
            taker.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    List<String> fruits = getFruitBowl().getFruit();
                    String eat = fruits.get((int) (fruits.size() * Math.random()));
                    getFruitBowl().removeFruit(eat);
                    if (getFruitBowl().getFruit().isEmpty()) {
                        taker.setEnabled(false);
                    }
                }
            });

            add(giver, gbc);
            add(taker, gbc);
        }

    }

    public class FruitBowelMonitorPane extends AbstractFruitPane {

        private JLabel label;

        public FruitBowelMonitorPane(FruitBowl fruitBowl) {
            super(fruitBowl);
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();

            label = new JLabel("I'm watching you...");
            add(label);

            fruitBowl.addFruitBowlListener(new FruitBowlListener() {
                @Override
                public void fruitAdded(FruitBowlEvent evt) {
                    label.setText("You added " + evt.getFruit());
                }

                @Override
                public void fruitRemoved(FruitBowlEvent evt) {
                    if (getFruitBowl().getFruit().isEmpty()) {
                        label.setText("You ate all the fruit!");
                    } else {
                        label.setText("You ate my " + evt.getFruit());
                    }
                }
            });

        }

    }

    public class FruitBowlEvent extends EventObject {

        private String fruit;

        public FruitBowlEvent(FruitBowl fruitBowl, String fruit) {
            super(fruitBowl);
            this.fruit = fruit;
        }

        public FruitBowl getFruitBowl() {
            return (FruitBowl) getSource();
        }

        public String getFruit() {
            return fruit;
        }

    }

    public interface FruitBowlListener extends EventListener {
        public void fruitAdded(FruitBowlEvent evt);
        public void fruitRemoved(FruitBowlEvent evt);
    }

    public interface FruitBowl {
        public void addFruit(String fruit);
        public void removeFruit(String fruit);
        public List<String> getFruit();
        public void addFruitBowlListener(FruitBowlListener listener);
        public void removeFruitBowlListener(FruitBowlListener listener);
    }

    public abstract class AbstractFruitBowl implements FruitBowl {

        private EventListenerList listenerList;

        public AbstractFruitBowl() {
        }

        protected EventListenerList getEventListenerList() {
            if (listenerList == null) {
                listenerList = new EventListenerList();
            }
            return listenerList;
        }

        @Override
        public void addFruitBowlListener(FruitBowlListener listener) {
            getEventListenerList().add(FruitBowlListener.class, listener);
        }

        @Override
        public void removeFruitBowlListener(FruitBowlListener listener) {
            getEventListenerList().remove(FruitBowlListener.class, listener);
        }

        protected void fireFruitAdded(String fruit) {
            FruitBowlListener[] listeners = getEventListenerList().getListeners(FruitBowlListener.class);
            if (listeners.length > 0) {
                FruitBowlEvent evt = new FruitBowlEvent(this, fruit);
                for (FruitBowlListener listener : listeners) {
                    listener.fruitAdded(evt);
                }
            }
        }

        protected void fireFruitRemoved(String fruit) {
            FruitBowlListener[] listeners = getEventListenerList().getListeners(FruitBowlListener.class);
            if (listeners.length > 0) {
                FruitBowlEvent evt = new FruitBowlEvent(this, fruit);
                for (FruitBowlListener listener : listeners) {
                    listener.fruitRemoved(evt);
                }
            }
        }
    }

    public class DefaultFruitBowl extends AbstractFruitBowl {

        private List<String> fruits;

        public DefaultFruitBowl() {
            fruits = new ArrayList<>(25);
        }

        @Override
        public void addFruit(String fruit) {
            fruits.add(fruit);
            fireFruitAdded(fruit);
        }

        @Override
        public void removeFruit(String fruit) {
            fruits.remove(fruit);
            fireFruitRemoved(fruit);
        }

        @Override
        public List<String> getFruit() {
            return Collections.unmodifiableList(fruits);
        }

    }
}


来源:https://stackoverflow.com/questions/25841715/how-to-pass-data-between-two-jinternalframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!