Help With JavaSwing - JFileChooser
I have created a JButton which on actionevent calls a constructor of a second class in a separate file. The second file contains a 'select file' button which on clicking should open up a dialogbox for the user to select a file. I have used Jfilechooser for this purpose. The problem is When I click on 'select file' nothing happens. The following two are the respective files.
Ex.java
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Ex extends JFrame {
JPanel p;
JButton bound;
public static void main(String [] args) {
Ex m=new Ex();
}
public Ex() {
p = new JPanel();
bound=new JButton("Bound");
p.add(bound);
this.getContentPane().add(p);
this.setSize(500,500);
this.setVisible(true);
JBound b1=new JBound(); bound.addActionListener(b1);
}
class JBound implements ActionListener {
public void actionPerformed(ActionEvent bdy) {
Object obj3=bdy.getSource();
if(obj3==bound) {
Boundary bd=new Boundary();
}
}
}
}
Boundary.java
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.File;
public class Boundary {
JFrame frame;
JButton button;
JPanel p1;
public Boundary() {
frame = new JFrame("JComboBox Test");
button = new JButton("Select File");
p1=new JPanel();
button.setBounds(700,170,110,27); p1.add(button);
p1.setLayout(null);
p1.setBackground(Color.lightGray);
frame.getContentPane().add(p1);
frame.pack();
frame.setVisible(true);
JBrowse bro = new JBrowse();
button.addActionListener(bro);
}
public class JBrowse implements ActionListener {
public void actionPerformed(ActionEvent bu) {
Object obj8=bu.getSource();
if(obj8==button) {
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println(selectedFile.getName());
}
}
}
}
}
Any help? And I am using the JFileChooser to open and display the image chosen. So if possible, can someone please tell me how to do that too..?