简体   繁体   English

在Java Swing中,当另一个JPanel中的ComboBox更改时,将触发JPanel中的更新

[英]In Java Swing trigger an update in a JPanel when a ComboBox changes in another JPanel

I want to update a JPanel according to the value of a ComboBox in another JPanel. 我想根据另一个JPanel中的ComboBox的值更新JPanel。

I have a JFrame in which I am adding two panels: 我有一个JFrame,其中要添加两个面板:

package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class MainFrame extends JFrame {
    public MainFrame() {
    }

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainFrame frame = new MainFrame();

                    frame.setSize(1200, 1200);

                    PanelClass1 panelClass1= new PanelClass1 ();
                    PanelClass2 panelClass2= new PanelClass2 ();


                    frame.getContentPane().add(panelClass1.contentPanel, BorderLayout.PAGE_START);
                    frame.getContentPane().add(panelClass2.contentPanel, BorderLayout.PAGE_END);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

Now in Panel1 I have a ComboBox in which depending on its value I need to hide or show fields in Panel2. 现在在Panel1中,我有一个ComboBox,其中需要根据其值隐藏或显示Panel2中的字段。 I am saving the value of this ComboBox in a singleton. 我将这个ComboBox的值保存为单例。

public class PanelClass1 extends JPanel {

testComboBox = new JComboBox<ComboItem>();
                testComboBox.setEditable(true);
                testComboBox.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                        POJOSingleton.getInstance().setValor(testComboBox.getSelectedItem().toString());
                        checkFields();
                    }
                });
 }

Then in PanelClass2 I am reading that variable and then making some components visible or not according to the value. 然后在PanelClass2中,我正在读取该变量,然后根据该值使某些组件可见或不可见。 The whole logic of the panelClass2 is based on the value I set in the first JPanel. panelClass2的整个逻辑基于我在第一个JPanel中设置的值。

public class panelClass2 extends JPanel {

  public panelClass2 () {

    if(POJOSingleton.getInstance().getValor() != null) {
        int value1 = posicionesTenion.get(0);

        pos1.setVisible(true);
        lblPos1TensionAT.setVisible(true);
        ...

What should I do so when I change the ComboBox I trigger a refresh in my second panel?. 更改ComboBox时,我应该在第二个面板中触发刷新吗?

The best way is probably to use an Observer pattern . 最好的方法可能是使用观察者模式

The way to do that is to add an Observable field to your PanelClass1 , and let it notify all Observers when the ActionListener of the JComboBox gets triggered. 这样做的方法是将一个Observable字段添加到PanelClass1中 ,并在触发JComboBox的ActionListener时通知所有Observer。

public class PanelClass1 extends JPanel {

    private JComboBox<?> testComboBox;
    private Observable obs;

    public PanelClass1() {

        obs = new Observable();

        testComboBox = new JComboBox<SingletonClass>();
        testComboBox.setEditable(true);
        testComboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                SingletonClass.getInstance().setValue(testComboBox.getSelectedIndex());
                obs.notifyObservers();
            }
        });
    }

    public void addObserver(Observer observer) {
        obs.addObserver(observer);
    }

    public void removeObserver(Observer observer) {
        obs.deleteObserver(observer);
    }
}

Now let your PanelClass2 implement Observer and let your code execute when it gets notified, like so: 现在,让您的PanelClass2实现Observer,并在收到通知时执行代码,如下所示:

public class PanelClass2 extends JPanel implements Observer {

    public PanelClass2() {

    }

    @Override
    public void update(Observable o, Object arg) {
        int value = SingletonClass.getInstance().getValue();
        // do your stuff here
    }
}

Saying your singleton object looks somewhat like this: 说您的单例对象看起来像这样:

public class SingletonClass {

    private static final SingletonClass singleton = new SingletonClass();
    private int value; // this doesn't have to be an integer, I am just using this as an example

    private SingletonClass() {

    }

    public static SingletonClass getInstance() {
        return singleton;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

}

you now are able to access the value field of your SingletonClass instance in the update() method of PanelClass2. 您现在可以在PanelClass2的update()方法中访问SingletonClass实例的value字段。

Your MainFrame class needs only one change: You need to add your PanelClass2 object to your PanelClass1 object as an observer. 大型机级只需要一个变化:您需要在PanelClass2对象添加到您的PanelClass1对象作为一个观察者。

public class MainFrame extends JFrame {

    public MainFrame() {

    }

    /**
     * Launch the application.
     */
     public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainFrame frame = new MainFrame();

                    frame.setSize(1200, 1200);

                    PanelClass1 panelClass1 = new PanelClass1();
                    PanelClass2 panelClass2 = new PanelClass2();

                    panelClass1.addObserver(panelClass2);

                    frame.getContentPane().add(panelClass1,BorderLayout.PAGE_START);
                    frame.getContentPane().add(panelClass2, BorderLayout.PAGE_END);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

I hope this helped you solve your problem. 我希望这可以帮助您解决问题。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM