Код: Выделить всё
// javax.swing.JTabbedPane#insertTab
public void insertTab(String title, Icon icon, Component component, String tip, int index) {
int newIndex = index;
// If component already exists, remove corresponding
// tab so that new tab gets added correctly
// Note: we are allowing component=null because of compatibility,
// but we really should throw an exception because much of the
// rest of the JTabbedPane implementation isn't designed to deal
// with null components for tabs.
int removeIndex = indexOfComponent(component);
if (component != null && removeIndex != -1) {
removeTabAt(removeIndex);
if (newIndex > removeIndex) {
newIndex--;
}
}
Код: Выделить всё
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;
import javax.swing.WindowConstants;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
public class TabbedPaneDemo {
private static JTabbedPane tabbedPane;
public static void main(String[] args) {
Container mainPanel = createMainPanel();
JFrame frame = new JFrame("Tabbed Pane demo");
frame.setContentPane(mainPanel);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static JComponent createMainPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.add(createTabbedPane());
return panel;
}
private static JTabbedPane createTabbedPane() {
tabbedPane = new JTabbedPane();
tabbedPane.addTab("Green tab", createGreenPanel());
tabbedPane.addTab("Red tab", createRedPanel());
tabbedPane.setPreferredSize(new Dimension(250, 150));
tabbedPane.setComponentPopupMenu(createPopupMenu());
return tabbedPane;
}
private static Component createGreenPanel() {
JPanel panel = new JPanel();
panel.setName("Green panel");
panel.setBackground(Color.GREEN);
return panel;
}
private static Component createRedPanel() {
JPanel panel = new JPanel();
panel.setName("Red panel");
panel.setBackground(Color.RED);
return panel;
}
private static JPopupMenu createPopupMenu() {
JPopupMenu popupMenu = new JPopupMenu();
popupMenu.add(createDuplicateTabItem());
return popupMenu;
}
private static JMenuItem createDuplicateTabItem() {
JMenuItem menuItem = new JMenuItem("Duplicate"); // doesn't work
menuItem.addActionListener(TabbedPaneDemo::duplicate);
return menuItem;
}
private static void duplicate(ActionEvent event) {
Component selectedComponent = tabbedPane.getSelectedComponent();
String selectedComponentTitle = tabbedPane.getTitleAt(tabbedPane.indexOfComponent(selectedComponent));
tabbedPane.addTab(selectedComponentTitle, selectedComponent);
}
}
< /code>
Я мог бы сделать что-то подобное: < /p>
private static Component createGreenPanel() {
JPanel panel = new JPanel();
panel.setName("Green panel");
panel.setBackground(Color.GREEN);
panel.putClientProperty("creator", (Supplier) TabbedPaneDemo::createGreenPanel);
return panel;
}
private static Component createRedPanel() {
JPanel panel = new JPanel();
panel.setName("Red panel");
panel.setBackground(Color.RED);
panel.putClientProperty("creator", (Supplier) TabbedPaneDemo::createRedPanel);
return panel;
}
< /code>
@SuppressWarnings("unchecked")
private static void duplicate(ActionEvent event) {
JComponent selectedComponent = (JComponent) tabbedPane.getSelectedComponent();
String selectedComponentTitle = tabbedPane.getTitleAt(tabbedPane.indexOfComponent(selectedComponent));
Supplier selectedComponentCreator = (Supplier) selectedComponent.getClientProperty("creator");
if (selectedComponentCreator == null) return;
Component selectedComponentDuplicate = selectedComponentCreator.get();
tabbedPane.addTab(selectedComponentTitle, selectedComponentDuplicate);
tabbedPane.setSelectedComponent(selectedComponentDuplicate);
}
Java 8.
Подробнее здесь: https://stackoverflow.com/questions/797 ... edpane-tab