Eclipse 4.4 Pleiades はベクターからダウンロードして解凍すればすぐに使えます。
※ 4.5 は動作しないので、4.4を使用します
プロジェクトを一から作る方法はこちらから参照できますが、とても面倒ですし展開されるテンプレートが英語コメントなのでこちらのテンプレート(コメントを日本語で入力しました)でサクっと作ってしまいます。
インポート
インポートは、『既存プロジェクトをワークスペースへ』を選択して下さい。
その後、アーカイブファイルの選択をするだけです
但し、このままでは WindowBulder エディタでソースが開かれないので、右クリックの『次で開く』から『WindowBuilder エディタ』を選択します
Window 配置について
初期配置に違和感を感じる場合は、新たにパースペクティブから 『Java』を選びます。
そして、『タスクリスト』と『アウトライン』は閉じるといいでしょう(復活は、ウインドウメニューの『ビューの表示』)
フォントの変更
Java のエディタと通常のテキストエディタとコンソールのフォントを変更します。
ウインドウの『設定』で設定ウインドウを開いて、左上のフィルター入力に『フォント』と入力します。
▼ Java
フォントがすぐ変更されない場合は、いったんソースタブを閉じてもう一度開きます
▼ デバッグ
▼ 基本
TopWindow.java
ウインドウアプリケーションでは、ブラウザコントロールに WEB ページを表示しています。デフォルトでは、URL を使用してページを表示しますが、getType 変数の中身を変更すると、HttpGet クラスを使用して、HTML データを取得してブラウザに表示します。
ダイアログボタンをクリックすると、ダイアログを開いてテーブルコントロールにデータを表示します。また、タイトルの設定や、アイコンの設定を configureShell で行っています。
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class TopWindow extends ApplicationWindow {
private String getType = "browser";
// Run Code の PHP サンプルの URL
private String url = "http://rextester.com/CPAFA49735";
private int width = 800;
private int height = 980;
private Browser browser;
// *********************************
// コンストラクタ
// *********************************
public TopWindow() {
super(null);
}
// *********************************
// コントロールの追加
// ※ Design での変更が反映されます
// *********************************
@Override
protected Control createContents(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
// ▼ Design にから作成された記述
Button button = new Button(container, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// ▼ ボタンが押された時の処理( 後からコードを追加 )
System.out.println("ボタンがクリックされました");
// プライベート変数の getType の内容で処理を変えるようにしています
if ( getType.equals("browser") ) {
browser.setUrl(url);
System.out.println("Browser に url をセットして表示しました");
}
else {
// インターネットからデータを取り出しています
String s = HttpGet.execute(url, "utf-8");
browser.setText(s);
System.out.println("Browser に HTML をセットして表示しました");
}
}
});
button.setBounds(10, 10, 81, 28);
button.setText("ブラウザ表示");
browser = new Browser(container, SWT.NONE);
browser.setBounds(10, 44, 750, 850);
Button button_1 = new Button(container, SWT.NONE);
button_1.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// ダイアログを表示します
Dialog1 dialog = new Dialog1(getShell());
int result = dialog.open();
System.out.println(result);
}
});
button_1.setBounds(117, 10, 81, 28);
button_1.setText("ダイアログ");
return container;
}
// *********************************
// アプリケーションの開始
// *********************************
public static void main(String args[]) {
try {
// 自分自身(TopWindow) の作成
TopWindow window = new TopWindow();
// open を実行したら、Window を閉じるまで次の行を実行しない
window.setBlockOnOpen(true);
window.open();
// Window を閉じたら以下を実行してアプリケーションを終了する
Display.getCurrent().dispose();
} catch (Exception e) {
e.printStackTrace();
}
}
// *********************************
// Window の初期設定
// *********************************
@Override
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
// タイトルの設定
newShell.setText("タイトル文字列");
// アイコンの設定
ImageData id = new ImageData("image/lightbox.png");
Image img = new Image(newShell.getDisplay(), id);
newShell.setImage(img);
}
// *********************************
// Window サイズの初期設定
// *********************************
@Override
protected Point getInitialSize() {
// プライベート変数を使用してウインドウサイズを決定しています
return new Point(width, height);
}
}
Dialog1
ここでは、テーブルコントロールを配置して、仮のデータをセットしています。このパターンを応用する事によって、外部からのデータをループ処理で一覧表示する事が可能になります。
import java.awt.GraphicsEnvironment;
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;
// *********************************
// コンストラクタ
// *********************************
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[] data;
// 行の作成
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)
// );
}
}
HttpGet
インターネットより、テキストデータを取得します
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpGet {
// **********************************************
// 指定した URL へ 任意の charset で処理
// **********************************************
public static String execute(String targetUrl,String targetCharset) {
StringBuffer web_data = new StringBuffer();
try {
// **********************************************
// インターネットへの接続
// **********************************************
// 読み込む WEB上のターゲット
URL url = new URL(targetUrl);
// 接続オブジェクト
HttpURLConnection http = (HttpURLConnection)url.openConnection();
// GET メソッド
http.setRequestMethod("GET");
// 接続
http.connect();
// **********************************************
// ストリームとして読み込む準備
// **********************************************
// 以下読み込み3点セット InputStream / InputStreamReader / BufferedReader
InputStream input_stream = http.getInputStream();
// UTF-8 でリーダーを作成
InputStreamReader input_stream_reader = new InputStreamReader(input_stream, targetCharset);
// 行単位で読み込む為の準備
BufferedReader buffered_reader = new BufferedReader(input_stream_reader);
// **********************************************
// 行の一括読み込み
// **********************************************
String line_buffer = null;
// BufferedReader は、readLine が null を返すと読み込み終了
while ( null != (line_buffer = buffered_reader.readLine() ) ) {
// コマンドプロンプトに表示
web_data.append( line_buffer );
web_data.append( "\n" );
}
// **********************************************
// 接続解除
// **********************************************
http.disconnect();
}
catch(Exception e) {
// 失敗
System.out.println( "インターネットへのアクセスに失敗しました" );
e.printStackTrace();
}
return web_data.toString();
}
}