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