I have a Java assignment in which I need to build a program for exchanging information between students. I am using IntelliJ IDEA and a plugin called JFormDesigner for designing the GUI. I want do display a file chooser when clicking on a button. I have tried different implementations but non of them works. Nothing is displayed on the screen when clicking the button I was wondering if someone could help me find a solution.
This is the button code:
ChooseButton1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
ChooseButton1MouseClicked(e);
}
});
This is the method where actions are implemented:
private void ChooseButton1MouseClicked(MouseEvent e) {
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION)
{
File selectedFile = fileChooser.getSelectedFile();
System.out.println(selectedFile.getName());
}
}
Here is test code:
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;
import java.io.*;
public class Test extends JPanel {
private JButton ChooseButton;
public Test() {
initComponents();
}
private void ChooseButtonActionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION)
{
File selectedFile = fileChooser.getSelectedFile();
System.out.println(selectedFile.getName());
}
}
private void initComponents() {
ChooseButton = new JButton();
setBorder(new javax.swing.border.CompoundBorder(
new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 0),
"JFormDesigner Evaluation", javax.swing.border.TitledBorder.CENTER,
javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dialog", java.awt.Font.BOLD, 12),
java.awt.Color.red), getBorder())); addPropertyChangeListener(new java.beans.PropertyChangeListener(){public void propertyChange(java.beans.PropertyChangeEvent e) {
if("border".equals(e.getPropertyName())) throw new RuntimeException();
}
});
//---- ChooseButton ----
ChooseButton.setText("Choose");
ChooseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ChooseButtonActionPerformed(e);
}
});
GroupLayout layout = new GroupLayout(this);
setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup()
.addGroup(layout.createSequentialGroup()
.addGap(155, 155, 155)
.addComponent(ChooseButton)
.addContainerGap(175, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup()
.addGroup(layout.createSequentialGroup()
.addGap(125, 125, 125)
.addComponent(ChooseButton)
.addContainerGap(143, Short.MAX_VALUE))
);
}
public void main(String[] args) {
Runnable runnable = new Runnable() {
@Override public void run() {
new Test();
}
};
EventQueue.invokeLater(runnable);
}
}
Choose another listener
ChooseButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ChooseButton1MouseClicked(e);
}
});