3

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);
    }

}
kleopatra
  • 51,061
  • 28
  • 99
  • 211
msc87
  • 943
  • 3
  • 17
  • 39
  • 4
    Please restrict your posts to one specific question. – Keppil Aug 08 '12 at 23:30
  • @ MadProgramer & Keppil: I removed the question about architecture...please only think of Listeners.....;) – msc87 Aug 08 '12 at 23:42
  • @msc87 What do you mean by "I want to show the text entered to the text-field in its text-pane in each tab..." - Which text field?? The main one on the frame or the one in the tab? – MadProgrammer Aug 08 '12 at 23:44
  • the one in the tab....I mean that after the new tabs are created in my frame, I enter something in its new text-field and press the button in the tab and I want to show it in the text-pane in the tab...the listener is needed for the new button...(take this into consideration that I want to have several same tabs working independently)... – msc87 Aug 08 '12 at 23:49
  • 6
    I've updated your code to be compilable as an [sscce](http://sscce.org/). – trashgod Aug 09 '12 at 00:27
  • +1 for a 99% [sscce](http://sscce.org/); it shows that you're missing the notion of a custom (editable) tab component. – trashgod Aug 09 '12 at 00:52

3 Answers3

6

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;
}
Nick Rippe
  • 6,465
  • 14
  • 30
4

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I read it but I don't know how to use it in my code yet...Sorry, but I am a beginner in JAVA... – msc87 Aug 09 '12 at 00:44
  • I understand. Modifying the [example](http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html#eg) was how I learned about custom components. I'd hoped you might find my experiment useful. – trashgod Aug 09 '12 at 00:50
3

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 JComponents 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);
            }
        });
    }

}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143