Ayuda con JTabbePane
Estoy haciendo una aplicación que tiene una barra de menu, un JTabbedPane con una pestaña.
Una de las opciones del menú te pide un nombre y a partir de este nombre se crea una nueva pestaña en el JTabbedPane. Cómo puede añadir esta nueva pestaña en tiempo de ejecución??
con el metodo addTab() no me lo inserta y tampoco con el insertTab();
Una de las opciones del menú te pide un nombre y a partir de este nombre se crea una nueva pestaña en el JTabbedPane. Cómo puede añadir esta nueva pestaña en tiempo de ejecución??
con el metodo addTab() no me lo inserta y tampoco con el insertTab();
He conseguido hacer un programa que hace más o menos lo que pretendes hacer. Bueno el programa crea inicialmente una barra de menú con un menú que a su vez tiene tres items. Además también crea un JTabbedPane. Cuando pulsas cualquiera de los items del menú, se crea una ventana con el nombre del ítem. Míralo y si sigues sin encontrar el error, mejor sería que pusieras tu código que suele ayudar.
import javax.swing.*;
import java.awt.event.*;
public class Pestaña extends JFrame implements ActionListener {
JMenuBar barraMenu;
JMenu menu;
JMenuItem item1, item2, item3;
public Pestaña() {
super("Pestaña");
this.setContentPane(new JTabbedPane());
this.menu = new JMenu("Menú");
this.item1 = new JMenuItem("Mi pestaña");
this.item2 = new JMenuItem("Otra pestaña");
this.item3 = new JMenuItem("Y otra más");
this.menu.add(this.item1);
this.menu.add(this.item2);
this.menu.add(this.item3);
this.item1.addActionListener(this);
this.item2.addActionListener(this);
this.item3.addActionListener(this);
this.barraMenu = new JMenuBar();
this.barraMenu.add(this.menu);
JPanel panel = new JPanel();
this.setJMenuBar(this.barraMenu);
((JTabbedPane)this.getContentPane()).addTab("Hola", panel);
this.setSize(500,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new Pestaña();
}
public void actionPerformed(ActionEvent e) {
((JTabbedPane)Pestaña.this.getContentPane()).addTab(((JMenuItem)e.getSource()).getText(), new JPanel());
}
}
Bueno si tienes más dudas pues preguntas. Que te vaya bien y hasta luego
import javax.swing.*;
import java.awt.event.*;
public class Pestaña extends JFrame implements ActionListener {
JMenuBar barraMenu;
JMenu menu;
JMenuItem item1, item2, item3;
public Pestaña() {
super("Pestaña");
this.setContentPane(new JTabbedPane());
this.menu = new JMenu("Menú");
this.item1 = new JMenuItem("Mi pestaña");
this.item2 = new JMenuItem("Otra pestaña");
this.item3 = new JMenuItem("Y otra más");
this.menu.add(this.item1);
this.menu.add(this.item2);
this.menu.add(this.item3);
this.item1.addActionListener(this);
this.item2.addActionListener(this);
this.item3.addActionListener(this);
this.barraMenu = new JMenuBar();
this.barraMenu.add(this.menu);
JPanel panel = new JPanel();
this.setJMenuBar(this.barraMenu);
((JTabbedPane)this.getContentPane()).addTab("Hola", panel);
this.setSize(500,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new Pestaña();
}
public void actionPerformed(ActionEvent e) {
((JTabbedPane)Pestaña.this.getContentPane()).addTab(((JMenuItem)e.getSource()).getText(), new JPanel());
}
}
Bueno si tienes más dudas pues preguntas. Que te vaya bien y hasta luego
