在comboBox上的操作侦听器以在textField中显示选择

凯文·舒尔茨

我有一个包含选择的comboBox。我正在尝试应用actionListener在textField上显示选定的字符串。我不确定如何完成此操作,我读过的所有内容进一步使我感到困惑。

这是comboBox的代码:

JComboBox comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"Select Something", "Selection 1", "Selection 2", "Selection 3", "Selection 4"}));
        panelTop.add(comboBox);
        comboBox.setPreferredSize(new Dimension(160, 20));
        comboBox.addItem("Select Something");

textField代码:

textField = new JTextField();
        panelTop.add(textField);
        textField.setColumns(40);

到目前为止,我对actionListener的了解:

public void actionPerformed(ActionEvent e) {
            JComboBox comboBox = (JComboBox)e.getSource();
            String newSelection = (String)comboBox.getSelectedItem();
            currentPattern = newSelection;

        }

完整代码:

package SwingAssign2;

import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import java.awt.BorderLayout;

import javax.swing.JComboBox;
import javax.swing.JTextField;

import java.awt.FlowLayout;

import javax.swing.SwingConstants;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.DefaultComboBoxModel;

public class SwingAssign2 {

    private JFrame frame;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SwingAssign2 window = new SwingAssign2();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public SwingAssign2() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setSize(600, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panelTop = new JPanel();
        FlowLayout fl_panelTop = (FlowLayout) panelTop.getLayout();
        fl_panelTop.setAlignment(FlowLayout.LEFT);
        frame.getContentPane().add(panelTop, BorderLayout.NORTH);
        //panel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

        final JComboBox comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"Select Something", "Selection 1", "Selection 2", "Selection 3", "Selection 4"}));
        panelTop.add(comboBox);
        comboBox.setPreferredSize(new Dimension(160, 20));
        comboBox.addItem("Select Something");


        textField = new JTextField();
        panelTop.add(textField);
        textField.setColumns(40);

        JPanel panelBottom = new JPanel();
        frame.getContentPane().add(panelBottom, BorderLayout.SOUTH);
        panelBottom.setLayout(new BorderLayout(0, 0));

        JLabel status = new JLabel("Status");
        status.setHorizontalAlignment(SwingConstants.CENTER);
        panelBottom.add(status, BorderLayout.SOUTH);
        status.setBorder(BorderFactory.createLoweredBevelBorder());

        JTextPane txtpnSomeContentOn_B = new JTextPane();
        txtpnSomeContentOn_B.setText("Some content on the bottom panel\r\nMore content\r\nSome more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content");
        JScrollPane jScrollPane3 = new JScrollPane(txtpnSomeContentOn_B);
        jScrollPane3.setHorizontalScrollBarPolicy(
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane3.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        panelBottom.add(jScrollPane3, BorderLayout.NORTH);
        jScrollPane3.setPreferredSize(new Dimension(200, 200));
        //bText.setBorder(BorderFactory.createLoweredBevelBorder());

        JPanel panelCenter = new JPanel();
        frame.getContentPane().add(panelCenter, BorderLayout.CENTER);
        panelCenter.setLayout(new GridLayout(0, 2, 0, 0));

        JTextPane txtpnSomeContentOn_0 = new JTextPane();
        txtpnSomeContentOn_0.setText("Some content on the left panel\r\nMore content\r\nSome more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content");
        JScrollPane jScrollPane1 = new JScrollPane(txtpnSomeContentOn_0);
        jScrollPane1.setHorizontalScrollBarPolicy(
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        panelCenter.add(jScrollPane1);

        JTextPane txtpnSomeContentOn = new JTextPane();
        txtpnSomeContentOn.setText("Some content on the right panel\r\nMore content\r\nSome more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content\r\nMore and more content");
        JScrollPane jScrollPane2 = new JScrollPane(txtpnSomeContentOn);
        jScrollPane2.setHorizontalScrollBarPolicy(
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane2.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        panelCenter.add(jScrollPane2);

        comboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange() == ItemEvent.SELECTED) {
                    textField.setText((String) comboBox.getSelectedItem());
                }
            }
        });


    }

}
大卫·叶(David Yee)

考虑向添加ItemListenerJComboBox并检查其ItemEvent状态是否为ItemEvent.SELECTED您可以在itemStateChanged方法中设置文本字段的文本

comboBox.addItemListener(new ItemListener() {
    @Override
    public void itemStateChanged(ItemEvent e) {
        if(e.getStateChange() == ItemEvent.SELECTED) {
            textField.setText((String) comboBox.getSelectedItem());
        }
    }
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

删除表项上的选择侦听器

在 LibGdx 中添加 TextField 及其侦听器

recyclerview 上的 Onclick 侦听器以显示图像

选择项中的JList动作侦听器“延迟”

JavaFX的TextField的值更改侦听器

Java8 Stream 在并行流中的终端操作上的自定义 SWT 侦听器中失败

选择广播字段显示特定面板时,如何在Extjs中为广播组编写单个侦听器?

Embers js中的更改侦听器上的组合框

Flutter中的LongAction侦听器上的FloatingActionButton

在addEventListener中粘贴参数的元素上删除侦听器

PHP中的侦听器

从侦听器列表中删除侦听器

动作侦听器中的动作侦听器错误

NullPointerException与JButton操作侦听器

微调器上的侦听器

JComboBox选择更改侦听器?

基本textField actionListener发生错误(命令在侦听器中时发生错误)

__invoke在zend框架2中的控制器操作后调用的侦听器类中

UL 上的侦听器遍历 li

Tomcat上的文件侦听器进程

触摸侦听器上的文本视图

Nuxt上的Firebase身份侦听器

画布在事件侦听器上消失

iOS 14上的UDP侦听器

在HTML中,如何构建文件选择器,而无需使用动作侦听器就可以更改显示的图像?

JSF中的最佳实践:模型,操作,获取器,导航,阶段侦听器

单击侦听器上的单选按钮仅在选择切换按钮后才能工作

为什么侦听器总是显示上一次迭代的编号?

NMAP看到开放的端口,但是netstat不在它们上显示侦听器