Search code examples
javaswingjtextfieldjfilechooser

Jtextfield setText() would not work at all


So this is what I'm trying to do is to get the absolutePath into the jtextfield and I did System print and it showed the path but would not set the text in jtextfield.

Action event (button) in wizard class :

  //buttons
  JButton openMapButton = new JButton("Load Map");
  ImageLoader imgload = new ImageLoader();
  openMapButton.addActionListener(new ActionListener(){
     public void actionPerformed(ActionEvent e){
        imgload.fileChooser();
     }
 });

Jtextfield in Wizard class:

      JTextField mapURLField = new JTextField(20);

ImageLoader class:

public class ImageLoader extends Wizard{

private JLabel label; 
JFileChooser chooser;
File file; 
private BufferedImage img;
Wizard wiz = new Wizard();
JTextField mapURLField;   


public void loadImg(){
}


public void fileChooser(){
 wiz.mapURLField = new JTextField();
      
  if(chooser == null){
  chooser = new JFileChooser(".");
}

int returnVal = chooser.showOpenDialog(null);

if(returnVal == JFileChooser.APPROVE_OPTION){
  wiz.mapURLField.setText(chooser.getSelectedFile().getAbsolutePath());
  System.out.println(chooser.getSelectedFile().getAbsolutePath());
  }else{
     wiz.mapURLField.setText("");
  }
  
  chooser.setSelectedFile(null);


 } 
}

Solution

  • You're setting the state of the wrong JTextField inside of the wrong Wizard object. The text field that should be changed is the one that is displayed in the displayed Wizard class, not a JTextField that you create on the spot in some other class that is in a newly created non-displayed Wizard class that you also create in your ImageLoader.

    This:

    public class ImageLoader extends Wizard{
        private JLabel label; 
    
        // ....        
    
        Wizard wiz = new Wizard();
        JTextField mapURLField; 
    

    creates a new Wizard object, but it is certainly not the Wizard object that is currently being displayed, and so changing the state of this newly created and non-displayed object will not help you.

    Instead, change:

    ImageLoader imgload = new ImageLoader();
    

    to:

    ImageLoader imgload = new ImageLoader(this);     
    

    if not called within an ActionListener or other anonymous class, as this should load the displayed Wizard object into the ImageLoader class.

    And in ImageLoader do:

    private Wizard wiz; // a field to hold the displayed Wizard
    
    public ImageLoader(Wizard wiz) {
        this.wiz = wiz; // set the field with the parameter passed in
        ....
    }
    

    and then call the methods of *true Wizard object, now held by wiz, in your ImageLoader class.

    Also, get rid of

    wiz.mapURLField = new JTextField();
    

    It makes no sense creating a new JTextField, one that certainly is not being displayed in the original Wizard object. You want to change the state of the displayed JTextField. The best way to do this is to give Wizard a public method that allows you to do this:

    // in the Wizard class:
    
    public void setMapUrlField(String text) {
        mapURLField.setText(text);
    }
    

    Finally, the ImageLoader extending the Wizard class:

    public class ImageLoader extends Wizard {
    

    is probably not doing what you think it's doing, and is likely not what you want to be doing.


    Proof of concept code:

    import java.io.File;
    import javax.swing.*;
    
    public class Wizard extends JPanel {
        private ImageLoader imgload = new ImageLoader(this);
        private JTextField mapURLField = new JTextField(30);
        private JButton openMapButton = new JButton("Load Map");
        private File file;
        
        public Wizard() {
            openMapButton.addActionListener(e -> {
                file = imgload.fileChooser();
            });
            
            mapURLField.setFocusable(false);
            add(mapURLField);
            add(openMapButton);
        }
        
        public void setMapUrlField(String text) {
            mapURLField.setText(text);
        } 
        
        public File getFile() {
            return file;
        }
        
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                Wizard wiz = new Wizard();
                
                JFrame frame = new JFrame("Wizard");
                frame.add(wiz);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            });
        }
    }
    
    class ImageLoader {
    
        private Wizard wiz;
    
        public ImageLoader(Wizard wizard) {
            this.wiz = wizard;
        }
    
        public File fileChooser() {
            File file = null;
            JFileChooser fileChooser = new JFileChooser();
            int retValue = fileChooser.showOpenDialog(wiz);
            if (retValue == JFileChooser.APPROVE_OPTION) {
                file = fileChooser.getSelectedFile();
                wiz.setMapUrlField(file.getAbsolutePath());
            } else {
                wiz.setMapUrlField("");
            }
            
            return file;
        }
        
    }