Billu
Billu

Reputation: 71

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?

Upvotes: 0

Views: 805

Answers (1)

camickr
camickr

Reputation: 324118

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);
    }
}

Upvotes: 1

Related Questions