Showing posts with label java swing. Show all posts
Showing posts with label java swing. Show all posts

Aug 22, 2014

SWING JScrollPane (JAVA GUI)





This post about scroll.

We need scrollbar when making chat or communication program.

We can easily add scrollbar using JScrollPane.

Just add JScrollPane and paste to text-window.

My case paste to JEditorPane commText.


And go to source.

  1.         JScrollPane scrollPane = new JScrollPane(commText);
  2.         scrollPane.setBounds(12, 10, 471, 389);
  3.         frame.getContentPane().add(scrollPane);
Look at Line 1.

JScrollPane include JEditorPane commText.

And Line 2 is JScrollPane's size.

The size bigger then JEditorPane's size.

Okay, run to source.

You can see this picture.


You don't see Scrollbar .

If you add text more

You can see Scrollbar.


Aug 21, 2014

JOptionPane showInputDialog, showOptionDialog (JAVA SWING GUI)







This post about showInputDialog and showOptionDialog of JOptionPane class.

This method is simple than previous posting method.

I added button for test(showInputDialog and showOptionDialog).




showInputDialog test 2-type.

First button is just input dialog.

Second is choice dialog.

showOptionDialog is similar to showConfirmDialog. 

But showoptionDialog is choice the option.

We change the option value.

Okay go to source code.

If you don't understand JButton, visit my post JButton.

This is first showInputDialog source.

  1.         //showInputDialog
  2.         else if(e.getSource()==btnShowinputdialog){
  3.             String text=JOptionPane.showInputDialog(null,"What's your hobby?","hobby",JOptionPane.INFORMATION_MESSAGE);
  4.             JOptionPane.showMessageDialog(null,"Your hobby is "+text);
  5.         }
Line 3 : simple showInputDialog.

Method is 

JOptionPane.showInputDialog(Component arg0, Object arg1, String arg2, int arg3)

Component arg0 : location. generally null.

Object arg1 : showInputDialog's Message.

String arg2 : showInputDialog's Title.

int arg3 : showInputDialog's Icon.

And showInputDialog's return value is String.

So, I using this return value.

Line 4: Display showInputDialog's return value.

Click to showInputDialog



I write 'basketball' and click to 'Yes'.




This second showInputDialog's source


  1.         }else if(e.getSource()==btnShowmultiinputdialog){
  2.             Object[] choiceOne={"SAMSUNG-GALAXY","APPLE-IPhone","LG-G","Pantech-VEGA"};
  3.             Object choose=JOptionPane.showInputDialog(null,"what is best cell phone?" ,"Best phone",JOptionPane.QUESTION_MESSAGE,null,choiceOne,choiceOne[0]);
  4.             JOptionPane.showMessageDialog(null,"Your best cell phone is "+choose);
  5.         }

Second showinputDialog's Method is
JOptionPane.showInputDialog(Component arg0, Object arg1, String arg2, int arg3, Icon arg4, Object[] arg5, Object arg6)
Component arg0 : Location dialog. Generally null.

Object arg1 : showInputDialog's Message.

String arg2 : showInputDialog's Title.

int arg3 : showInputDialog's icon.

Icon arg4 : maybe null.

Object[] arg5 : Spinner's item.

Object arg6 : Default show item.

Click to showMultiInputDialog.



Line 2 : My item is 'SAMSUNG-GALAXY', APPLE-IPhone', 'LG-G', 'Pantech-VEGA'.

Line 3: I choose default item SAMSUNG-GALAXY. So. default show 'SAMSUNG-GALAXY'.

I choose APPLE-IPhone and click to 'Yes'.



This is showOptionDialog's source code.


  1.         //showOptionDialog
  2.         else if(e.getSource()==btnShowoptiondialog){
  3.             Object[] option={"IPhone5S","Galaxy Note3","G3"};
  4.             int response=JOptionPane.showOptionDialog(null, "What's your phone?","Select Phone",JOptionPane.DEFAULT_OPTION ,JOptionPane.QUESTION_MESSAGE, null, option, option[0]);
  5.             if(response==0){
  6.                 JOptionPane.showMessageDialog(null,"Your phone's company is APPLE");
  7.             }else if(response==1){
  8.                 JOptionPane.showMessageDialog(null,"Your phone's company is SAMSUNG");
  9.             }else if(response==2){
  10.                 JOptionPane.showMessageDialog(null,"Your phone's company is LG");
  11.             }
  12.         }
  13.         //showOptionDialog
showOptionDialog Method is


JOptionPane.showOptionDialog(Component arg0, Object arg1, String arg2, int arg3, int arg4, Icon arg5, Object[] arg6, Object arg7)
Component arg0 : location. Generally null.

Object arg1 : showOptionDialog's message.

String arg2 : showOptionDialog's title.

int arg3 : message option. Generally JOptionPane.DEFAULT_OPTION.

int arg4 : showOptionDialog's icon.

Icon arg5 : maybe null.

Object[] arg6 : option items.

Object arg7 : default selected item.

And showOptionDialog's return value is integer type.

Click to showOptionDialog.



Line 3 : I made items 'IPhone5S', 'Galaxy Note3', 'G3'.

Line 4 : I select default item 'IPhone5S'. show Dialog.

And I click to 'Galaxy Note3'.





JOptionPane showConfirmDialog (JAVA SWING GUI)





This time I posting about showConfirmDialog.

This Dialog similar to showMessageDialog. But this is 'confirm'.

'showConfirmDialog' has 4 options.

Option is...


  • DEFAULT_OPTION
  • YES_NO_OPTION
  • YES_NO_CANCEL_OPTION
  • OK_CANCEL_OPTION


I add source code to showMessageDialog test source.

There are new source's layout!!


Left side is test for showMessageDialog.

Right side is test for showConfirmDialog.

Okay, go to source.

  1.     @Override
  2.     public void actionPerformed(ActionEvent e) {
  3.         //showMessageDialog
  4.         if(e.getSource()==btnErrMsg){
  5.             JOptionPane. showMessageDialog(null,"THIS IS ERROR MESSAGE","ERROR TITLE",JOptionPane.ERROR_MESSAGE);
  6.         }else if(e.getSource()==btnInfoMsg){
  7.             JOptionPane.showMessageDialog(null,"THIS IS INFORMATION MESSAGE","INFORMATION TITLE",JOptionPane.INFORMATION_MESSAGE);
  8.         }else if(e.getSource()==btnWarningMsg){
  9.             JOptionPane.showMessageDialog(null,"THIS IS WARNING MESSAGE","WARNING TITLE",JOptionPane.WARNING_MESSAGE);
  10.         }else if(e.getSource()==btnQuestionMsg){
  11.             JOptionPane.showMessageDialog(null,"THIS IS QUESTION MESSAGE","QUESTION TITLE",JOptionPane.QUESTION_MESSAGE);
  12.         }else if(e.getSource()==btnPlainMsg){
  13.             JOptionPane.showMessageDialog(null,"THIS IS PLAIN MESSAGE","PLAIN TITLE",JOptionPane.PLAIN_MESSAGE);
  14.         }
  15.        
  16.         //showconfirmDialog
  17.         else if(e.getSource()==btnDefaultoption){
  18.             JOptionPane.showConfirmDialog(null,"THIS IS DEFAULT OPTION","DEFAULT TITLE",JOptionPane.DEFAULT_OPTION);
  19.             
  20.         }else if(e.getSource()==btnYesnooption){
  21.             JOptionPane.showConfirmDialog(null,"THIS IS YES_NO OPTION","YES_NO TITLE",JOptionPane.YES_NO_OPTION);
  22.         }else if(e.getSource()==btnYesnocanceloption){
  23.             JOptionPane.showConfirmDialog(null,"THIS IS YES_NO_CANCEL OPTION","YES_NO_CANCEL TITLE",JOptionPane.YES_NO_CANCEL_OPTION);
  24.         }else if(e.getSource()==btnOkcanceloption){
  25.             JOptionPane.showConfirmDialog(null,"THIS IS OK_CANCEL OPTION","OK_CANCEL TITLE",JOptionPane.OK_CANCEL_OPTION);
  26.         }
  27.         //showconfirmDialog
  28.     }
Line 16~27 : adding code for showConfirmDialog.

JOptionPane.showConfirmDialog(Component arg0, Object arg1, String arg2, int arg3)
Component arg0 : location of dialog. So generally null.

Object arg1 : confrim message.

String arg2 : title

int arg3 : showConfirmDialog's option.

Look at Line 18, 21, 23, 25

This is making dialog.

Click to DEFAULT_OPTION



Click to YES_NO_OPTION


Click to YES_NO_CANCEL_OPTION



Click to OK_CANCEL_OPTION

Default icon is QUESTION_MESSAGE.

If you want change the icon, add parameter for icon.

  1. JOptionPane.showConfirmDialog(null,"THIS IS DEFAULT OPTION","DEFAULT TITLE",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);





And showConfirmDialog returned integer value.

You can use this return value for choice situation.

  1. int choose=JOptionPane.showConfirmDialog(null,"THIS IS YES_NO OPTION","YES_NO TITLE",JOptionPane.YES_NO_OPTION);
  2.            
  3. if(choose==JOptionPane.YES_OPTION){
  4.     JOptionPane.showMessageDialog(null,"You are choose YES!!");
  5. }else if(choose==JOptionPane.NO_OPTION){
  6.     JOptionPane.showMessageDialog(null,"You are choose NO!!");
  7. }

This is YES_NO_OPTION.

If you choose 'yes'


If you choose 'no'



Aug 19, 2014

How to Using JCheckBox of SWING (JAVA GUI)





This post is 'How to using JCheckBox'.

If You run SWING, can see below picture.



I made 1 JCheckBox.

And change the name.


'Variable' is variable name in Source.

'text' is check box name in GUI.

Go to 'Source'

serverSWING.java

  1. public class serverSWING implements ActionListener,ItemListener{
  2.     private JFrame frame;
  3.     private JTextField portNum;
  4.     private JButton btnServerOpen,btnClear,btnSend;
  5.     public static JEditorPane commText;
  6.     ServerSocket ss;
  7.     Socket socket;
  8.     InetAddress ia;
  9.     Thread server;
  10.     public static JButton btnServerClose;
  11.     public static boolean disconnected=true;
  12.     private JEditorPane sendText;
  13.     public static boolean echoEnable=false;
  14.     private JCheckBox checkEcho;
  15.     private JScrollPane scrollPane;
  16. }
Line 1 : 'ItemListener' is for JCheckBox listener.

Line 14 : declared JCheckBox.

This is initailize.

  1. checkEcho = new JCheckBox("Echo Mode");
  2. checkEcho.setBounds(493, 375, 119, 23);
  3. frame.getContentPane().add(checkEcho);
  4. checkEcho.addItemListener(this);
Line 4 : registration to listener.

This is auto adding listener code.

  1.     @Override
  2.     public void itemStateChanged(ItemEvent e) {
  3.         if(e.getSource()==checkEcho){
  4.             if(ItemEvent.SELECTED==e.getStateChange())
  5.                 echoEnable=true;
  6.             else
  7.                 echoEnable=false;
  8.            
  9.             System.out.println("now echoEnable: "+echoEnable);
  10.             checkEcho();
  11.         }
  12.        
  13.     }
Line 3 : 'e.getSource()' is JCheckBox's name.

Line 4: 'ItemEvent.SELECTED' mean checked JCheckBox.

Aug 18, 2014

How to using JButton of SWING (JAVA)



SWING is GUI tool kit for JAVA.

You can using it. If you installing plug-in.

This post is JButton.

JButton is button of SWING.

If you executing swing, can see this picture.


There are 2 type button. (JButton and Button)

JButton is for SWING and  Button is for AWT.

I use JButton.

This is my sample source.


You can making Button likes this picture.

And look, left side!!

I checked 'Variable' and 'text'.

Variable is JButton's name.

You possible to change name.

And text is display text.

You can check right side.

Okay, go to java source.


  1. public class serverSWING implements ActionListener,ItemListener{
  2.     private JFrame frame;
  3.     private JTextField portNum;
  4.     private JButton btnServerOpen,btnClear,btnSend;
  5.     public static JEditorPane commText;
  6.     ServerSocket ss;
  7.     Socket socket;
  8.     InetAddress ia;
  9.     Thread server;
  10.     public static JButton btnServerClose;
  11.     public static boolean disconnected=true;
  12.     private JEditorPane sendText;
  13.     public static boolean echoEnable=false;
  14.     private JCheckBox checkEcho;
  15.     /**
  16.      * Launch the application.
  17.      */
  18.     public static void main(String[] args) {
  19.         EventQueue.invokeLater(new Runnable() {
  20.             public void run() {
  21.                 try {
  22.                     serverSWING window = new serverSWING();
  23.                     window.frame.setVisible(true);
  24.                 } catch (Exception e) {
  25.                     e.printStackTrace();
  26.                 }
  27.             }
  28.         });
  29.     }


Line 1 :  I implement 'ActionListener'. This similar Android's 'onClickListener'.

Line 5 : I declared JButton.

Line 19 : Starting SWING.



  1. /**
  2.      * Create the application.
  3.      */
  4.     public serverSWING() {
  5.         initialize();
  6.     }
  7.     /**
  8.      * Initialize the contents of the frame.
  9.      */
  10.     private void initialize() {
  11.         frame = new JFrame();
  12.         frame.setBounds(100, 100, 640, 480);
  13.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14.         frame.getContentPane().setLayout(null);
  15.        
  16.         portNum = new JTextField();
  17.         portNum.setText("5000");
  18.         portNum.setBounds(549, 10, 59, 21);
  19.         frame.getContentPane().add(portNum);
  20.         portNum.setColumns(10);
  21.        
  22.         JLabel lblPort = new JLabel("PORT");
  23.         lblPort.setBounds(503, 13, 44, 15);
  24.         frame.getContentPane().add(lblPort);
  25.        
  26.         btnServerOpen = new JButton("SERVER OPEN");
  27.         btnServerOpen.addActionListener(this);
  28.            
  29.         btnServerOpen.setBounds(493, 41, 119, 23);
  30.         frame.getContentPane().add(btnServerOpen);
  31.        
  32.         commText = new JEditorPane();
  33.         commText.setText("Plz, type port and press connect button.");
  34.         commText.setBounds(12, 10, 471, 389);
  35.         frame.getContentPane().add(commText);
  36.        
  37.         btnServerClose = new JButton("SERVER Close");
  38.         btnServerClose.addActionListener(this);
  39.         btnServerClose.setBounds(493, 74, 119, 23);
  40.         frame.getContentPane().add(btnServerClose);
  41.        
  42.         btnClear = new JButton("Clear");
  43.         btnClear.addActionListener(this);
  44.         btnClear.setBounds(493, 107, 119, 23);
  45.         frame.getContentPane().add(btnClear);
  46.        
  47.         sendText = new JEditorPane();
  48.         sendText.setBounds(12, 409, 471, 21);
  49.         frame.getContentPane().add(sendText);
  50.        
  51.         btnSend = new JButton("Send");
  52.         btnSend.addActionListener(this);
  53.        
  54.         btnSend.setBounds(493, 407, 119, 23);
  55.         frame.getContentPane().add(btnSend);
  56.        
  57.         checkEcho = new JCheckBox("Echo Mode");
  58.         checkEcho.setBounds(493, 375, 119, 23);
  59.         frame.getContentPane().add(checkEcho);
  60.         checkEcho.addItemListener(this);
  61.        
  62.     }

Line 5 : initailize to serverSWING.

Line 27~31 : GUI's position, registration listener and make button.

This auto making source about actionListener.

  1.     @Override
  2.     public void actionPerformed(ActionEvent e) {
  3.    
  4.         }

Add code. 

  1.     @Override
  2.     public void actionPerformed(ActionEvent e) {
  3.         if(e.getSource()==btnServerOpen){
  4.             String getPort=portNum.getText();
  5.             if(!getPort.isEmpty()){
  6.                
  7.                 int port=Integer.parseInt(getPort);
  8.                
  9.                 portOpen(port);
  10.                
  11.                
  12.             }else{
  13.                 commText.setText("Not found PORT number");
  14.             }
  15.            
  16.         }
  17.     }

Line 3 : 'e.getSource()== btnServerOpen' similar android's 'e.getId()'.

but 'getID()' is integer. So android possible to using 'switch-case'.