Need help using JFileChooser for the first time - java

To be blunt about it, I'm looking for help as to how to actually use it. We have just been set work using this on my course, but our new teacher does not teach, and I'm really struggling with this one. So I have a basic JFrame set up using windows builder, and the object is to be able to open a text file as a string and put it into the text space, and then be able to find strings in the text and change them. I'll paste the code I have below, if anyone can help explain how to do this, I'll really appriciate it, thanks. :)
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.TextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.filechooser.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class task1 extends JFrame {
private JPanel contentPane;
private JTextField findTxtBox;
private JButton findBtn;
private JTextField replaceTxtBox;
private JTextField fileTxtBox;
private JButton openBtn;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
task1 frame = new task1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public task1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 312);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
TextArea textArea = new TextArea();
textArea.setBounds(10, 45, 380, 160);
contentPane.add(textArea);
findTxtBox = new JTextField();
findTxtBox.setBounds(80, 211, 236, 20);
contentPane.add(findTxtBox);
findTxtBox.setColumns(10);
findBtn = new JButton("Find");
findBtn.setBounds(326, 210, 89, 23);
contentPane.add(findBtn);
JButton btnReplace = new JButton(" Replace");
btnReplace.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnReplace.setBounds(326, 240, 89, 23);
contentPane.add(btnReplace);
replaceTxtBox = new JTextField();
replaceTxtBox.setColumns(10);
replaceTxtBox.setBounds(80, 242, 236, 20);
contentPane.add(replaceTxtBox);
fileTxtBox = new JTextField();
fileTxtBox.setColumns(10);
fileTxtBox.setBounds(80, 11, 236, 20);
contentPane.add(fileTxtBox);
final JFileChooser fc = new JFileChooser();
fc.setFileFilter(new FileNameExtensionFilter("Text Files", "txt"));
fc.removeChoosableFileFilter(fc.getAcceptAllFileFilter());
openBtn = new JButton("Open File");
openBtn.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
fc.showOpenDialog(null);
}
});
openBtn.setBounds(326, 10, 89, 23);
contentPane.add(openBtn);
}
}

the object is to be able to open a text file as a string and put it into the text space, and then be able to find strings in the text and change them.
That involves a lot more than just using a file chooser. I suggest you start by reading the section from the Swing tutorial on How to Use File Choosers for a working example.
The file chooser is just used to get a file name not read a file. So next I would suggest you use a JTextArea (not a TextArea) to display the text from the file that you read. You can use the read(...) method of the JTextArea to do this.
All text components have a getText() method you can use the get the text. You can then search the string for whatever you want and replace the text by using the replace() method of a JTextArea.
Finally you should NOT be using the setBounds() method to set the size/location of a component. You should be using Layout Managers and let them do their job. The Swing tutorial also has a section on using layout managers.

Related

How to pass inputted data from JDialog to parent JFrame when user clicks button in JDialog?

How do I pass user-inputted date from a JDialog to the parent JFrame when the user clicks a certain button in the JDialog?
Here's how I want the program to work: When the user clicks a button in the JFrame, a JDialog pops up. The user then enters some data of various types (string and integer). If the user clicks an "Add Task" button, the data is passed back to the original JFrame, which will display the data, and the JDialog closes. If the user clicks the "Cancel" button, the data is discarded and the JDialog closes.
I thought about using JOptionPane, but I don't think it allows for data of various types. I thought about creating a method in the JFrame and calling it from the JDialog, but I don't know how to reference the JFrame. I thought about creating a variable in the JDialog, but I don't know to stop the JDialog from immediately passing an empty variable to the JFrame.
Any help?
Code for JFrame:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Dialog.ModalityType;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JDialog;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MainInterface extends JFrame {
private JPanel contentPane;
public MainInterface() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 400, 800);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton addTask = new JButton("Add");
addTask.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
NewTask newTask = new NewTask();
newTask.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
// Set window title
newTask.setTitle("Add Task");
newTask.setVisible(true);
}
});
addTask.setBounds(0, 728, 97, 25);
contentPane.add(addTask);
JButton modifyTask = new JButton("Modify");
modifyTask.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
modifyTask.setBounds(95, 728, 97, 25);
contentPane.add(modifyTask);
JButton deleteTask = new JButton("Delete");
deleteTask.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
deleteTask.setBounds(190, 728, 97, 25);
contentPane.add(deleteTask);
JButton settingMenu = new JButton("Settings");
settingMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Setting settings = new Setting();
settings.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
settings.setVisible(true);
settings.setTitle("Settings");
}
});
settingMenu.setBounds(285, 728, 97, 25);
contentPane.add(settingMenu);
}
}
The JFrame is launched by another class, so it doesn't have a main method.
Code for JDialog:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import org.omg.CORBA.PUBLIC_MEMBER;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.JSpinner;
import java.awt.event.ActionListener;
import java.util.jar.Attributes.Name;
import java.awt.event.ActionEvent;
public class NewTask extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField taskName;
public NewTask() {
setBounds(100, 100, 450, 600);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
{
JLabel lblNewLabel = new JLabel("Name:");
lblNewLabel.setBounds(24, 13, 38, 16);
contentPanel.add(lblNewLabel);
}
{
taskName = new JTextField();
taskName.setBounds(79, 10, 304, 22);
contentPanel.add(taskName);
taskName.setColumns(10);
}
JLabel lblNewLabel_1 = new JLabel("Time Required:");
lblNewLabel_1.setBounds(24, 58, 97, 16);
contentPanel.add(lblNewLabel_1);
JSpinner hourSpinner = new JSpinner();
hourSpinner.setBounds(125, 55, 44, 22);
contentPanel.add(hourSpinner);
JLabel lblNewLabel_2 = new JLabel("hours");
lblNewLabel_2.setBounds(175, 58, 44, 16);
contentPanel.add(lblNewLabel_2);
// Set maximum value for minutes to 59
int min = 0;
int max = 59;
int step = 1;
int i = 1;
SpinnerModel value = new SpinnerNumberModel(i, min, max, step);
JSpinner minuteSpinner = new JSpinner(value);
minuteSpinner.setBounds(225, 55, 44, 22);
contentPanel.add(minuteSpinner);
JLabel lblNewLabel_3 = new JLabel("minutes");
lblNewLabel_3.setBounds(281, 58, 56, 16);
contentPanel.add(lblNewLabel_3);
JLabel lblNewLabel_4 = new JLabel("Deadline:");
lblNewLabel_4.setBounds(24, 108, 56, 16);
contentPanel.add(lblNewLabel_4);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton addButton = new JButton("Add Task");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
addButton.setActionCommand("OK");
buttonPane.add(addButton);
getRootPane().setDefaultButton(addButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Close dialog window
dispose();
}
});
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
}
I thought about creating a method in the JFrame and calling it from
the JDialog, but I don't know how to reference the JFrame.
Even though this could be a nice solution, referencing the object who called the method is very discouraged.
See why (first answer): How to find the object that called a method in Java
One possible approach would be to create a method in the JFrame and adding a JFrame as a parameter for your newTask() function. Then, when invoking newTask() you pass this as an argument: newTask(this); .
Inside the modified newTask(JFrame frame) method, you just use the argument passed to reference the method in the parent JFrame.
This might seem the same as referencing the object who called the method, but it's not. Here you are passing as an argument the parent JFrame, which may not always be the object which invoked the method.
I hope I was clear enough, have a nice day!

Issues with ActionListener

I'm trying to write a program that asks you to click on a button to generate a random number and then makes the user try to guess what that number is.
Right now I'm working on getting a button click to generate a random number and to tell the user that the random number has been generated through a JTextPane. For whatever reason this part of the code doesn't seem to be executing correctly.
I have already instanced the object in the main class and I've added an actionlistener to the button. I have no idea what's causing this not to work appropriately:
package GUIs;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JToggleButton;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Principal extends JFrame {
//Elements
private JPanel contentPane;
private JTextField attemptField;
private JTextPane hint;
private JButton btnGenerate;
private JButton btnEnter;
//Events
private GenerateRandomNumber genRanNum;
//Misc variables
int ranNum = 1;
private JTextField textField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Principal frame = new Principal();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Principal() {
//Generates JPanel
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//Generates text field where the user will write the number
attemptField = new JTextField();
attemptField.setBounds(130, 59, 130, 26);
contentPane.add(attemptField);
attemptField.setColumns(10);
//Generates the hint
hint = new JTextPane();
hint.setBackground(SystemColor.window);
hint.setText("First, generate a new number!");
hint.setBounds(130, 110, 192, 19);
contentPane.add(hint);
//Generates the button that should call for the creation of the new number
btnGenerate = new JButton("Generate number");
btnGenerate.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
btnGenerate.setBounds(165, 153, 117, 29);
contentPane.add(btnGenerate);
btnGenerate.addActionListener(genRanNum);
//Generates button that will be used to enter the number
btnEnter = new JButton("Enter");
btnEnter.setBounds(269, 59, 58, 29);
contentPane.add(btnEnter);
}
public class GenerateRandomNumber implements ActionListener {
public void actionPerformed(ActionEvent e) {
ranNum = (int) (Math.random() * 101);
hint.setText("The number has been created!");
}
}
}
private GenerateRandomNumber genRanNum;
Your random number generator is null. I don't see where you ever create an instance of it.
btnGenerate.addActionListener(genRanNum);
The above statement does nothing because the variable is null. (ie. no ActionListener has been added to the button)
I don't even know how your GenerateRandomNumber class would compile because it doesn't have access to the "hint" variable of your "Principal" class.
Don't make the GenerateRandomNumber class a public class. Instead make it an inner class of the "Principal" class.
Read the section from the Swing tutorial on How to Use Actions for an example showing how to use inner classes. Note you can even use an Action instead of an ActionListener.

How do I output to textField?

I'm making a java program to turn any text into capital capital letters.
It worked fine with system.out.print but now I want to turn it into a GUI.
I tried getting the text from the original textField input, turn it into a double, turn the double into a string then I used addText but I don;t think it works.
Here is how it is supposed to work:
I write a bunch a text into a the input box then when I click the GO button it converts every character into capital letters. The output is supposed to to inside another TextField
here is my code (using Eclipse)
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Frame1 {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame1 window = new Frame1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Frame1() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 710, 508);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblInputTextTo = new JLabel("Input Text to Capitalise:");
lblInputTextTo.setFont(new Font("Segoe UI", Font.BOLD, 26));
lblInputTextTo.setBounds(210, 0, 289, 54);
frame.getContentPane().add(lblInputTextTo);
textField = new JTextField();
textField.setBounds(34, 77, 624, 150);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(34, 354, 613, 90);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
JButton btnGo = new JButton("GO!");
btnGo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double TextInput = Double.parseDouble(textField.getText());
String Str = String.valueOf(TextInput);
textField_1.setText(Str.toUpperCase());
textField_1.setEditable(false);
}
});
btnGo.setFont(new Font("Segoe UI", Font.BOLD, 18));
btnGo.setBounds(250, 239, 180, 23);
frame.getContentPane().add(btnGo);
JLabel lblOutput = new JLabel("Output:");
lblOutput.setFont(new Font("Segoe UI", Font.BOLD, 26));
lblOutput.setBounds(304, 300, 95, 43);
frame.getContentPane().add(lblOutput);
}}
I'm not done yet if you have some suggestion it would be nice.
You don't need to convert the input String into double to make it uppercase. I did below change to your program and it works now.
btnGo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Remove these 2 lines
//double TextInput = Double.parseDouble(textField.getText());
//String Str = String.valueOf(TextInput);
//Directly take input text, make it uppercase and set it in output field.
textField_1.setText(textField.getText().toUpperCase());
textField_1.setEditable(false);
}
});
Additional tip:
Don't do setLayout(null) and use setBounds() to set components in absolute positions. Instead use layout managers. See https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Focus on Keylistener

So what I've got is that I would love to try and make an little login applet. I'm using Java Windowsbuilder for it, to make it easyer for me. I hope the code isn't to messy, because I'm just a starter.
The problem I've got is that My JButton "login" only registers a keyevent when it's selected trought tab, or when you first click it. What I want is that I can use the button always just by pressing the "ENTER" key.
Hope you guys solve my problem :)
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;
import java.awt.event.KeyAdapter;
public class nummer1 extends JFrame{
private JPanel contentPane;
public static nummer1 theFrame;
private JTextField textFieldUsername;
private JTextField textFieldPass;
private nummer1 me;
private JLabel lblCheck;
private String password = "test", username = "test";
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
nummer1 frame = new nummer1();
nummer1.theFrame = frame;
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void Check(){
String Pass = textFieldPass.getText();
String Username = textFieldUsername.getText();
System.out.println(Pass);
if (Pass.equals(password) && Username.equals(username)){
lblCheck.setText("Correct Login");
}else{
lblCheck.setText("Invalid username or password");
}
}
/**
* Create the frame.
*/
public nummer1() {
me = this;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 356, 129);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(10, 11, 61, 14);
contentPane.add(lblUsername);
JLabel lblPassword = new JLabel("Password:");
lblPassword.setBounds(10, 36, 61, 14);
contentPane.add(lblPassword);
textFieldUsername = new JTextField();
textFieldUsername.setBounds(81, 8, 107, 20);
contentPane.add(textFieldUsername);
textFieldUsername.setColumns(10);
me.textFieldUsername = textFieldUsername;
textFieldPass = new JTextField();
textFieldPass.setBounds(81, 33, 107, 20);
contentPane.add(textFieldPass);
textFieldPass.setColumns(10);
me.textFieldPass = textFieldPass;
JButton btnLogin = new JButton("Login");
contentPane.requestFocusInWindow();
btnLogin.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
me.Check();
System.out.println("hi");
}
}
});
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
me.Check();
}
});
btnLogin.setBounds(198, 7, 89, 23);
contentPane.add(btnLogin);
JLabel lblCheck = new JLabel("");
lblCheck.setHorizontalAlignment(SwingConstants.TRAILING);
lblCheck.setBounds(10, 65, 264, 14);
contentPane.add(lblCheck);
me.lblCheck = lblCheck;
contentPane.setFocusTraversalPolicy(new FocusTraversalOnArray(new
Component[]{lblUsername, lblPassword, textFieldUsername, textFieldPass, btnLogin, lblCheck}));
}
}
Thanks Emil!
What I want is that I can use the button always just by pressing the "ENTER" key
Sounds like you want to make the "login" button the default button for the dialog.
See Enter Key and Button for the problem and solution.
When using panels KeyListeners won't work unless the panel is focusable. What you can do is make a KeyBinding to the ENTER key using the InputMap and ActionMap for your panel.
public class Sample extends JPanel{
//Code
Sample() {
//More code
this.getInputMap().put(KeyStroke.getKeyStroke((char)KeyEvent.VK_ENTER), "Enter" );
this.getActionMap().put("Enter", new EnterAction());
}
private class EnterAction extends AbstractAction(){
#Override
public void ActionPerformed(ActionEvent e){
//Acion
}
}
}

Java Swing JFileChooser append files to a JTextArea

I have a browse button which opens a dialog where the user can view directories and files. I am having some trouble appending the files the user selects to a JTextArea. I am trying to do this so the user can select multiple files at a time. The files eventually will be submitted to an Oracle database.
The code I have used for the filechooser is here:
final JFileChooser fc = new JFileChooser();
JList list = new JList();
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(list)) {
File file = fc.getSelectedFile();
Can you please show me how to append the files to a JTextArea?
Thanks.
Edit:
I have added the following:
JButton btnBrowse = new JButton("Browse");
btnBrowse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
final JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(true);
JList list = new JList();
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(list)) {
File file = fc.getSelectedFile();
}
for (File file : fc.getSelectedFiles()) {
log.append(file.getPath());
}
}
});
But when selecting browse and choosing multiple files and then selecting open the files are not being displayed within the text area.
Full Code:
package com.example.android.apis.appwidget;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.awt.Color;
import javax.swing.UIManager;
public class VFSTool extends JFrame {
private JPanel contentPane;
static private final String newline = "\n";
JButton openButton, saveButton;
JTextArea log;
JFileChooser fc;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
VFSTool frame = new VFSTool();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Tool() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 499, 423);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JLabel lblVfsLoaderTool = new JLabel("Tool");
lblVfsLoaderTool.setBackground(new Color(255, 255, 153));
lblVfsLoaderTool.setForeground(UIManager.getColor("Button.darkShadow"));
lblVfsLoaderTool.setFont(new Font("Copperplate Gothic Light", Font.BOLD, 25));
lblVfsLoaderTool.setHorizontalAlignment(SwingConstants.CENTER);
contentPane.add(lblVfsLoaderTool, BorderLayout.NORTH);
JPanel panel = new JPanel();
panel.setBackground(UIManager.getColor("Button.darkShadow"));
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JButton btnBrowse = new JButton("Browse");
btnBrowse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
final JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(true);
JList list = new JList();
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(list)) {
File file = fc.getSelectedFile();
}
for (File file : fc.getSelectedFiles()) {
log.append(file.getPath() + System.getProperty("line.separator"));
}
}
});
btnBrowse.setBounds(107, 185, 97, 25);
panel.add(btnBrowse);
JLabel lblCategory = new JLabel("label1");
lblCategory.setForeground(UIManager.getColor("Button.background"));
lblCategory.setBounds(12, 13, 82, 25);
panel.add(lblCategory);
JComboBox comboBox = new JComboBox();
comboBox.setForeground(UIManager.getColor("Button.background"));
comboBox.setBounds(91, 13, 113, 24);
panel.add(comboBox);
JLabel lblNewLabel = new JLabel("label2");
lblNewLabel.setForeground(UIManager.getColor("Button.background"));
lblNewLabel.setBounds(12, 50, 77, 25);
panel.add(lblNewLabel);
JComboBox comboBox_1 = new JComboBox();
comboBox_1.setBounds(91, 50, 113, 25);
panel.add(comboBox_1);
JLabel lblLanguage = new JLabel("label3");
lblLanguage.setForeground(UIManager.getColor("Button.background"));
lblLanguage.setBounds(12, 114, 56, 16);
panel.add(lblLanguage);
JComboBox comboBox_2 = new JComboBox();
comboBox_2.setBounds(91, 110, 113, 25);
panel.add(comboBox_2);
JCheckBox chckbxIncludeExt = new JCheckBox("include");
chckbxIncludeExt.setForeground(UIManager.getColor("Button.background"));
chckbxIncludeExt.setBackground(UIManager.getColor("Button.darkShadow"));
chckbxIncludeExt.setBounds(12, 219, 113, 25);
panel.add(chckbxIncludeExt);
JButton btnSubmit = new JButton("Submit");
btnSubmit.setBounds(107, 264, 97, 25);
panel.add(btnSubmit);
JTextArea textArea = new JTextArea();
textArea.setBounds(240, 14, 219, 220);
panel.add(textArea);
}
}
Enable multi-file selection using JFileChooser#setMultiSelectionEnabled, then iterate through all files returned from getSelectedFiles, appending the output of getPath to your JTextArea
if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(list)) {
for (File file : fc.getSelectedFiles()) {
myTextArea.append(file.getPath() + System.getProperty("line.separator"));
}
}
Edit:
You should be getting a stacktrace like this
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at swing9.VFSTool$2.actionPerformed(VFSTool.java:81)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
This is telling you that something is not initialized, i.e. your JTextArea log
Not only has this not been initialized but hasnt been added to the frame. You probably want the text displayed in textArea instead. This is only available in the scope of the constructor so will need to declared as a class member variable.
private JTextArea textArea;

Categories

Resources