简体   繁体   中英

How can i write Focus Gained event for Particular column in JTable?

In my project i have used JTable to billing. In first column i will give some number as input like 1 when i click tab button second column should display name of the product. I wrote it for mouse clicked event. But i dont know how to write it for focus gained. Am using Netbeans IDE.

    if(table.getSelectedColumn() == 1)
    {
        int row = table.getSelectedRow();
        int column = table.getSelectedColumn();
        int code = Integer.parseInt(table.getValueAt(table.getSelectedRow(), 0).toString());
        if(code < 1 || code > 48)
        {
            JOptionPane.showMessageDialog(this, "Please Enter Correct Product Code");
            return;
        }
        if(table.getValueAt(row, 0) != null)
        {
            table.setValueAt(tamil.get(code-1), row, 1);
        }
    }

This is my code its working fine for mouse clicked. Anyone can help me?

I wrote it for mouse clicked event.

Don't write code for a mouse event. What it the user uses the tab key to move to the next cell?

Instead implement a general solution by overriding the setValueAt(...) method of your TableModel.

When you change the value in the first column do your lookup and change the value in the second column. Something like:

@Override
public void setValueAt(Object value, int row, int column)
{
    super.setValueAt(value, row, column);

    if (column == 0)
    {
        String name = lookupName(...);
        super.setValueAt(name, row, 1);
    }
}

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