Tuesday, August 26, 2014

Java :Simple GUI (Button activating an Edit Field)

Java :Simple GUI (Button activating an Edit Field) 

Steps for creating new GUI :
  1. Make sure you have added Windows Builder Pro addin to your Eclipse
  2. Select the immediate parent location where you want to create a new GUI file.
  3. Rt click>new>other>Window Builder > Swing Designer >Select Application Window.
  4. Click on Next
  5. In Create application window give a Name in "Name"box.
  6. Click on Finish
Refer : http://catchbug.blogspot.in/2014/02/java-add-browse-button-to-gui.html

 // Use JDialog instead of JFrame for thread to pause until an event


Code for gui


import java.awt.EventQueue;
public class gui {

 JFrame frame;
 JButton btnNewButton ;
 JTextArea textArea;
 SpringLayout springLayout;
 JTextPane textPane;

// gui(){ initialize();}

public void initialize() {
 
 frame = new JFrame();
 frame.setBounds(100, 100, 450, 300);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 springLayout = new SpringLayout();
 frame.getContentPane().setLayout(springLayout);
 
 btnNewButton = new JButton("New button");
 springLayout.putConstraint(SpringLayout.NORTH, btnNewButton, 113, SpringLayout.NORTH, frame.getContentPane());
 springLayout.putConstraint(SpringLayout.WEST, btnNewButton, 132, SpringLayout.WEST, frame.getContentPane());
 frame.getContentPane().add(btnNewButton);
 
 textPane = new JTextPane();
 textPane.setText("Hi");

 btnNewButton.addActionListener(new ActionListener(){ 
  public void actionPerformed(ActionEvent e){ 
   
   if(textPane.isDisplayable()==false){
    frame.getContentPane().add(textPane); 
    springLayout.putConstraint(SpringLayout.NORTH, textPane, 31, SpringLayout.SOUTH, btnNewButton);
    springLayout.putConstraint(SpringLayout.WEST, textPane, 148, SpringLayout.WEST, frame.getContentPane());
    springLayout.putConstraint(SpringLayout.EAST, textPane, 248, SpringLayout.WEST, frame.getContentPane());
   }
   else    
    frame.getContentPane().remove(textPane);
 
    frame.validate();
    frame.repaint();
 
   }});

}
}
Code calling GUI
 class Test {

  public static void main(String[] args) {

   gui obj=new gui();
   obj.initialize();
   obj.frame.setVisible(true);
     }

No comments:

Post a Comment