JTabbedPane
Nimbus のルック アンド フィールを使用してアプリケーションを作成していました
このコードを使用してタブを配置しました:
pane.addTab("Welcome",new ImageIcon("resources\\1.png"),mainPanel,"Takes to the welcome page");
アイコンを左側に表示したいのですが、
JTabbedPane
Nimbus のルック アンド フィールを使用してアプリケーションを作成していました
このコードを使用してタブを配置しました:
pane.addTab("Welcome",new ImageIcon("resources\\1.png"),mainPanel,"Takes to the welcome page");
アイコンを左側に表示したいのですが、
JTabbedPane.setTabComponentAt(int index, Component component)メソッドを使用して、タブ タイトルをレンダリングするためのカスタム コンポーネントを設定できます。
指定されたタブのタイトルのレンダリングを担当するコンポーネントを設定します。null 値は
JTabbedPane
、指定されたタブのタイトルやアイコンをレンダリングすることを意味します。null 以外の値は、コンポーネントがタイトルをJTabbedPane
レンダリングし、タイトルやアイコンをレンダリングしないことを意味します。注: コンポーネントは、開発者が既にタブ付きペインに追加したものであってはなりません。
たとえば、これを行うことができます:
JLabel label = new JLabel("Tab1");
label.setHorizontalTextPosition(JLabel.TRAILING); // Set the text position regarding its icon
label.setIcon(UIManager.getIcon("OptionPane.informationIcon"));
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
tabbedPane.addTab(null, new JPanel());
tabbedPane.setTabComponentAt(0, label); // Here set the custom tab component
スクリーンショット 1:
注:この機能を使用すると、必要に応じて設定できComponent
ます。たとえば、 aJPanel
で aを作成しJButton
てタブを閉じることができます。
final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
for(int i = 0; i < tabbedPane.getTabCount(); i++) {
if(SwingUtilities.isDescendingFrom(button, tabbedPane.getTabComponentAt(i))) {
tabbedPane.remove(i);
break;
}
}
}
};
JLabel label = new JLabel("Tab1", UIManager.getIcon("OptionPane.informationIcon"), JLabel.RIGHT);
JButton closeButton = new JButton("X");
closeButton.addActionListener(actionListener);
JPanel tabComponent = new JPanel(new BorderLayout());
tabComponent.add(label, BorderLayout.WEST);
tabComponent.add(closeButton, BorderLayout.EAST);
tabbedPane.addTab(null, new JPanel());
tabbedPane.setTabComponentAt(0, tabComponent); // Here set the custom tab component
スクリーンショット 2:
このトピックもご覧になることをお勧めします: JTabbedPane: タブの配置が LEFT に設定されていますが、アイコンが整列されていません