简体   繁体   中英

How and where should I add the ActionListener in my code?

I have written the below code which has a text-field and a button. as soon as a character is entered and the button is pressed a tab is created with the title same as what is entered in the field.

Several tabs can be create the same way.....now in the new tab again, a text-field and a button exist a long with a text pane to show the result....

I want to show the text entered to the text-field in its text-pane in each tab...

Now please lead me learn how and where I put the listener for the button of the tab.... and recommend any other required Listeners(I think there should be another Listener to direct me to the focused or selected tab).

It should be mentioned that I have added these tabs to an array-list for any reuse but I don't know if I did right or how I can use it?

package test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;

public class TestGUI extends JFrame {


    private JTextField jTextField1;
    private JButton jButton1;
    static ArrayList<JPanel> ary = new ArrayList<JPanel>();
    private int tabIndex = 0;
    static int index = 0;
    private JTabbedPane tabbedPane;

    /**
    * @param args
    */
    public TestGUI() {

        super("Testing Tab Frame");
        setLayout(null);

        Handler but1 = new Handler();

        jTextField1 = new JTextField();
        jTextField1.setVisible(true);
        jTextField1.setBounds(12, 12, 85, 30);
        add(jTextField1);

        jButton1 = new JButton("Button1");
        jButton1.setVisible(true);
        jButton1.setBounds(130, 12, 85, 30);
        add(jButton1);
        jButton1.addActionListener(but1);

        tabbedPane = new JTabbedPane();
        tabbedPane.setBounds(12, 54, 200, 150);
        tabbedPane.setVisible(false);
        add(tabbedPane);
        pack();
        setSize(250, 110);
        setLocationRelativeTo(null);

    }

    private class Handler implements ActionListener {

        public void actionPerformed(ActionEvent evt) {
            String input = jTextField1.getText();
            if (!input.isEmpty()) {
                setSize(250, 250);
                JPanel inst = createPanel(input);
                inst.setVisible(true);
                tabbedPane.addTab(Integer.toString(tabIndex), inst);
                tabbedPane.setVisible(true);
            }

        }
    }

    protected JPanel createPanel(String input) {
        JPanel inst = new JPanel();
        inst.setVisible(true);
        JTextField textField = new JTextField();
        textField.setVisible(true);
        textField.setBounds(12, 12, 80, 30);
        JButton button = new JButton();
        button.setVisible(true);
        button.setBounds(100, 12, 80, 30);
        JTextPane textPane = new JTextPane();
        textPane.setBounds(12, 54, 168, 40);
        inst.add(textPane);
        textPane.setVisible(true);
        inst.setLayout(null);
        inst.add(button);
        inst.add(textField);
        ary.add(inst);
        tabIndex = index;
        index++;
        return inst;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TestGUI inst = new TestGUI();
        inst.setVisible(true);
    }

}

You would add the ActionListener to the button inside your createPanel method. So your method would be something like this (making some assumptions about what you actually want to do with the text since it wasn't clear):

protected JPanel createPanel(String input) {
    JPanel inst = new JPanel();
    inst.setVisible(true);
    final JTextField textField = new JTextField();
    textField.setVisible(true);
    textField.setBounds(12, 12, 80, 30);
    JButton button = new JButton();        
    button.setVisible(true);
    button.setBounds(100, 12, 80, 30);
    final JTextPane textPane = new JTextPane();
    textPane.setBounds(12, 54, 168, 40);
    inst.add(textPane);
    textPane.setVisible(true);

    button.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent arg0) {
            textPane.setText(textPane.getText() + textField.getText());
        }});

    inst.setLayout(null);
    inst.add(button);
    inst.add(textField);
    ary.add(inst);
    tabIndex = index;
    index++;
    return inst;
}

The modification to TabComponentsDemo shown here shows one approach to renaming tabs. It listens to a JButton on each pane, but an ActionListener on a JTextField should serve, too.

Various points that you can use to make your User Interface more robust :

  • It's really not a good idea to start with Absolute Positioning . Please do read the first paragraph of the link to have more information on why this approach is discouraged over using LayoutManagers .
  • There is no need to explicitly use setVisible(true); on various JComponent s as you are doing in your code, since as you make the parent visible, all child components will be set to visible likewise.
  • Calls like pack()/setVisible(true/false) must be done on the EDT-Event Dispatch Thread, instead of calling them from the main method. For more info please read Concurrency in Swing .

Have a look at this modified code of yours, and Please do ask if you need more insight into this :

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;

public class TestGUI extends JFrame {

    private JPanel contentPane;
    private JTextField jTextField1;
    private JButton jButton1;
    static ArrayList<JPanel> ary = new ArrayList<JPanel>();
    private int tabIndex = 0;
    static int index = 0;
    private JTabbedPane tabbedPane;

    public TestGUI() {

        super("Testing Tab Frame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(5, 5));

        Handler but1 = new Handler();
        JPanel footerPanel = new JPanel();

        jTextField1 = new JTextField(10);
        footerPanel.add(jTextField1);

        jButton1 = new JButton("Create TAB");
        footerPanel.add(jButton1);
        jButton1.addActionListener(but1);

        tabbedPane = new JTabbedPane();

        contentPane.add(tabbedPane, BorderLayout.CENTER);
        contentPane.add(footerPanel, BorderLayout.PAGE_END);   

        setContentPane(contentPane);    
        setSize(300, 300);
        setLocationRelativeTo(null);
    }

    private class Handler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent evt) {
            String input = jTextField1.getText();
            if (!input.isEmpty()) {

                JPanel inst = createPanel();                
                tabbedPane.addTab(input, inst);
                ary.add(inst);              

                jTextField1.setText("");
                contentPane.revalidate();
                contentPane.repaint();              
            }
        }
    }

    protected JPanel createPanel() {

        JPanel inst = new JPanel();
        inst.setLayout(new BorderLayout(5, 5));

        final JTextPane textPane = new JTextPane();

        JPanel footerPanel = new JPanel();
        final JTextField textField = new JTextField(10);
        JButton button = new JButton("SHOW");
        button.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                if (textField.getDocument().getLength() > 0)
                    textPane.setText(textField.getText());
                textField.setText("");  
            }
        });
        footerPanel.add(textField);
        footerPanel.add(button);

        inst.add(textPane, BorderLayout.CENTER);
        inst.add(footerPanel, BorderLayout.PAGE_END);    

        return inst;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                TestGUI inst = new TestGUI();
                inst.setVisible(true);
            }
        });
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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