我创建了我的Swing类,其中包含用于filechooser的动作侦听器(如下所示)。 Filechooser将允许用户浏览文件。当用户选择文件时,我想将该文件路径存储在pojo bean对象中,并在我的java代码中使用该路径(以执行进一步的操作)。但是,ActionPerformed()方法不允许返回bean对象。那么,如何从这个类中返回我的bean对象以获取其他java类中的文件路径?
这是我到目前为止所处的地方: -
package com.tcs.autocreatic.gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.*;
import com.tcs.autocreatic.commons.Bean;
import com.tcs.autocreatic.create.bean.BeanCreation;
public class PageCreationUI extends JPanel
implements ActionListener{
JLabel label,label1,label2;
Object[] options1 = { "OK", "Cancel" };
int decision;
JFileChooser fc;
JButton browse;
JPanel panel;
JTextField beanPath;
static JFrame frame;
Dimension d;
Bean bean =new Bean();
BeanCreation bCreate = new BeanCreation();
public PageCreationUI() {
super(new BorderLayout());
d=new Dimension(800,600);
panel = new JPanel();
browse= new JButton("Browse..");
browse.addActionListener(this);
JPanel p = new JPanel();
p.setLayout(new GridLayout(3, 1));
label =new JLabel("Enter PageBean(src) location : ");
p.add(label);
JPanel q=new JPanel();
q.setLayout(new GridLayout(3, 1));
beanPath =new JTextField(20);
q.add(beanPath);
JPanel b=new JPanel();
b.setLayout(new GridLayout(3, 1));
b.add(browse);
JPanel contentPane = new JPanel();
//contentPane.setPreferredSize(d);
contentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
contentPane.setLayout(new BorderLayout());
contentPane.add(p,"West");
contentPane.add(q);
contentPane.add(b,"East");
fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = JOptionPane.showOptionDialog(null, contentPane, "Select Location",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options1, null);
if (result == JOptionPane.YES_OPTION){
JOptionPane.showMessageDialog(null, beanPath.getText());
//Calling (my defined) other POJO class method when user clicks OK button
try {
bCreate.createBean();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if (result == JOptionPane.NO_OPTION){
int res = JOptionPane.showConfirmDialog(null, "Do you really want to exit ?", "Closing the Application", JOptionPane.OK_CANCEL_OPTION);
if (res == JOptionPane.OK_OPTION){
System.exit(1);
}
else if (result == JOptionPane.CANCEL_OPTION){
}
}
else if (result == JOptionPane.CANCEL_OPTION){
System.exit(1);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == browse) {
int returnVal = fc.showOpenDialog(PageCreationUI.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
String beanLocation= file.getAbsolutePath();
beanPath.setText(beanLocation);
beanLocation=beanLocation.replaceAll("\\\\","\\\\\\\\");
//trying to store filepaths in bean .But don't know how to return this bean object to other //class
bean.setBeanPath(beanLocation);
System.out.println("Bean :: Absolute location is :-" +beanLocation);
} else {
}
}
}
private static void createAndShowGUI() {
//Create and set up the window.
frame = new JFrame("contentPane");
//Add content to the window.
frame.add(new PageCreationUI());
frame.pack();
//Display the window.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
任何人都可以帮我这个吗? 在此先感谢:)