Sorry for the newbish question, I'm trying to create a GUI for a simple application using JAVA and Swing but I'm stuck trying to get a variable value generated within an action listener from the outside.
public geRes()
{
setTitle("geRes");
setBounds(100, 100, 272, 308);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
JButton btnNewButton = new JButton("igen");
btnNewButton.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.showOpenDialog(fc.getParent());
fc.getName();
}
});
btnNewButton.setToolTipText("Selec");
getContentPane().add(btnNewButton);
JButton btnCivos = new JButton("smbinar");
btnCivos.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
File dir = new File(); // I want to use fc.getName() as argument there
I'd like to access fc.getName() from the second method, inside a different button. Any suggestions? Thanks in advance!
make your JFileChooser
as a global variable so you could call it from the other method.
initialize it outside the method
JFileChooser fc;
you can place it here:
public geRes()
{
JFileChooser fc;
setTitle("geRes");
...
then when you use the JFileChooser, it will look like this
fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
...
then, you can call now the JFileChooser from the other method.
JButton btnCivos = new JButton("smbinar");
btnCivos.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
//you can now get the value of fc.getName()
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.