在JTabbedPane中的选项卡之间切换组件

时间:2013-02-07 17:25:40

标签: java jtabbedpane

有没有办法在JTabbedPane中的标签之间切换组件而不创建这些对象的新实例?或者以任何方式切换标签?

当通过JTabbedPane.remove(idx)方法从.removeTabAt(idx)移除组件时,组件将被销毁。也许有办法防止破坏物体?

我正在寻找一种方法来删除包含该组件的标签,以便在最近的将来将其添加回去,但是使用另一个索引。

换句话说,我只需要更改Tab键顺序。但对我来说,不要创建新的组件实例非常重要。

3 个答案:

答案 0 :(得分:3)

JTabbedPane.removeTabAt只会删除标签,但不会删除您在标签中放置的组件。因此,要移动特定组件的选项卡,只需在JTabbedPane.insertTab的新位置插入组件的新选项卡,这将隐式删除旧选项卡。或者您可以自己删除它,稍后再添加它。两种方法都可以正常工作:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

public class ReorderTabs {
    private static void addTab(final JTabbedPane tabbedPane, final String title) {
        final JPanel panel = new JPanel(new BorderLayout());

        JLabel content = new JLabel(title + " - content", JLabel.CENTER); 
        panel.add(content, BorderLayout.CENTER);

        JButton toFrontButton = new JButton("|<");
        toFrontButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                // Re-insert the component in a new tab at the front. The tabbed pane will remove the old tab.
                tabbedPane.insertTab(title, null, panel, null, 0);
            }
        });
        panel.add(toFrontButton, BorderLayout.WEST);

        JButton toBackButton = new JButton(">|");
        toBackButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // Remove the component from the tab. The component will not be destroyed ...
                int index = tabbedPane.indexOfComponent(panel);
                tabbedPane.removeTabAt(index);

                // ... and can be added again (or inserted at an arbitrary index with insertTab).
                tabbedPane.addTab(title, panel);
            }
        });
        panel.add(toBackButton, BorderLayout.EAST);

        tabbedPane.addTab(title, panel);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JTabbedPane tabbedPane = new JTabbedPane();
                addTab(tabbedPane, "Tab #1");
                addTab(tabbedPane, "Tab #2");
                addTab(tabbedPane, "Tab #3");
                addTab(tabbedPane, "Tab #4");

                JFrame frame = new JFrame("Reorder Tabs Demo");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(tabbedPane, BorderLayout.CENTER);
                frame.setSize(400, 200);
                frame.setVisible(true);
            }
        });
    }
}

答案 1 :(得分:0)

如果您不想删除它们,可以隐藏它。

或者您可以使用jtpan.setEnabledAt(int tab_index, boolean enabled)切换标签。

设置索引使用setSelectedIndex(int index)

隐藏组件使用componen.setVisible(false)

答案 2 :(得分:0)

这个肮脏的黑客呢?

public static void main (String [] args) throws Exception
{
    final JTabbedPane tabbedPane = new JTabbedPane ();
    tabbedPane.addTab ("A", new JButton ("A"));
    tabbedPane.addTab ("B", new JButton ("B"));
    tabbedPane.addTab ("C", new JButton ("C"));
    tabbedPane.addTab ("D", new JButton ("D"));
    JFrame frame = new JFrame ();
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    frame.getContentPane ().setLayout (new BorderLayout ());
    frame.getContentPane ().add (tabbedPane, BorderLayout.CENTER);
    frame.pack ();
    frame.setVisible (true);

    Field pagesField = JTabbedPane.class.getDeclaredField ("pages");
    pagesField.setAccessible (true);
    final List <Object> pages = (List <Object>)pagesField.get (tabbedPane);

    while (true)
    {
        Thread.sleep (1000);

        SwingUtilities.invokeLater (new Runnable()
        {
            @Override
            public void run ()
            {
                Object o = pages.get (0);

                for (int i = 1; i < pages.size (); i++)
                    pages.set (i - 1, pages.get (i));

                pages.set (pages.size () - 1, o);

                tabbedPane.setSelectedIndex ((tabbedPane.getSelectedIndex () + pages.size () - 1) % pages.size ());

                tabbedPane.invalidate ();
                tabbedPane.repaint ();
            }
        });
    }
}