Ti posto un esempio che sicuramente è meglio di mille parole....
codice:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class TabbedTest extends JFrame
{
private JTextField inputTextField;
private JButton sendButton;
private JButton addButton;
private JTabbedPane tabbedPane;
public TabbedTest()
{
super("Test");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.inputTextField = new JTextField();
this.inputTextField.setColumns(20);
this.sendButton = new JButton("INVIA");
this.sendButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sendTextToTab();
}
});
this.addButton = new JButton("AGGIUNGI TAB");
this.addButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
addNewTab();
}
});
tabbedPane = new JTabbedPane();
tabbedPane.addTab("Tab 1", new JTextArea());
this.setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
topPanel.add(inputTextField);
topPanel.add(sendButton);
topPanel.add(addButton);
this.add(topPanel, BorderLayout.NORTH);
this.add(tabbedPane);
this.setPreferredSize(new Dimension(500, 500));
this.pack();
}
protected void addNewTab()
{
this.tabbedPane.addTab("TAB " + (this.tabbedPane.getTabCount() + 1), new JTextArea());
}
protected void sendTextToTab()
{
JTextArea textArea = (JTextArea) this.tabbedPane.getSelectedComponent();
textArea.append(this.inputTextField.getText());
this.inputTextField.setText("");
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
TabbedTest test = new TabbedTest();
test.setVisible(true);
}
});
}
}