可以动态添加swing控件动作侦听器吗?

时间:2012-07-21 07:43:12

标签: java swing dynamic

我在运行时创建了JTextFields数组。这是基于用户输入。因此,为每个文本字段分配单独的动作侦听器。

我如何为TextField提供actionlisterner?

代码:

public class BucketInfoDynamic extends javax.swing.JFrame {

    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    int value;
    JPanel[] p2 ;
    JTextField[] txtFrom;
    JTextField[] txtTo;
    JLabel[] lblBucket;
    JLabel lblEnd;

    public BucketInfoDynamic() {
        for(;;)
        {
            try{
                String number = JOptionPane.showInputDialog("How many Bucket do you want?");
                value = Integer.parseInt(number);
                if(value <= 5 && value > 1)
                    break;
                else
                    JOptionPane.showMessageDialog(this, "Enter bucket value should greater than 1 and less than 5.\n"); 
            }
            catch(NumberFormatException ex)
             {
                   JOptionPane.showMessageDialog(this, "This is Not a valid number format..\n"); 
             }
        }
        initComponents();
        jPanel1.setLayout(new GridLayout(value,1,20,10));
        p2 = new JPanel[value];
        txtFrom = new JTextField[value];
        txtTo = new JTextField[value -1];
        lblBucket = new JLabel[value];
        lblEnd = new JLabel("And Above");
        this.designProcess();

    }

 private void designProcess() {
        jPanel1.setSize(value * 50 ,jPanel1.getWidth());
        for(int i = 0; i < value; i++)
        {
            lblBucket[i] = new JLabel("Bucket "+i);
            p2[i] = new JPanel();
            p2[i].setLayout(new GridLayout(1,3,20,10));
            txtFrom[i] = new JTextField();
            if(i == 0)
                txtFrom[i].setText("0");
            txtFrom[i].setEditable(false);
            p2[i].add(lblBucket[i]);
            p2[i].add(txtFrom[i]);
            if(i < value-1)
            {
                txtTo[i] = new JTextField();
                txtTo[i].setFocusTraversalKeysEnabled(false);
                txtTo[i].addKeyListener(new java.awt.event.KeyAdapter() {
                    public void keyReleased(java.awt.event.KeyEvent evt) {
                        toKeyReleased(evt);
                } 
            });
                p2[i].add(txtTo[i]);
            }
            else
                p2[i].add(this.lblEnd);

            jPanel1.add(p2[i]);

        }
    }

     private void toKeyReleased(KeyEvent evt) {
        // this.checkFormat(evt);
         Component com = evt.getComponent();
         System.out.println(com.getName());

        if(KeyEvent.VK_TAB == evt.getKeyCode())
            this.checkRange();
     }
}

1 个答案:

答案 0 :(得分:1)

这样做的一种方法是:

jTextFieldArray[index].addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // code what you want this field to do
            }
        });

其他方面是: 将一个ActionListener注册到所有JTextField并使其搜索触发事件的那个(提示:ActionEvent.getSource()为您提供触发事件的组件。)

像这样:

public class Jtext {
JTextField[] txt;
    public Jtext() {
        txt = new JTextField[100];
        int i = 0;
        txt[i] = new JTextField();
        Listener l = new Listener();
        txt[i].addActionListener(l);

    }    
    class Listener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            JTextField txt = (JTextField) e.getSource();
            if(txt.equals(YourJtextField){

            }
        }

    }
}

对于有限数量的文本域解决方案可能是这样的:

public class Jtext {
JTextField[] txt;
public static String ACTION_CMD_1 = "txt1";
public static String ACTION_CMD_2 = "txt2";
    public Jtext() {
        txt = new JTextField[5];
        int i = 0;
        txt[i] = new JTextField();
        Listener l = new Listener();
        txt[i].setActionCommand("txt" + counter);

    }    
    class Listener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            String actCommand = e.getActionCommand();
            if(actCommand.equals(ACTION_CMD_1)){
                 // code
            }

            if(actCommand.equals(ACTION_CMD_2)){
                 //code
            }
        }

    }