posted by 희정냥★ 2008. 11. 14. 10:00


이렇게 생긴거입니다 ㅋ

SWT 관련 jar를 추가시켜줘야 합니다.

- Java Project


import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
 * This class demonstrates how to create your own dialog classes. It allows
 * users to input a String
 */
public class InputDialog extends Dialog {

	// dialog 결과값
	boolean result = false;

	private String fileName = "";
	private String title = "";
	private String description = "";

	private Text fileNameText;
	private Text titleText;
	private Text descriptionText;

	
	Shell shell;
	// /////////////////////////////////////////////////////

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	// /////////////////////////////////////////////////////

	public InputDialog(Shell parent) {
		this(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
	}

	public InputDialog(Shell parent, int style) {
		super(parent, style);
	}

	public boolean open() {
		shell = new Shell(getParent(), getStyle());
		shell.setText(getText());
		createContents(shell);
		shell.pack();
		shell.open();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	private void createContents(final Shell shell) {

		shell.setText("Input Box");
		shell.setLayout(new GridLayout(2, true));

		// //////////////////////////////
		// fileName
		Label fileNameLabel = new Label(shell, SWT.NONE);
		fileNameLabel.setText("File Name");
		fileNameLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		fileNameText = new Text(shell, SWT.BORDER);
		fileNameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		((GridData) fileNameText.getLayoutData()).widthHint = 100;

		// //////////////////////////////
		// title
		Label titleLabel = new Label(shell, SWT.NONE);
		titleLabel.setText("Title");
		titleLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		titleText = new Text(shell, SWT.BORDER);
		titleText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		// ////////////////////////////
		// Description
		Label descriptionLabel = new Label(shell, SWT.NONE);
		descriptionLabel.setText("Description");
		descriptionLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		descriptionText = new Text(shell, SWT.BORDER);
		descriptionText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		// //////////////////////////////

		Button ok = new Button(shell, SWT.PUSH);
		ok.setText("OK");
		ok.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		ok.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				result = true;
				fileName = fileNameText.getText();
				title = titleText.getText();
				description = descriptionText.getText();
				shell.close();
			}
		});

		Button cancel = new Button(shell, SWT.PUSH);
		cancel.setText("Cancel");
		cancel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		cancel.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				result = false;

				shell.close();
			}
		});

		shell.setDefaultButton(ok);

	}

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);

		InputDialog dlg = new InputDialog(shell);

		if (dlg.open()) {
			System.out.println("ok");
			System.out.println(dlg.getFileName());
			System.out.println(dlg.getTitle());
			System.out.println(dlg.getDescription());

		} else {
			System.out.println("cancel");
		}
	}

}

댓글을 달아 주세요

  1. cremazer 2010.05.11 13:20  Addr  Edit/Del  Reply

    좋은정보 감사합니다 ^^ 다이얼로그 2개를 연동하여 메시징 처리를 하고 있었는데 데이터 호출하는 부분에서 막혔었는데, 올려주신 소스 보고 힌트를 얻어서 해결했습니다ㅋ