SNIPPETCENTRAL.NET

UIManager Look And Feel

Jul 5, 2017 - 53 Views
This code snippet shows how to set the look and feel of components to the system.

Listing 1

  1. 1import  javax.swing.UIManager;
  2. 2import  javax.swing.JFrame;
  3. 3import  javax.swing.JPanel;
  4. 4import  javax.swing.JButton;
  5. 5
  6. 6class  Main{
  7. 7        
  8. 8        public  static  void  main(String  args[]){
  9. 9                try{
  10. 10                        //  Set  System  L&F
  11. 11                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  12. 12                }  
  13. 13                catch  (Exception  e)  {
  14. 14                        //  handle  exception
  15. 15                }
  16. 16                Main  main  =  new  Main();
  17. 17        }
  18. 18        
  19. 19        public  Main(){
  20. 20                JFrame  frame  =  new  JFrame();
  21. 21                JPanel  panel  =  new  JPanel();
  22. 22
  23. 23                JButton  button1  =  new  JButton("Button 1");
  24. 24                JButton  button2  =  new  JButton("Button 2");
  25. 25                
  26. 26                panel.add(button1);
  27. 27                panel.add(button2);
  28. 28                
  29. 29                frame.add(panel);
  30. 30                
  31. 31                frame.setSize(300,300);
  32. 32                frame.setVisible(true);
  33. 33        }
  34. 34}

Simple JToolBar Example

Jul 5, 2017 - 53 Views
This code snippet shows how to add a JToolBar component to a form with JButtons.

Listing 1

  1. 1import  javax.swing.JFrame;
  2. 2import  javax.swing.JPanel;
  3. 3import  javax.swing.JToolBar;
  4. 4import  javax.swing.JButton;
  5. 5import  java.awt.FlowLayout;
  6. 6import  java.awt.event.ActionListener;
  7. 7import  java.awt.event.ActionEvent;
  8. 8
  9. 9class  Main  implements  ActionListener{
  10. 10        
  11. 11        public  static  void  main(String  args[]){
  12. 12                Main  main  =  new  Main();
  13. 13        }
  14. 14        
  15. 15        public  Main(){
  16. 16                JFrame  frame  =  new  JFrame();
  17. 17                JPanel  panel  =  new  JPanel();
  18. 18
  19. 19                JButton  button1  =  new  JButton("Button 1");
  20. 20                button1.addActionListener(this);
  21. 21                
  22. 22                JButton  button2  =  new  JButton("Button 2");
  23. 23                button2.addActionListener(this);
  24. 24                
  25. 25                JButton  button3  =  new  JButton("Button 3");
  26. 26                button3.addActionListener(this);
  27. 27                
  28. 28                JToolBar  toolBar  =  new  JToolBar();
  29. 29                toolBar.add(button1);
  30. 30                toolBar.add(button2);
  31. 31                toolBar.add(button3);
  32. 32
  33. 33                panel.add(toolBar);
  34. 34                panel.setLayout(new  FlowLayout(FlowLayout.LEFT));
  35. 35                
  36. 36                frame.add(panel);
  37. 37                
  38. 38                frame.setSize(300,300);
  39. 39                frame.setVisible(true);
  40. 40        }
  41. 41        
  42. 42        public  void  actionPerformed(ActionEvent  e){
  43. 43                switch(e.getActionCommand()){
  44. 44                        case  "Button 1":
  45. 45                                System.out.println("You clicked Button 1");
  46. 46                                break;
  47. 47                        case  "Button 2":
  48. 48                                System.out.println("You clicked Button 2");
  49. 49                                break;
  50. 50                        case  "Button 3":
  51. 51                                System.out.println("You clicked Button 3");
  52. 52                                break;
  53. 53                                
  54. 54                }
  55. 55        }
  56. 56}

Multiple JButtons With A Single ActionListener

Jul 5, 2017 - 180 Views
This code snippet shows how to handle multiple JButtons with a single ActionListener. If your form consists of a few buttons, you can use one ActionListener to handle the operations of these buttons. The getActionCommand() can be used to determine which button was pressed.

Listing 1

  1. 1import  javax.swing.JFrame;
  2. 2import  javax.swing.JPanel;
  3. 3import  javax.swing.JButton;
  4. 4import  java.awt.event.ActionListener;
  5. 5import  java.awt.event.ActionEvent;
  6. 6
  7. 7class  Main  implements  ActionListener{
  8. 8        
  9. 9        public  static  void  main(String  args[]){
  10. 10                Main  main  =  new  Main();
  11. 11        }
  12. 12        
  13. 13        public  Main(){
  14. 14                JFrame  frame  =  new  JFrame();
  15. 15                JPanel  panel  =  new  JPanel();
  16. 16                
  17. 17                JButton  button1  =  new  JButton();
  18. 18                button1.setText("Button 1");
  19. 19                button1.addActionListener(this);
  20. 20                
  21. 21                JButton  button2  =  new  JButton();
  22. 22                button2.setText("Button 2");
  23. 23                button2.addActionListener(this);
  24. 24                
  25. 25                panel.add(button1);
  26. 26                panel.add(button2);
  27. 27                
  28. 28                frame.getContentPane().add(panel);
  29. 29                
  30. 30                frame.setSize(200,200);
  31. 31                frame.setVisible(true);
  32. 32        }
  33. 33        
  34. 34        public  void  actionPerformed(ActionEvent  e){
  35. 35                switch(e.getActionCommand()){
  36. 36                        case  "Button 1":
  37. 37                                System.out.println("You clicked Button 1");
  38. 38                                break;
  39. 39                        case  "Button 2":
  40. 40                                System.out.println("You clicked Button 2");
  41. 41                                break;
  42. 42                }
  43. 43        }
  44. 44}

Simple JButton Click Example

Jul 5, 2017 - 42 Views
This code snippet shows how to add an action listener to a JButton.

Listing 1

  1. 1import  javax.swing.JFrame;
  2. 2import  javax.swing.JPanel;
  3. 3import  javax.swing.JButton;
  4. 4import  java.awt.event.ActionListener;
  5. 5import  java.awt.event.ActionEvent;
  6. 6
  7. 7class  Main{
  8. 8        
  9. 9        public  static  void  main(String  args[]){
  10. 10                
  11. 11                JFrame  frame  =  new  JFrame();
  12. 12                JPanel  panel  =  new  JPanel();
  13. 13                
  14. 14                JButton  button  =  new  JButton();
  15. 15                button.setText("Click me");
  16. 16                button.addActionListener(new  ActionListener(){
  17. 17                        public  void  actionPerformed(ActionEvent  e){
  18. 18                                System.out.println("Hello World");        
  19. 19                        }
  20. 20                });
  21. 21                
  22. 22                panel.add(button);
  23. 23                
  24. 24                frame.getContentPane().add(panel);
  25. 25                
  26. 26                frame.setSize(200,200);
  27. 27                frame.setVisible(true);
  28. 28        }
  29. 29}

An alternative to the above code is to have the Main class implement an ActionListener as show in Listing 2 below.

Listing 2

  1. 1import  javax.swing.JFrame;
  2. 2import  javax.swing.JPanel;
  3. 3import  javax.swing.JButton;
  4. 4import  java.awt.event.ActionListener;
  5. 5import  java.awt.event.ActionEvent;
  6. 6
  7. 7class  Main  implements  ActionListener{
  8. 8        
  9. 9        public  static  void  main(String  args[]){
  10. 10                Main  main  =  new  Main();
  11. 11        }
  12. 12        
  13. 13        public  Main(){
  14. 14                JFrame  frame  =  new  JFrame();
  15. 15                JPanel  panel  =  new  JPanel();
  16. 16                
  17. 17                JButton  button  =  new  JButton();
  18. 18                button.setText("Click me");
  19. 19                button.addActionListener(this);
  20. 20                
  21. 21                panel.add(button);
  22. 22                
  23. 23                frame.getContentPane().add(panel);
  24. 24                
  25. 25                frame.setSize(200,200);
  26. 26                frame.setVisible(true);
  27. 27        }
  28. 28        
  29. 29        public  void  actionPerformed(ActionEvent  e){
  30. 30                System.out.println("Hello World");        
  31. 31        }
  32. 32}

If a class implements an ActionListener, it must then implement the actionPerformed method as a class method.

Simple JFrame Example

Jul 5, 2017 - 51 Views
This code snippet demonstrates how to show a JFrame with a JLabel.

Listing 1

  1. 1import  javax.swing.JFrame;
  2. 2import  javax.swing.JPanel;
  3. 3import  javax.swing.JLabel;
  4. 4
  5. 5class  Main{
  6. 6        
  7. 7        public  static  void  main(String  args[]){
  8. 8                
  9. 9                JFrame  frame  =  new  JFrame();
  10. 10                JPanel  panel  =  new  JPanel();
  11. 11                
  12. 12                JLabel  label  =  new  JLabel();
  13. 13                label.setText("Hello World");
  14. 14                
  15. 15                panel.add(label);
  16. 16                
  17. 17                frame.add(panel);
  18. 18                
  19. 19                frame.setSize(200,200);
  20. 20                frame.setVisible(true);
  21. 21        }
  22. 22}