Add Browse button to GUI
Before starting :
- WindowBuilder Pro - should have been added to eclipse
- Create a new project
- Inside your package , Rt ck >New >Other>WindowBuilder-Swing Designer-(Select) Application window.
- Once the workspace is created , at the bottom a new tab "Design" will be created. Select that.
- Now in the Layout select "Spring Layout" or "Mig Layout" - this would place the relative and child properties close .
- Now in components > click JButton and click on UI panel where you want it to be placed.
- Now right click on the placed element and select Add>Event handler>Mouse >Mouse clicked.
- Once done you will be taken to the code, an Anonymous class will be created on JButton.
- Come back to design .
- In components >select JFile Chooser and place it on the panel.
- Once done go back to the source and find JFile Chooser code and put it inside the anonymous inner class of JButton created before.
- Now add JFileChoose_object.showOpenDialog(class_name.this);
- Now run the same
Code Here :
JButton btnBrowse = new JButton("Browse");
btnBrowse.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JFileChooser Filechoose=new JFileChooser();
int retval=Filechoose.showOpenDialog(GUI2.this);
if (retval == JFileChooser.APPROVE_OPTION) {
//... The user selected a file, get it, use it.
File file = Filechoose.getSelectedFile();
System.out.println(file.getPath());
}
}
});
btnBrowse.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JFileChooser Filechoose=new JFileChooser();
int retval=Filechoose.showOpenDialog(GUI2.this);
if (retval == JFileChooser.APPROVE_OPTION) {
//... The user selected a file, get it, use it.
File file = Filechoose.getSelectedFile();
System.out.println(file.getPath());
}
}
});
No comments:
Post a Comment