Reputation: 788
I have created simple gui application using JFilechooser
for opening a pdf file. The gui has a browse button and a textArea to visualize the contents of the file.
I have created two classes: Gui(contains main()) and GuiJFrame to implement the gui, handlers and listeners. I have managed to get the window app but the browse button doesn't seem to work. I don't know where I have made a mistake, please help me
import java.awt.EventQueue;
public class Gui {
/** Launch the application. */
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Gui window = new Gui();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/** Create the application. */
public Gui() {
initialize();
}
/** Initialize the contents of the frame. */
private void initialize() {
GuiJFrame guiJFrame = new GuiJFrame();
guiJFrame.setVisible(true);
}
}
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class GuiJFrame extends JFrame {
private JButton btnBrowse;
private JTextArea log, filtered_log;
public GuiJFrame() {
this.setTitle("TikaExtractorGui");
this.setBounds(100, 100, 700, 800);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout(0, 0));
JPanel panel_1 = new JPanel();
this.getContentPane().add(panel_1, BorderLayout.NORTH);
JPanel panel_2 = new JPanel();
this.getContentPane().add(panel_2, BorderLayout.CENTER);
/*
* BUTTONS *
*/
JButton btnBrowse = new JButton("Browse");
panel_1.add(btnBrowse);
/*
* Text_Areas*
*/
JTextArea log = new JTextArea(50, 30);
panel_2.add(log);
log.setEditable(false);
/*
* LAYOUT *
*/
setLayout(new FlowLayout());
add(panel_1, FlowLayout.CENTER);
add(panel_2, FlowLayout.LEFT);
JScrollPane logScrollPane1 = new JScrollPane(log);
logScrollPane1.setSize(300, 300);
add(logScrollPane1);
/*
* Setting the handlers *
*/
ActionHandler a_handler = new ActionHandler();
btnBrowse.addActionListener(a_handler);
}
private class ActionHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
TikaExtractorInterface ex = new TikaExtractor();
PDFParser parser = new PDFParser();
String g = null;
if (event.getSource() == btnBrowse) {
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(log);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
if (file.getName().endsWith("pdf")) {
file.getPath();
if (file != null) {
try {
g = ex.extractFromFile(parser, file);
log.setText(g);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
}
}
Upvotes: 2
Views: 404
Reputation: 22084
this:
if(event.getSource() == btnBrowse)
should be
if(event.getSource().equals(btnBrowse))
You can't use the ==
to identify an equal object in java, you always have to use equals()
to make sure that two objects are the same.
This:
JButton btnBrowse = new JButton("Browse");
should be
btnBrowse = new JButton("Browse");
You are shadowing your class member variable with a local one so the if clause always comapres against a null value. The btnBrowse is never stored in your class.
Upvotes: 2
Reputation: 4239
I would say add a System.out.println at the entry of actionPerformed method and check if that is being called indeed.
Also its better to use action commands for each button, so that you don't have to check for equality of event source. Something like this:
btnBrowse.setActionCommand("Browse"); //before attaching the listener
and then in the actionPerformed
String actionCommand = event.getActionCommand();
if("Browse".equals(actionCommand)) {
JFileChooser fileChooser = new JFileChooser();
int retVal = fileChooser.showOpenDialog(null);
}
Upvotes: 1
Reputation: 17461
I know why it is not working.
Change
/* BUTTONS *
* */
JButton btnBrowse = new JButton("Browse");
panel_1.add(btnBrowse);
to
/* BUTTONS *
* */
btnBrowse = new JButton("Browse");
panel_1.add(btnBrowse);
Upvotes: 5
Reputation: 3602
Browse button should be given as argument for filechooser.
int returnVal = fc.showOpenDialog(log);
should be,
int returnVal = fc.showOpenDialog(btnBrowse);
Upvotes: 0