组合框上的Action侦听器在textField中显示选择 [英] Action listener on comboBox to display selection in textField

查看:162
本文介绍了组合框上的Action侦听器在textField中显示选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含选择的组合框。我试图应用一个actionListener显示在textField上选择的字符串。

I have a comboBox that contains selections. I am trying to apply an actionListener to display te selected string on a textField. I am not sure how to complete this and all I have read is confusing me even more.

以下是comboBox的代码:

Here is the code for the 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 Code:

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

到目前为止,我已经在actionListener上了:

What I have so far on the 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());
                }
            }
        });


    }

}


推荐答案

考虑向 JComboBox 添加 ItemListener c $ c> ItemEvent state是 ItemEvent.SELECTED 。您可以在 itemStateChanged 方法中设置文本字段的文本:

Consider adding an ItemListener to the JComboBox and checking that the ItemEvent state is ItemEvent.SELECTED. You can set the text field's text in the itemStateChanged method:

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

这篇关于组合框上的Action侦听器在textField中显示选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆