简体   繁体   中英

JTextField not updating

I have two separate components, one is a class that extends JTable , the other is a class that extends JPanel (contains a form).

Both are displayed on the same view when that application is run, when I click on a row on the table, I expect the textfileds on the form to be updated but nothing happens I use observer (the form class is the listener) pattern to send the clicked row to the class/ panel containing the form fields to be updated, the values are received but the textfields are not updated.

The code below is in the form class and it updates the form fields, the form class is added as a listener in the table class, the method below is fired when a table row is clicked

public void onTableRowClick(CollectedParcelEvent e)
{
     JOptionPane.showMessageDialog(null,"test", "test", 1);

    txtCost.setText(Double.toString(e.getSource().getCost()));
    txtCustomerName.setText(e.getSource().getCustomer().getName());


    txtCost.repaint();
    txtCost.revalidate();

}
public void onTableRowClick(CollectedParcelEvent e)
{
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            //JOptionPane.showMessageDialog(null,"test", "test", 1);
            txtCost.setText(Double.toString(e.getSource().getCost()));
            txtCustomerName.setText(e.getSource().getCustomer().getName());
        }
    });
}

Events are handled on the single event thread. There the GUI is not responsive for other events, and one should postpone doing such things later with invokeLater .

I don't understand why are you calling revalidate(); as its just tells the layout manager to reset based on the newly added or removed component list.

See this link for more answers about using revalidate(); and also this one

And perhaps, repaint(); should be enough for the required change.

So check your method to see if it really gets fired or not.

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