Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
615 views
in Technique[技术] by (71.8m points)

java - How to get cell value of jtable depending on which row is clicked

I am trying to use an update method on my jtable that's connected to the database and would like to fill in the textfields on the form depending on which row the users clicks. I understand I will be needing a getValueAt() method however I am uncertain of how to fill in which row depending on which row the user clicks. I am unable to find anything on Google or anything so any information would be helpful!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
private final UrTableModel urTableModel;
private JTable urTable;
...

// 1. Create your table model class that should extends from DefaultTableModel, instantiate it
urTableModel=new UrTableModel();

// 2. creates table
table = TableUtils.createStandardSortableTable(urTableModel);

// 3. customize your table
table.setBackground(Color.WHITE);
table.getTableHeader().setReorderingAllowed(false);

// 4. Add the mouse listner to it
table.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(final MouseEvent e) {
        if (e.getClickCount() == 1) {
            final JTable target = (JTable)e.getSource();
            final int row = target.getSelectedRow();
            final int column = target.getSelectedColumn();
            // Cast to ur Object type
            final UrObjctInCell urObjctInCell = (UrObjctInCell)target.getValueAt(row, column);
            // TODO WHAT U WANT!
        }
    }
});

Cheers,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...