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();  
   }  
 }  

Tuesday, August 5, 2014

How to deploy and use WSO2 ESB Cloud Connector

In this post I describe steps on how to upload a connector to WSO2 ESB, creating a proxy and sending a request to test the functionality of the connector.


Prerequisite


  • Have a bug free built connector zip file to upload to the ESB.
  • Have an instance of WSO2 ESB server up and running.
  • SOAP UI version 4.5 or above to test the request.


Uploading and enabling the connector in ESB

Assuming you have an up and running instance of the WSO2 ESB server, follow the below steps.
















Click on Choose File button and browse the connector zip file you have.
















Then click on Upload. Note if the upload is successful, the connector file name will be shown in the list and its status will be marked in red as disabled as shown below.









Click on the Status icon to make it enable.










Note:

If you get an error message while uploading the connector zip file saying the upload failed, Don't worry just refresh the page  few times and retry. If that is not caused by an error thrown by ESB server, this should work for you.

Also, you may need to wait for few seconds to enable the connector after it is uploaded. Note that if you cannot enable the connector even after few attempts, your connector may have issues, you can observe the ESB server log to check whether any exceptions are thrown. If so, carefully handle them in your connector, then build and upload again.


Creating a proxy and getting the WSDL 

Now we are done with the connector file and need to create a proxy to map the specific function call that we require to test.












You can observe there are several types of proxies shown as above. You can use Custom Proxy to define your own proxy file.

So, Click on Custom Proxy link and switch to source view from where you can handle the source of the proxy easily.

For the demonstrations purpose I am using the Github connector file and Ill focus on getPullRequest method. An example proxy file is shown below.















Proxy file content:



































Click on Save. If you created the proxy with no errors, the proxy file will be listed as shown below.
















If you face any error while creating the proxy, fix it and save again.

Now, to get the WSDL, you have to simply click on the WSDL1.1 link of the specific proxy.










Making a request through SOAP UI

In the final step, you can use the above WSDL to create a request and make an API call through the WSO2 ESB

Note that I will show how to create a REST request call since I deal with the Github REST API, You can customize your Proxy and the request in any type that suits your requirement (REST(JSON/XML), SOAP etc)

Create a New REST project in SOAP UI using the above WSDL.


















Now build the REST request with the necessary parameter and valid values as shown below. Send the request and observe the output






















Hope it was useful for you. If so, please leave a comment.

Thursday, June 12, 2014

Visual Studio Add-in creation tips : Cannot see the created add-in under Add-in manager in Visual Studio

Problem :


I made an add-in in visual studio and it is visible in any instance of visual studio under Add-in Manager. but if i want to work with the project having a copy of it to another machine, what should be done to make it visible under the add-in manager.

basically, i want be able to let VS of any of my PCs to detect the add-ins that i have created in it or copied from some other machine.

















Solution : 


You need to register the assembly as add-in for VS using an .AddIn file in some of the well-known locations.

The .AddIn file for add-ins that you create are typically in the folder C:\Users\<user>\Documents\Visual Studio <version>\AddIns.















Inside the .AddIn file there is a <Assembly> tag whose value you need to adjust if needed to point to the assembly path. (Open the AddIn file in a text editor)














Note the highlighted line in the above image. The location that mentioned within the Assembly tag should point to the .dll file of the addIn that you have created. Then the next time you open Visual Studio, your addIn will be visible.
























Hope it was a useful tip to you.

Sunday, June 1, 2014

Visual Studio Add-in creation tips : How to change the default icon of an add-in?

Problem :

When you create an add-in Visual Studio, you will see the default set icon as a smiley. May be your requirement needs some other proper icon to be used. The question is how to change the default add-in icon easily.














Solution :

-Open a solution that was created by using the Add-in project type.
-Open the Connect.cs file and navigate to the below shown line in OnConnection() method.











Note the number "59" in the line where the Command object is initialized. This is considered as the index of the current icon of the add-in. You can change this number to a different standard icon to get a new icon.

It is simple as that. Further more you can refer the below shown list to identify which icon stands for which number you specify.







You can see the complete list from the below reference :