追加されているタブの総数を取得する
広告
JTabbedPaneクラスのオブジェクトに追加されているタブの総数を取得する方法を確認します。JTabbedPaneクラスで用意されている「getTabCount」メソッドを使います。
getTabCount public int getTabCount()
この tabbedpane のタブの数を返します。 戻り値: タブページの数を指定する int 値
戻り値として現在追加されているタブの総数をint型の値として取得します。
実際の使い方は次のようになります。
JTabbedPane tabbedpane = new JTabbedPane();
tabbedpane.addTab("title1", new JButton("button1"));
int count = tabbedpane.getTabCount();
サンプルプログラム
では簡単なサンプルを作成して試してみます。
import javax.swing.*;
import java.awt.BorderLayout;
public class JTabbedPaneTest13 extends JFrame{
public static void main(String[] args){
JTabbedPaneTest13 frame = new JTabbedPaneTest13();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(10, 10, 300, 200);
frame.setTitle("タイトル");
frame.setVisible(true);
}
JTabbedPaneTest13(){
JTabbedPane tabbedpane = new JTabbedPane();
JPanel tabPanel1 = new JPanel();
tabPanel1.add(new JButton("button1"));
JPanel tabPanel2 = new JPanel();
tabPanel2.add(new JLabel("Name:"));
tabPanel2.add(new JTextField("", 10));
JPanel tabPanel3 = new JPanel();
tabPanel3.add(new JButton("button2"));
tabbedpane.addTab("tab1", tabPanel1);
tabbedpane.addTab("tab2", tabPanel2);
tabbedpane.addTab("tab3", tabPanel3);
JLabel countLabel = new JLabel();
countLabel.setText("Tab Count : " + tabbedpane.getTabCount());
JPanel labelPanel = new JPanel();
labelPanel.add(countLabel);
getContentPane().add(tabbedpane, BorderLayout.CENTER);
getContentPane().add(labelPanel, BorderLayout.PAGE_END);
}
}
上記をコンパイルした後で実行すると次のように表示されます。
画面下部に現在追加されているタブの総数を表示しています。
( Written by Tatsuo Ikura )
著者 / TATSUO IKURA
これから IT 関連の知識を学ばれる方を対象に、色々な言語でのプログラミング方法や関連する技術、開発環境構築などに関する解説サイトを運営しています。