简体   繁体   中英

Using JFileChooser to set values JTextField

I'm learning Java and I'm trying to load some numbers that are inside a .txt file. These numbers need to appear in Text Fields inside my GUI. I've done a lot of research in google but I cant get it to work. Any hints?

//TEXT FIELDS
    this.Vx = new JTextField();
    this.Vx.setBounds(120, 20, 100, 30);
    this.add(Vx);
    JLabel etiVx = new JLabel("Vx");
    etiVx.setBounds(90, 20, 90, 40);
    this.add(etiVx);

    this.Vy = new JTextField();
    this.Vy.setBounds(120, 60, 100, 30);
    this.add(Vy);
    JLabel etiVy = new JLabel("Vy");
    etiVy.setBounds(90,60,100,30);
    this.add(etiVy);

//BOTTON TO OPEN .TXT
@Override
public void actionPerformed(ActionEvent e) {
    JFileChooser abrir = new JFileChooser();
    if(e.getSource()==this.abrir){
        int returnVal = abrir.showOpenDialog(PanelControl.this);
        if(returnVal == JFileChooser.APPROVE_OPTION);
        File file = abrir.getSelectedFile();

        JOptionPane.showMessageDialog(null,"Abierto");
        try{
            BufferedReader eleccion = new BufferedReader(new FileReader(file));
            String linea = eleccion.readLine();
            while((linea=eleccion.readLine())!=null){
                Vx.setText(file);
                linea = eleccion.readLine();
            }
        }
        catch(IOException f){
            System.out.println(f);
        }
    }

What is this,

           while(eleccion!=null){
                Vx.setText(file);//how can you set a file handler to a TextField
                linea = eleccion.readLine();
            }

use,

               while((linea=eleccion.readLine())!=null){
                    Vx.setText(linea);

                }

If you need to show all the Text in your file it's better to use JTextArea and append all the lines to it that you read from the file.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM