Java Swing Applications Tips and Tricks

                  Java is very good in creating desktop applications but creating UI using java Java Swing Applications Tips and Tricks program is bit complicated.Few things we need to consider always while writing code for UI.here we will see some useful tips and java swing applications.
 
1.

How to display a JDialog in Center of JFrame
             Use following method to display JDialog in center of JFrame(parent frame).It will most useful while writing swing applications.

 

frame.setLocationRelativeTo (null);

 
   1:  

   2: import java.awt.FlowLayout;

   3: import java.awt.event.ActionEvent;

   4: import java.awt.event.ActionListener;

   5: import javax.swing.JButton;

   6: import javax.swing.JFrame;

   7: import javax.swing.JPanel;

   8: public class ParentConn implements ActionListener {

   9: private JButton jb;

  10: private JFrame jf;

  11: public ParentConn() {

  12: jf = new JFrame();

  13: jf.setSize(400,200);

  14: jb = new JButton("Click me");

  15: jb.addActionListener(this);

  16: JPanel jp = new JPanel(new FlowLayout());

  17: jp.add(jb);

  18: jf.add(jp);

  19: jf.setVisible(true);

  20: }

  21: public static void main(String[] args) {

  22: ParentConn obj = new ParentConn();

  23: }

  24: public void actionPerformed(ActionEvent e) {

  25: String com = e.getActionCommand();

  26: if(com.equalsIgnoreCase("Click me"))

  27: {

  28: JFrame anotherfr = new JFrame();

  29: anotherfr.setVisible(true);

  30: anotherfr.setSize(200,100);

  31: //anotherfr.setLocationRelativeTo(null);

  32: anotherfr.setLocationRelativeTo(jf);

  33: }

  34: }

  35: }

2.    How to add or remove Look and feel for Swing Applications.

   1: JFrame.setDefaultLookAndFeelDecorated(true);

   2: setUndecorated

public void setUndecorated (boolean undecorated)

Disables or enables decorations for this frame. This method can only be called while the frame is not displayable.

3.

How to connect JDialog to Parent or Owner Window

public JDialog(Dialog owner,

String title)

Creates a modeless dialog with the specified title and with the specified owner dialog.

This constructor sets the component’s locale property to the value returned by JComponent.getDefaultLocale.

Parameters:

Owner – the owner Dialog from which the dialog is displayed or null if this dialog has no owner

Title – the String to display in the dialog’s title bar.
4.How to create Non – Editable Cells in JTable?

JTable Non –Editable cells.

5.How to add JButton in JTable ColumnHeader.6.

How to display JButton as Icon?

            Use following methods.
   1: jb.setBorderPainted(false);

   2: jb.setContentAreaFilled(false);

7.How to Exit Swing Application Properly?
Close the swing application properly and no need to press CTRL + C after that.

   1:  

   2: // JFrame f;

   3: f.addWindowListener(new WindowAdapter() {

   4:  public 

   5: void windowClosing(WindowEvent we) { System.exit(0); }

   6: });

8.load GIF, JPEG, and (in SDK 1.3) PNG images 
   1: Image i = new javax.swing.ImageIcon("tarsier.png").getImage();

instead of

   1: usingToolkit'sgetImage()method:

9.getResourceAsStream() For Portability instead of 

   1:  

   2: InputStream in = FileInputStream("images/tarsier.png");

10 Use Java Swing Development Tools

Related posts:

  1. How to Display JTable with Horizontal and Vertical Scrollbars
  2. Java Swing Development Tools

Comments are closed.