InputDialog에서 사용됨. 항목이 12 개 미만이면 JComboBox, 항목이 12개 이상이면 JList가 사용됨.
String[] Icon[]
8
Object initialSelectionValue
InputDialog에서 초기 선택값
String, Icon
9
Object[] options
OptionDialog의 버튼들에 표시되는 텍스트 또는 이미지
String[] Icon[]
10
Object initialValue
초기 포커스를 갖는 버튼의 텍스트 또는 이미지
String Icon
▷ showMessageDialog() 메소드를 제외하고 나머지 세 개의 showXXXDialog() 메소드는 모두 리턴값이 있음
▶리턴값은 어떤 버튼이 클릭되었는지, 입력된 값이 무엇인지에 대한 정보를 담고 있음
① ConfirmDialog는 어떤 버튼이 클릭되었는가가 중요하기 때문에 showConfirmDialog() 메소드는 클릭된 버튼에 해당하는 int 타입의 JOptionPane 상수를 리턴
NO
JOptionPane 상수
설명
1
JOptionPane.OK_OPTION
[확인] 버튼을 눌렀을 때
2
JOptionPane.CANCEL_OPTION
[취소] 버튼을 눌렀을 때
3
JOptionPane.YES_OPTION
[예] 버튼을 눌렀을 때
4
JOptionPane.NO_OPTION
[아니오] 버튼을 눌렀을 때
5
JOptionPane.CLOSED_OPTION
우측 상단의 [×] 버튼을 눌렀을 때
② InputDialog는 입력된 값이 중요하기 때문에 showInputDialog() 메소드는 입력 컴포넌 트가 JTextField이면 String을 리턴하고, JList나 JComboBox라면 선택된 Object를 리턴 한다.
③ OptionDialog는 어떤 버튼이 클릭되었는지가 중요하기 때문에 showOptionDialog() 메 소드는 매개값으로 주어진 options 배열에서 클릭된 버튼의 인덱스를 리턴한다.
◎ 표준 다이얼로그 생성 및 메소드 리턴값 이용 예제
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class JOptionPaneExample extends JFrame {
private JButton btnMessage, btnConfirm;
private JButton btnInput, btnOption;
// 메인 윈도우 설정
public JOptionPaneExample() {
this.setTitle("JOptionPaneExample");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new GridLayout(4,1));
this.getContentPane().add(getBtnMessage());
this.getContentPane().add(getBtnConfirm());
this.getContentPane().add(getBtnInput());
this.getContentPane().add(getBtnOption());
this.setSize(500, 300);
}
// MessageDialog를 보여주는 버튼 생성
public JButton getBtnMessage() {
if(btnMessage == null) {
btnMessage = new JButton();
btnMessage.setText("MessageDialog");
btnMessage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// MessageDialog를 보여줌
JOptionPane.showMessageDialog(
JOptionPaneExample.this,
"다이얼로그 텍스트 내용",
"INFORMATION_MESSAGE",
JOptionPane.INFORMATION_MESSAGE);
}
});
}
return btnMessage;
}
// ConfirmDialog를 보여주는 버튼 생성
public JButton getBtnConfirm() {
if(btnConfirm == null) {
btnConfirm = new JButton();
btnConfirm.setText("ConfirmDialog");
btnConfirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// ConfirmDialog를 보여줌
int option = JOptionPane.showConfirmDialog(
JOptionPaneExample.this,
"다이얼로그 텍스트 내용",
"OK_CANCEL_OPTION",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE,
null);
// 클릭된 버튼 확인하기
if(option == JOptionPane.OK_OPTION) {
System.out.println("확인 버튼을 눌렀군요");
} else if(option == JOptionPane.CANCEL_OPTION) {
System.out.println("취소 버튼을 눌렀군요");
} else if(option == JOptionPane.CLOSED_OPTION) {
System.out.println("닫기 버튼을 눌렀군요");
}
}
});
}
return btnConfirm;
}
// InputDialog를 보여주는 버튼 생성
public JButton getBtnInput() {
if(btnInput == null) {
btnInput = new JButton();
btnInput.setText("InputDialog");
btnInput.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String input = null;
// 텍스트 입력을 받는 InputDialog를 보여줌
input = JOptionPane.showInputDialog(
JOptionPaneExample.this,
"다이얼로그 텍스트 내용",
"InputDialog",
JOptionPane.INFORMATION_MESSAGE);
// 선택된 항목을 출력
System.out.println("입력된 텍스트: " + input);
// JComboBox로 항목을 선택받는 InputDialog를 보여줌
input = (String) JOptionPane.showInputDialog(
JOptionPaneExample.this,
"다이얼로그 텍스트 내용",
"InputDialog",
JOptionPane.PLAIN_MESSAGE,
null,
new String[] {"Java", "JDBC", "JSP", "Spring"}, "JDBC");
// 선택된 항목을 출력
System.out.println("선택된 항목: " + input);
}
});
}
return btnInput;
}
// OptionDialog를 보여주는 버튼 생성
public JButton getBtnOption() {
if(btnOption == null) {
btnOption = new JButton();
btnOption.setText("OptionDialog");
btnOption.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// OptionDialog를 보여줌
int option = JOptionPane.showOptionDialog(
JOptionPaneExample.this,
"다이얼로그 텍스트 내용",
"OptionDialog",
JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
new String[] {"시작","종료"}, "시작");
// 클릭된 버튼 확인하기
if(option == 0) {
System.out.println("시작 버튼을 눌렀군요");
} else if(option == 1) {
System.out.println("종료 버튼을 눌렀군요");
}
}
});
}
return btnOption;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPaneExample jFrame = new JOptionPaneExample();
jFrame.setVisible(true);
}
});
}
}
1. MessageDialog : 메시지 창 출력 2. ConfirmDialog : 메시지 창 출력 ▷ OK 버튼 선택시 '확인 버튼을 눌렀군요' 출력 ▷ Cancle 버튼 선택시 '취소 버튼을 눌렀군요' 출력 3. InputDialog : 텍스트 입력 창 출력 ▷ 텍스트 입력시 '입력된 텍스트 : ???' 출력 ▷ 이후 다이얼로그 텍스트 내용 선택하는 체크박스 선택 ▶OK 버튼 선택시 '선택된 항목 : JAVA' 출력 ▶Cancle 버튼 선택시 '선택된 항목 : null' 출력 4. OptionDialog : 메시지 창 출력 ▷ 시작 버튼 선택시 '시작 버튼을 눌렀군요' 출력 ▷ 종료 버튼 선택시 '종료 버튼을 눌렀군요' 출력
다이얼로그에서 JOptionPane의 메소드들은 다양한 메시지 창들을 표현할 수 있습니다.