Tentang JOptionPane Class pada Java
Pada post kali ini, kita akan belajar tentang JOptionPane dan penggunaannya dalam pemrograman swing Java.
JOptionPane merupakan class yang digunakan sebagai standar dialog box untuk memberikan informasi kepada pengguna aplikasi. Sebenarnya JOptionPane memiliki banyak method, dan kebayakan method tersebut static method. Berikut ini 4 method dari JOptionPane yang sering digunakan.
- showConfirmDialog
- showInputDialog
- showMessageDialog
- showOptionDialog
Untuk parameter dari 4 method tersebut mempunyai pattern yang sama. Dan setiap method tersebut memiliki 4 – 7 varian tergantung dari jumlah parameter setiap method ( kecuali showOptionDialog yang cuma memiliki satu variant) . Berikut ini beberapa parameter yang digunakan pada 4 method tersebut.
- parentComponent : mendefinisikan component yang menjadi parent dari dialog box. Jika diisi dengan null, maka secara default Frame akan menjadi parent component.
- message : merupakan message yang akan ditampilkan dalam dialog box. Message bisa berupa String. Tapi type sebenarnya dari parameter ini adalah Object class.
- title : merupakan title untuk dialog box
- initialValue : merupakan input value
- messageType : mendefinisikan style dari message. Jenis dari messageType sebagai berikut ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, PLAIN_MESSAGE
- optionType : mendefinisikan kumpulan button option yang muncul pada dialog box. Nilai dari parameter sebagai berikut. DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION, OK_CANCEL_OPTION ( Nilai dari variable ini dapat di-customize)
Berikut ini contoh codenya. ( Kembali kedasar, tanpa menggunakan pallete pada NetBeans )
Nama file : JOptionDemo.java
/**
*
* @author Taeyeon
*/
import java.awt.Component;
import java.awt.FlowLayout;
import javax.swing.JComponent.*;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
public class JOptionDemo extends JFrame {
public JOptionDemo(String title){
super(title);
setSize(500,100);
JButton button1 = new JButton("Click Me!");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog((Component) event.getSource(),"Selamat Pagi Dunia","Message Dialog example",3);
}
});
JButton button2 = new JButton("What is your name?");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
String nameUser = JOptionPane.showInputDialog((Component)event.getSource(),"Input Your Name" );
if(nameUser!=null && nameUser!=""){
JOptionPane.showMessageDialog((Component) event.getSource(),"Arigatou "+ nameUser);
}}
});
JButton button3 = new JButton("Close Application");
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
int option = JOptionPane.showConfirmDialog((Component)event.getSource(), "Close Application?");
if (option == JOptionPane.YES_OPTION){
System.exit(0);
} else if(option==JOptionPane.NO_OPTION){
//Do Nothing
}
}
});
setLayout(new FlowLayout(FlowLayout.CENTER));
add(button1);
add(button2);
add(button3);
}
public static void main(String[] args){
new JOptionDemo("Dialog Demo").setVisible(true);
}
}
Semoga bermanfaat 😀
Referensi : Dari berbagai sumber
Recent Posts
Categories
Blog Stats
- 451,056 hits
Archives
- October 2021 (1)
- September 2021 (1)
- November 2020 (1)
- October 2020 (1)
- September 2020 (1)
- August 2020 (1)
- July 2020 (1)
- June 2020 (1)
- May 2020 (1)
- April 2020 (1)
- March 2020 (1)
- February 2020 (1)
- January 2020 (1)
- November 2019 (1)
- October 2019 (1)
- September 2019 (1)
- April 2019 (1)
- April 2018 (1)
- March 2018 (1)
- February 2018 (1)
- January 2018 (1)
- October 2017 (1)
- September 2017 (1)
- August 2017 (1)
- July 2017 (1)
- June 2017 (1)
- May 2017 (1)
- April 2017 (1)
- March 2017 (1)
- February 2017 (1)
- January 2017 (3)
- October 2016 (1)
- September 2016 (1)
- August 2016 (1)
- July 2016 (1)
- April 2016 (1)
- March 2016 (1)
- February 2016 (1)
- January 2016 (1)
- December 2015 (1)
- November 2015 (1)
- October 2015 (1)
- September 2015 (1)
- August 2015 (1)
- July 2015 (1)
- June 2015 (1)
- May 2015 (2)
- April 2015 (2)
- March 2015 (2)
- February 2015 (1)
- January 2015 (1)
- December 2014 (1)
- November 2014 (3)
- October 2014 (1)
- September 2014 (1)
- August 2014 (3)
- July 2014 (1)
- June 2014 (1)
- May 2014 (2)
- April 2014 (3)
- March 2014 (1)
- February 2014 (6)
- January 2014 (1)
- December 2013 (12)
- November 2013 (7)
- October 2013 (1)
- September 2013 (7)
- August 2013 (4)
- July 2013 (8)
- June 2013 (2)
- May 2013 (3)
- April 2013 (1)
- March 2013 (2)
- February 2013 (2)
- January 2013 (3)
- December 2012 (4)
- November 2012 (1)
- October 2012 (2)
- September 2012 (13)
- August 2012 (3)
- July 2012 (1)
- June 2012 (1)
- May 2012 (1)
- April 2012 (2)
- March 2012 (1)
- February 2012 (1)
- January 2012 (3)
- December 2011 (5)
- November 2011 (1)
- October 2011 (4)
- September 2011 (1)
- August 2011 (3)
- July 2011 (1)
- June 2011 (5)
- May 2011 (10)
- April 2011 (9)
- March 2011 (11)
- February 2011 (8)
- January 2011 (7)
- December 2010 (9)
- November 2010 (14)
- October 2010 (9)
- September 2010 (3)
- August 2010 (13)
- July 2010 (7)