I am using Intellij Idea 2020.2.1 IDE for my project.
I design a simple screen which includes a file chooser to select a file.
I made it but I am not sure it is proper way to make it. I created a GUI form via "Swing UI Designer->GUI FORM". But I couldn't use this variables for screen. I need to create another Jbutton, jPanel etc.
Here is my GUI Form screen looks like.
And this my code.
public class SubtitleUploadPage implements ActionListener {
private JButton SelectSubtitleFileButton;
private JPanel fileUploadPanel;
static JLabel pathText;
public SubtitleUploadPage() {
}
public static void main(String[] args) {
JFrame jFrame = new JFrame("File Select Screen");
jFrame.setSize(400, 400);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton selectSubtitleButton = new JButton("Select File");
SubtitleUploadPage subtitleUploadPage = new SubtitleUploadPage();
selectSubtitleButton.addActionListener(subtitleUploadPage);
JPanel fileUploadPanel = new JPanel();
fileUploadPanel.add(selectSubtitleButton);
pathText = new JLabel("no file selected");
fileUploadPanel.add(pathText);
jFrame.add(fileUploadPanel);
jFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String com = e.getActionCommand();
if (com.equals("Select File")) {
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
j.setAcceptAllFileFilterUsed(false);
j.setDialogTitle("Select a .srt file");
FileNameExtensionFilter restrict = new FileNameExtensionFilter("Only .srt files", "srt");
j.addChoosableFileFilter(restrict);
int r = j.showOpenDialog(null);
if (r == JFileChooser.APPROVE_OPTION) {
pathText.setText(j.getSelectedFile().getAbsolutePath());
}
else
pathText.setText("the user cancelled the operation");
}
}
}
And here is the result. As you can see it works as intended.
But as you can see I created Jbutton and Jpanel again. So I can't use pre-created ones.
When I try to change pre-defined Jpanel to static like:
private static JPanel fileUploadPanel;
It gives error "Cannot bind: field is static: com.convert.utf8.SubtitleUploadPage.fileUploadPanel"
If I didn't insert static keyword I can't use this Jpanel directly because main method is static.
I know my code works but I think it is not the proper way to make it. I searched it a lot I found some examples but they extends JFrame and I am pretty sure it is not the correct way either.
Could someone please show me the correct way to use a GUI element (Jbutton, Jpanel etc.) from GUI Form directly? Thanks in advance
I figure it out finally. Here is the code which I want to create at first place.
public class SubtitleUploadPage {
private JButton SelectSubtitleFileButton;
private JPanel fileUploadPanel;
private JLabel fileLocationLabel;
public SubtitleUploadPage() {
SelectSubtitleFileButton.addActionListener(e -> {
String com = e.getActionCommand();
System.out.println(com);
if (com.equals("Select File")) {
JFileChooser fileChooser = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
fileChooser.setAcceptAllFileFilterUsed(false);
fileChooser.setDialogTitle("Select a .srt file");
FileNameExtensionFilter restrict = new FileNameExtensionFilter("Only .srt files", "srt");
fileChooser.addChoosableFileFilter(restrict);
int dialogResult = fileChooser.showOpenDialog(null);
if (dialogResult == JFileChooser.APPROVE_OPTION) {
fileLocationLabel.setText(fileChooser.getSelectedFile().getAbsolutePath());
}
else
fileLocationLabel.setText("the user cancelled the operation");
}
});
}
public static void main(String[] args) {
JFrame jFrame = new JFrame("File Select Screen");
jFrame.setContentPane(new SubtitleUploadPage().fileUploadPanel);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.pack();
jFrame.setVisible(true);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.