▼ テンプレートはこちらから Eclipse 4.4 Pleiades + WindowBuilder テンプレート / アプリケーションウインドウとダイアログテンプレートにはあらかじめ Table コントロールを配置しています。元々のコードは上のリンク先を参照して下さい。こちらでは、追加として MySQL の mysql-connector-java-5.1.30-bin.jar をプロジェクト内に lib フォルダを作成してコピーしています。 ※ ファイルシステムからコピーして、プロジェクトツリーからリフレッシュすると表示されます その後、mysql-connector-java-5.1.30-bin.jar を右クリックして『ビルド・パス』を開くと追加のメニューが現れるので実行すると使えるようになります。 SQL をテキストファイルから読み込む static メソッド TopWindow の中に追加して使います
static String loadText(String name) throws Exception { FileInputStream fis = new FileInputStream(name); int size = fis.available(); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); StringBuffer all_string = new StringBuffer(size); String str = null; while ((str = br.readLine()) != null) { // 初回以外は前に改行を挿入する if (!all_string.toString().equals("")) { all_string.append("\n"); } all_string.append(str); } br.close(); return all_string.toString(); }
後は、Dialog1 の初期表示で MySQL からデータを読み込みます Dialog1.java
import java.awt.GraphicsEnvironment; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; import org.eclipse.swt.widgets.TableItem; public class Dialog1 extends Dialog { private int width = 800; private int height = 600; private Table table; private Connection con; private Statement stmt; private ResultSet rset; // ********************************* // コンストラクタ // ********************************* public Dialog1(Shell parentShell) { super(parentShell); } // ********************************* // コントロールの追加 // ※ Design での変更が反映されます // ********************************* @Override protected Control createDialogArea(Composite parent) { Composite container = (Composite) super.createDialogArea(parent); // テーブルの作成 table = new Table(container, SWT.BORDER | SWT.FULL_SELECTION); table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1)); table.setHeaderVisible(true); table.setLinesVisible(true); // 列の作成 TableColumn tableColumn = new TableColumn(table, SWT.NONE); tableColumn.setWidth(100); tableColumn.setText("名前"); TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE); tableColumn_1.setWidth(131); tableColumn_1.setText("詳細"); String connectionString = "jdbc:mysql://localhost/lightbox?" + "user=root&password=パスワード"; TableItem tableItem; String[] columnData = new String[2]; try { con = DriverManager.getConnection(connectionString); stmt = con.createStatement(); rset = stmt.executeQuery ( TopWindow.loadText("data/sql1.txt") ); ResultSetMetaData rm = rset.getMetaData(); int nCols = rm.getColumnCount(); String[] data = new String[nCols]; int i; while( rset.next() ) { for( i = 0; i < nCols; i++ ) { data[i] = rset.getString( i+1 ); if ( i != 0 ) { System.out.print( "," ); } System.out.print( data[i] ); } System.out.println( ); columnData[0] = rset.getString("氏名"); if ( rset.getString("管理者名") == null ) { columnData[1] = "管理者"; } else { columnData[1] = String.format("%s の部下", rset.getString("管理者名") ); } tableItem = new TableItem(table, SWT.NONE); tableItem.setText(columnData); } stmt.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } // 行の作成 // TableItem tableItem = new TableItem(table, SWT.NONE); // data = new String[]{"山田","主任"}; // tableItem.setText(data); // // TableItem tableItem_1 = new TableItem(table, SWT.NONE); // data = new String[]{"鈴木","プロジェクトリーダ"}; // tableItem_1.setText(data); return container; } // ********************************* // ダイアログ専用のボタン // ********************************* @Override protected void createButtonsForButtonBar(Composite parent) { createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true); createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false); } // ********************************* // Window サイズの初期設定 // ********************************* @Override protected Point getInitialSize() { // プライベート変数を使用してウインドウサイズを決定しています return new Point(width, height); } // ********************************* // Window の初期位置 // ※ デスクトップの中心 // ********************************* @Override protected Point getInitialLocation(Point initialSize) { GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); // デスクトップのサイズ Point windowSize = new Point( env.getMaximumWindowBounds().width, env.getMaximumWindowBounds().height); Point position = new Point(0,0); // デスクトップの中心に表示します return new Point( position.x + ((windowSize.x - width) / 2), position.y + ((windowSize.y - height) / 2) ); /* 親ウインドウの中心の場合 */ // Point windowSize = this.getShell().getParent().getSize(); // Point position = this.getShell().getParent().getLocation(); // // System.out.println(windowSize.x); // // return new Point( // position.x + ((windowSize.x - width) / 2), // position.y + ((windowSize.y - height) / 2) // ); } }
▼ 外部ファイルの SQLselect a.*, b.氏名 as 管理者名 from 社員マスタ a left outer join 社員マスタ b on a.管理者 = b.社員コード▼ テーブルの create 文create table `社員マスタ` ( `社員コード` VARCHAR(4) ,`氏名` VARCHAR(50) ,`フリガナ` VARCHAR(50) ,`所属` VARCHAR(4) ,`性別` INT ,`作成日` DATETIME ,`更新日` DATETIME ,`給与` INT ,`手当` INT ,`管理者` varchar(4) ,`生年月日` DATETIME ,primary key(`社員コード`) )関連する記事 MySQL 5.6 : テストデータ自動作成スクリプト
|
【java : WindowBuilderの最新記事】
- WindowBuilder JFace : WEB より JSON を取得して Table に表示する
- Pleiades All in One で、Windows アプリを作成する手順( WindowBuilder + Swing デザイナー or SWT デザイナー[JFace] )
- WindowBuilder(Swing) で、WEBカメラを使用して画像を保存して okhttp で WEBサーバへアップロードする
- Java WindowBuilder から超簡易掲示板(CSVタイプ)投稿
- WindowBuilder のテーブルコントロールにインターネットから JSON データを読み込んで処理するテンプレート
- Eclipse 4.4 Pleiades + WindowBuilder テンプレート / アプリケーションウインドウとダイアログ / インポート手順