3

一部のタブを非表示に設定するにはどうすればよいJTabbedPaneですか? を使用してみJTabbedPane#getTabComponentAt(index).setVisible(false);ましたが、NullPointerException. タブを無効にすることはできますが、非表示にすることはできません。

SSCCE:

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;

public class Main {
    public static void main(String[] args) {
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.setPreferredSize(new Dimension(400, 100));
        for (int i = 0; i < 7; i++)
            tabbedPane.add("tab " + i, new JLabel("content " + i));

        // this throws a NullPointerException
        tabbedPane.getTabComponentAt(1).setVisible(false);
        // this works
        tabbedPane.setEnabledAt(1, false);

        JFrame frame = new JFrame();
        frame.setContentPane(tabbedPane);
        frame.pack();
        frame.setVisible(true);
    }
}

私は自分が間違っていることを見つけることができません。

4

1 に答える 1

5

See the javadoc of the corresponding setter:

Sets the component that is responsible for rendering the title for the specified tab. A null value means JTabbedPane will render the title and/or icon for the specified tab. A non-null value means the component will render the title and JTabbedPane will not render the title and/or icon.

So the JTabbedPane#getTabComponentAt(index) method returns the Component used to render the tab if you set any, otherwise it uses a label and/or an icon.

Not sure whether you can make a tab invisible, but sure as hell you can remove them and insert them. That might be an acceptable solution

于 2012-08-29T15:58:27.940 に答える