コンボボックスのテスト ( jdk1.6.0_07 )

  LboxCombo extends JComboBox



最初に作ったクラスは、VB.NET 用です。
次に AIR & Flex3 用を作成して、Java 用は最後に作成しています。

VB.NET での LboxCombo
AIR & Flex3 での LboxCombo

データの持ち方はVB.NET と同じで、「item」 という入れ物に テキストとデータのセットとなる Object を入れて
( ここでは、DataClass )、それを介してデータにアクセスしています。

※ メソッド名の先頭を大文字にしているのは、オリジナルのメソッドと区別する為です



  
package lightbox;

import javax.swing.*;

public class LboxCombo extends JComboBox {

	// コンストラクタ
	public LboxCombo() {

		Initialize();

	}

	// 初期化
	private void Initialize() {

		this.setFont(new java.awt.Font("MS Pゴシック", 0, 12));

	}

	// 幅のみ変更
	public void SetWidth(int width) {

		int height;

		height = this.getHeight();
		this.setSize(width,height);

	}

	// データ追加
	public void Add(String Label, Object Data) {

		if ( Label == null ) {
			Label = "";
		}
		if ( Data == null ) {
			Data = "";
		}

		this.addItem(new DataClass(Label,Data));
	}

	// データ用内部クラス
	private class DataClass {

		private String Text;
		private Object Value;

		public DataClass(String paramText,Object paramValue) {
			this.Text = paramText;
			this.Value = paramValue;
		}

		public String getText() {
			return this.Text;
		}
		public Object getValue() {
			return this.Value;
		}

		public String toString() {
			return this.Text;
		}
	}

	// データ数
	public int Length() {

		return this.getItemCount();

	}

	// インデックスでテキストを取得
	public String GetText(int nIndex) {

		Object obj = this.getItemAt(nIndex);

		return ((DataClass)obj).getText();

	}

	// インデックスでデータを取得
	public Object GetData(int nIndex) {

		Object obj = this.getItemAt(nIndex);

		return ((DataClass)obj).getValue();

	}

	// インデックスで選択
	public void Select(int nIndex) {

		try {
			this.setSelectedIndex(nIndex);
		}
		catch ( IllegalArgumentException e ) {
			this.setSelectedIndex(0);
		}

	}

	// テキストで選択
	public void SelectByText(String label) {

		int i;
		int length = this.Length();

		for( i = 0; i < length; i++ ) {
			if ( label.equals(this.GetText(i))) {
				this.Select(i);
				break;
			}
		}

	}

	// データで選択
	public void SelectByValue(Object value) {

		int i;
		int length = this.Length();

		for( i = 0; i < length; i++ ) {
			if ( value.equals(this.GetData(i))) {
				this.Select(i);
				break;
			}
		}

	}

	// 選択されたテキスト
	public String SelectedText() {
		if (this.getSelectedIndex() == -1 ) {
			return "";
		}
		else {
			return this.GetText(this.getSelectedIndex());
		}
	}

	// 選択されたデータ
	public Object SelectedData() {
		if (this.getSelectedIndex() == -1 ) {
			return "";
		}
		else {
			return this.GetData(this.getSelectedIndex());
		}
	}

	// 開く
	public void Open() {

		this.showPopup();

	}

	// 閉じる
	public void Close() {

		this.hidePopup();

	}

	// インデックスで削除
	public void Delete(int nIndex) {

		this.removeItemAt(nIndex);

	}

	// 全て削除
	public void Clear() {

		this.removeAllItems();

	}

	// 全て削除
	public void Reset() {

		this.removeAllItems();

	}
}
  




  ビジュアル・クラス( Eclipse )



いわゆる、コンボボックスの onChange は、ActionListener の初回以外で発生します。
初回は、isShowing() が false になります

初期処理は、initialize で行っています。

※ ボタンは JButton そのままなので、フォントの設定は手動でコードを挿入しています

  
import javax.swing.SwingUtilities;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Rectangle;
import javax.swing.JLabel;
import javax.swing.JButton;

import lightbox.LboxCombo;

public class SBUN extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jPanel = null;
	private JLabel jLabel = null;
	private LboxCombo lboxCombo = null;
	private JButton jButton = null;
	private JButton jButton1 = null;
	private JButton jButton2 = null;
	private JButton jButton3 = null;
	private JButton jButton4 = null;
	private JButton jButton5 = null;
	/**
	 * This method initializes jPanel
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(23, 31, 88, 30));
			jLabel.setText("処理区分");
			jLabel.setFont(new java.awt.Font("MS Pゴシック", 0, 12));
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.add(jLabel, null);
			jPanel.add(getLboxCombo(), null);
			jPanel.add(getJButton(), null);
			jPanel.add(getJButton1(), null);
			jPanel.add(getJButton2(), null);
			jPanel.add(getJButton3(), null);
			jPanel.add(getJButton4(), null);
			jPanel.add(getJButton5(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes lboxCombo
	 *
	 * @return testcontrol.LboxCombo
	 */
	private LboxCombo getLboxCombo() {
		if (lboxCombo == null) {
			lboxCombo = new LboxCombo();
			lboxCombo.setBounds(new Rectangle(119, 33, 179, 24));
			lboxCombo.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {

				}
			});
			lboxCombo.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println(((LboxCombo)e.getSource()).isShowing());
				}
			});
		}
		return lboxCombo;
	}

	/**
	 * This method initializes jButton
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setFont(new java.awt.Font("MS Pゴシック", 0, 12));
			jButton.setBounds(new Rectangle(51, 125, 252, 30));
			jButton.setText("コンボボックスを開く");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					lboxCombo.Open();
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setFont(new java.awt.Font("MS Pゴシック", 0, 12));
			jButton1.setBounds(new Rectangle(51, 162, 252, 30));
			jButton1.setText("コンボボックスを閉じる");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					lboxCombo.Close();
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jButton2
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setFont(new java.awt.Font("MS Pゴシック", 0, 12));
			jButton2.setBounds(new Rectangle(51, 199, 252, 30));
			jButton2.setText("数値 \"1\" でコンボボックスを選択");
			jButton2.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					lboxCombo.SelectByValue(1);
				}
			});
		}
		return jButton2;
	}

	/**
	 * This method initializes jButton3
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setFont(new java.awt.Font("MS Pゴシック", 0, 12));
			jButton3.setBounds(new Rectangle(51, 236, 252, 30));
			jButton3.setText("\"修正\"でコンボボックスを選択");
			jButton3.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					lboxCombo.SelectByText("修正");
				}
			});
		}
		return jButton3;
	}

	/**
	 * This method initializes jButton4
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getJButton4() {
		if (jButton4 == null) {
			jButton4 = new JButton();
			jButton4.setFont(new java.awt.Font("MS Pゴシック", 0, 12));
			jButton4.setBounds(new Rectangle(51, 273, 252, 30));
			jButton4.setText("選択されたテキスト");
			jButton4.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JOptionPane.showMessageDialog(null,lboxCombo.SelectedText());
				}
			});
		}
		return jButton4;
	}

	/**
	 * This method initializes jButton5
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getJButton5() {
		if (jButton5 == null) {
			jButton5 = new JButton();
			jButton5.setFont(new java.awt.Font("MS Pゴシック", 0, 12));
			jButton5.setBounds(new Rectangle(51, 310, 252, 30));
			jButton5.setText("選択された値");
			jButton5.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JOptionPane.showMessageDialog(null,lboxCombo.SelectedData());
				}
			});
		}
		return jButton5;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				SBUN thisClass = new SBUN();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	/**
	 * This is the default constructor
	 */
	public SBUN() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 *
	 * @return void
	 */
	private void initialize() {
		this.setSize(371, 417);
		this.setContentPane(getJPanel());
		this.setTitle("JFrame");

		this.lboxCombo.Add("新規", 1);
		this.lboxCombo.Add("修正", 2);
		this.lboxCombo.Add("削除", 9);

		this.lboxCombo.Select(-1);
	}

}  //  @jve:decl-index=0:visual-constraint="20,20"
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ