简体   繁体   中英

How to Make the Updated text Visible in JTextField in SWING

I have a JTextField which needs to be updated from a derived class. So, Used

CLASS TABPANE :

  package forstack;

  import java.awt.Cursor;
  import java.awt.EventQueue;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.beans.PropertyChangeEvent;
  import java.beans.PropertyChangeListener;

  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JTextField;
  import javax.swing.SwingWorker;
  import javax.swing.border.EmptyBorder;



        public class TabPane extends JFrame implements ActionListener,                                                                                           propertyChangeListener{

/**
 * 
 */
private static final long serialVersionUID = 1L;
private JPanel contentPane;
protected JTextField textField;

private Task task;

class Task extends SwingWorker<Void, Void> {
    /*
     * Main task. Executed in background thread.
     */
    @Override
    public Void doInBackground() {

        Modbus obj = new Modbus();
        obj.updatetextfield();
        return null;
    }

    /*
     * Executed in event dispatching thread
     */
    @Override
    public void done() {

        setCursor(null); //turn off the wait cursor
        //gcsObj.setVisible(false);


    }


}


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                TabPane frame = new TabPane();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public TabPane() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JPanel panel = new JPanel();
    panel.setBounds(10, 11, 414, 240);
    contentPane.add(panel);
    panel.setLayout(null);

    JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(this);
    btnNewButton.setBounds(160, 142, 89, 23);
    panel.add(btnNewButton);

    textField = new JTextField();
    textField.setText("Mainclass");
    textField.setBounds(119, 61, 175, 20);
    panel.add(textField);
    textField.setColumns(10);
}

@Override
public void propertyChange(PropertyChangeEvent event) {
    // TODO Auto-generated method stub

}


@Override
public void actionPerformed(ActionEvent arg0) {

        // TODO Auto-generated method stub
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        //Instances of javax.swing.SwingWorker are not reusuable, so
        //we create new instances as needed.
        task = new Task();
        task.addPropertyChangeListener(this);
        task.execute();

}

}

CLASS MODBUS :

     package forstack;

     public class Modbus extends TabPane{
/**
 * 
 */
private static final long serialVersionUID = 1L;

public void updatetextfield() {
    // TODO Auto-generated method stub
        textField.setText("hello im here");
        textField.revalidate();
        textField.repaint();

        System.out.println(textField.getText());
   }

 }

OUTPUT FRAME: 在此处输入图片说明

I got the output in the console as "hello im here" but it's not visible in the GUI..

Anyone help me with this issue ..

Check that you don't have 2 different JTextField instances.
Try renaming the JTextFields variables to have different names.
Looks like you're updating the one that's not showing on GUI.

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