Thursday, April 28, 2016

How to set the Windows look and feel for a Swing application

In this post I am going to show you how to change the default look and feel of a Swing application to a Windows look.

Before











After











Below is the sample code


   /*  
   * This method sets the current System's look and feel for the GUI  
   *  
   * */  
   public void setLookAndFeel(){  
     try {  
       // Set System Look and Feel  
       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());  
     }  
     catch (UnsupportedLookAndFeelException e) {  
       errorString = e.getMessage();  
       e.printStackTrace();  
     }  
     catch (ClassNotFoundException e) {  
       errorString = e.getMessage();  
       e.printStackTrace();  
     }  
     catch (InstantiationException e) {  
       errorString = e.getMessage();  
       e.printStackTrace();  
     }  
     catch (IllegalAccessException e) {  
       errorString = e.getMessage();  
       e.printStackTrace();  
     }  
   }  


Hope it was useful to you.

Wednesday, April 27, 2016

Creating a Toolbar in Swing

In my earlier post I described how to add a JMenuBar with menu items and sub menu items. I am going to show how to add a tool bar to the same application under the menu bar as shown below.













I have highlighted the code to add the tool bar to the same JFrame.

 import javax.swing.*;  
 import java.awt.*;  
 public class ToolBarExampleMain extends JFrame{  
   public ToolBarExampleMain(){  
     super("Tool Bar Example App");           //sets the title for the JFrame  
     this.createInterface();  
   }  
   public void createInterface(){  
     setLayout(new BorderLayout());           //main Layout of the JFrame is BorderLayout  
     Toolkit tk = Toolkit.getDefaultToolkit();  
     int xSize = ((int) tk.getScreenSize().getWidth());  
     int ySize = ((int) tk.getScreenSize().getHeight());  
     setSize(xSize,ySize);                //setting the JFrame size according to the screen width and height  
     JPanel mainPanel = new JPanel(new BorderLayout());  
     mainPanel.add(createMenuBar(),BorderLayout.NORTH); //creating a JMenuBar and adding it on top of the frame  
     mainPanel.add(createToolBar(),BorderLayout.LINE_START);  
     add(mainPanel, BorderLayout.CENTER);  
     setVisible(true);  
   }  
   public JMenuBar createMenuBar(){  
     JMenuBar menuBar=new JMenuBar();  
     JMenu menuFile=new JMenu("File");          //File,Edit,View and Help are the main menus in the JMenuBar  
     JMenu menuEdit=new JMenu("Edit");  
     JMenu menuView=new JMenu("View");  
     JMenu menuHelp=new JMenu("Help");  
     JMenuItem subMenuItemOpen=new JMenuItem("Open");  //Open and Exit are the sub menu items under 'File' menu item  
     JMenuItem subMenuItemExit=new JMenuItem("Exit");  
     menuFile.add(subMenuItemOpen);  
     menuFile.add(subMenuItemExit);  
     menuBar.add(menuFile);  
     menuBar.add(menuEdit);  
     menuBar.add(menuView);  
     menuBar.add(menuHelp);  
     return menuBar;  
   }  
   public static void main(String[] args){  
     ToolBarExampleMain main=new ToolBarExampleMain();  
   }  
   public JPanel createToolBar(){  
     JToolBar toolBar=new JToolBar();  
     toolBar.setRollover(true);  
     //buttons required for the tool bar  
     JButton btnMoveUp;  
     JButton btnMoveDown;  
     JButton btnAdd;  
     JButton btnRemove;  
     JButton btnComment;  
     JButton btnMessageBox;  
     //icon which sets an image to each of the butons in the tool bar  
     Icon btnIcon;  
     //this JPanel will hold the tool bar  
     JPanel toolBarPanel=new JPanel(new FlowLayout(FlowLayout.LEFT));  
     //Designing the Up Arrow button  
     btnIcon=new ImageIcon(getClass().getResource("\\icons\\upArrow.png"));  
     btnMoveUp=new JButton(btnIcon);  
     //setting a tool tip for the button  
     btnMoveUp.setToolTipText("Move up ");  
     //adding the button to the toolbar  
     toolBar.add(btnMoveUp);  
     //adding a separator after the button  
     toolBar.addSeparator();  
     //Designing the Down Arrow button  
     btnIcon=new ImageIcon(getClass().getResource("\\icons\\downArrow.png"));  
     btnMoveDown=new JButton(btnIcon);  
     btnMoveDown.setToolTipText("Move Down");  
     toolBar.add(btnMoveDown);  
     toolBar.addSeparator();  
     //Designing the Add button  
     btnIcon=new ImageIcon(getClass().getResource("\\icons\\add.png"));  
     btnAdd=new JButton(btnIcon);  
     btnAdd.setToolTipText("Add new Test Case");  
     toolBar.add(btnAdd);  
     toolBar.addSeparator();  
     //Designing the Remove button  
     btnIcon=new ImageIcon(getClass().getResource("\\icons\\remove.png"));  
     btnRemove=new JButton(btnIcon);  
     btnRemove.setToolTipText("Remove");  
     toolBar.add(btnRemove);  
     toolBar.addSeparator();  
     //Designing the Comment button  
     btnIcon=new ImageIcon(getClass().getResource("\\icons\\comment.png"));  
     btnComment=new JButton(btnIcon);  
     btnComment.setToolTipText("Comment");  
     toolBar.add(btnComment);  
     toolBar.addSeparator();  
     //Designing the Message Box button  
     btnIcon=new ImageIcon(getClass().getResource("\\icons\\messageBox.png"));  
     btnMessageBox=new JButton(btnIcon);  
     btnMessageBox.setToolTipText("Message Box");  
     toolBar.add(btnMessageBox);  
     toolBar.addSeparator();  
     toolBarPanel.add(toolBar);  
     return toolBarPanel;  
   }  
 }  
Note that you can add action listeners to the buttons in the tool bar as same as you do to JButtons.

Hope it helped you.

Adding a JFileChooser when clicking on a JMenuItem

In my previous post I showed how to add a JMenuBar to a Swing JFrame. In this post I will show you how to display a file chooser when clicking one of the items in the menu bar as below.

In this app, when the user selects the 'Open' menu tem from the menu bar then it will allow you to browse a location in your machine to select a file. Once you click on 'Exit' then the program exits.


















According to a code below, if the user selects a file or cancels the file selection, a message box will popup accordingly. You can perform any action in the appropriate code blocks as required.

 import javax.swing.*;  
 import java.awt.*;  
 import java.awt.event.ActionEvent;  
 import java.awt.event.ActionListener;  
 import java.io.File;  
 public class MenuBarExampleMain extends JFrame implements ActionListener{  
   private static String OPEN_COMMAND="open";  
   private static String EXIT_APP_COMMAND="exit";  
   public MenuBarExampleMain(){  
     super("Menu Bar Example App");           //sets the title for the JFrame  
     this.createInterface();  
   }  
   public void createInterface(){  
     setLayout(new BorderLayout());           //main Layout of the JFrame is BorderLayout  
     Toolkit tk = Toolkit.getDefaultToolkit();  
     int xSize = ((int) tk.getScreenSize().getWidth());  
     int ySize = ((int) tk.getScreenSize().getHeight());  
     setSize(xSize,ySize);                //setting the JFrame size according to the screen width and height  
     JPanel mainPanel = new JPanel(new BorderLayout());  
     mainPanel.add(createMenuBar(),BorderLayout.NORTH); //creating a JMenuBar and adding it on top of the frame  
     add(mainPanel, BorderLayout.CENTER);  
     setVisible(true);  
   }  
   public JMenuBar createMenuBar(){  
     JMenuBar menuBar=new JMenuBar();  
     JMenu menuFile=new JMenu("File");          //File,Edit,View and Help are the main menus in the JMenuBar  
     JMenu menuEdit=new JMenu("Edit");  
     JMenu menuView=new JMenu("View");  
     JMenu menuHelp=new JMenu("Help");  
     JMenuItem subMenuItemOpen=new JMenuItem("Open");  //Open and Exit are the sub menu items under 'File' menu item  
     subMenuItemOpen.setActionCommand(OPEN_COMMAND);  
     subMenuItemOpen.addActionListener(this);  
     JMenuItem subMenuItemExit=new JMenuItem("Exit");  
     subMenuItemExit.setActionCommand(EXIT_APP_COMMAND);  
     subMenuItemExit.addActionListener(this);  
     menuFile.add(subMenuItemOpen);  
     menuFile.add(subMenuItemExit);  
     menuBar.add(menuFile);  
     menuBar.add(menuEdit);  
     menuBar.add(menuView);  
     menuBar.add(menuHelp);  
     return menuBar;  
   }  
   public static void main(String[] args){  
     MenuBarExampleMain main=new MenuBarExampleMain();  
   }  
   @Override  
   public void actionPerformed(ActionEvent e) {  
     String command = e.getActionCommand();  
     if (OPEN_COMMAND.equals(command)) {  
       browseFiles();  
     }else if (EXIT_APP_COMMAND.equals(command)) {  
       exitProgram();  
     }  
   }  
   //opens a JFileChooser to browse XML files to parse to the application  
   public void browseFiles(){  
     final JFileChooser fileChooser = new JFileChooser();  
     //allows user to select more than one file  
     fileChooser.setMultiSelectionEnabled(true);  
     int returnVal = fileChooser.showOpenDialog(null);  
     String fileExtension;  
     //if the user confirms file selection display a message  
     if (returnVal == JFileChooser.APPROVE_OPTION) {  
       JOptionPane.showMessageDialog(null, "User made the selection");  
     }  
     else{  
       JOptionPane.showMessageDialog(null, "User canceled the selection");  
     }  
   }  
   public void exitProgram(){  
     System.exit(0);  
   }  
 }  

Adding a Menu with sub menus in Swing

In this blog post I am going to show you how to add the below JMenuBar to a JFrame in a Swing application.









Below is the commented code for the application. I will explain how to add ActionListeners to these menu items once clicked in another post separately.

 import javax.swing.*;  
 import java.awt.*;  
 public class MenuBarExampleMain extends JFrame{  
   public MenuBarExampleMain(){  
  //sets the title for the JFrame  
     super("Menu Bar Example App");             
     this.createInterface();  
   }  
   public void createInterface(){  
  //main Layout of the JFrame is BorderLayout  
     setLayout(new BorderLayout());             
     Toolkit tk = Toolkit.getDefaultToolkit();  
     int xSize = ((int) tk.getScreenSize().getWidth());  
     int ySize = ((int) tk.getScreenSize().getHeight());  
  //setting the JFrame size according to the screen width and height  
     setSize(xSize,ySize);                  
     JPanel mainPanel = new JPanel(new BorderLayout());  
  //creating a JMenuBar and adding it on top of the frame  
     mainPanel.add(createMenuBar(),BorderLayout.NORTH);   
     add(mainPanel, BorderLayout.CENTER);  
     setVisible(true);  
   }  
   public JMenuBar createMenuBar(){  
     JMenuBar menuBar=new JMenuBar();  
  //File,Edit,View and Help are the main menus in the JMenuBar  
     JMenu menuFile=new JMenu("File");            
     JMenu menuEdit=new JMenu("Edit");  
     JMenu menuView=new JMenu("View");  
     JMenu menuHelp=new JMenu("Help");  
  //Open and Exit are the sub menu items under 'File' menu item  
     JMenuItem subMenuItemOpen=new JMenuItem("Open");    
     JMenuItem subMenuItemExit=new JMenuItem("Exit");  
     menuFile.add(subMenuItemOpen);  
     menuFile.add(subMenuItemExit);  
     menuBar.add(menuFile);  
     menuBar.add(menuEdit);  
     menuBar.add(menuView);  
     menuBar.add(menuHelp);  
     return menuBar;  
   }  
   public static void main(String[] args){  
     MenuBarExampleMain main=new MenuBarExampleMain();  
   }  
 }