// 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--;
}
}
So how do you implement the "duplicate" feature (similar to the one Chrome has)?
But if the duplicated tab contains some state that I need to duplicate as well (for example, some form input or selections), it won't work as expected.
Java 8.
Swing не позволяет дублировать компоненты вкладки.[code]// 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--; } } [/code] So how do you implement the "duplicate" feature (similar to the one Chrome has)? [code]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;
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); } [/code] But if the duplicated tab contains some state that I need to duplicate as well (for example, some form input or selections), it won't work as expected. Java 8.
Swing не позволяет дублировать компоненты вкладки. // javax.swing.JTabbedPane#insertTab
public void insertTab(String title, Icon icon, Component component, String tip, int index) {
int newIndex = index;
Swing не позволяет дублировать компоненты вкладки. // javax.swing.JTabbedPane#insertTab
public void insertTab(String title, Icon icon, Component component, String tip, int index) {
int newIndex = index;
Swing не позволяет дублировать компоненты вкладки. // javax.swing.JTabbedPane#insertTab
public void insertTab(String title, Icon icon, Component component, String tip, int index) {
int newIndex = index;
Swing не позволяет дублировать компоненты вкладки. // javax.swing.JTabbedPane#insertTab
public void insertTab(String title, Icon icon, Component component, String tip, int index) {
int newIndex = index;