java swing从actionPerformed方法返回bean对象
我已经为filechooser创建了带有操作侦听器的Swing类(如下所示)。Filechooser允许用户浏览文件。当用户选择文件时,我希望将该文件路径存储在pojo bean对象中,并在java代码中使用该路径(以执行进一步的操作)。但是,ActionPerformed()方法不允许返回bean对象。那么,如何返回bean对象以从该类获取其他java类中的文件路径呢 这是我到目前为止所达到的目标:-java swing从actionPerformed方法返回bean对象,java,swing,javabeans,Java,Swing,Javabeans,我已经为filechooser创建了带有操作侦听器的Swing类(如下所示)。Filechooser允许用户浏览文件。当用户选择文件时,我希望将该文件路径存储在pojo bean对象中,并在java代码中使用该路径(以执行进一步的操作)。但是,ActionPerformed()方法不允许返回bean对象。那么,如何返回bean对象以从该类获取其他java类中的文件路径呢 这是我到目前为止所达到的目标:- package com.tcs.autocreatic.gui; import j
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();
}
});
}
}
有人能帮我吗?
提前感谢:)并尽快(或永远)获得更好的(或任何)帮助。既然可以存储
文件,为什么还要存储路径?为什么不将文件
存储在操作
中,看到了吗?@trashgood:但我想在另一个POJO类中使用这些路径。可以使用文件吗?你总是可以从文件实例中获取路径。@trashgood:我试过你的例子,它成功了。我能够提取绝对文件路径。但是,我不知道如何在我的例子中实现它。你能帮我吗?我不太会荡秋千。感谢您的帮助:)