Java Swing How to - Update highlighted rows in JTable








Question

We would like to know how to update highlighted rows in JTable.

Answer

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
//  w ww  .j  a va2  s  . c o m
public class Main {
  public static void main(String[] args) {
    JFrame f = new JFrame("Test");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(new String[][] { { "One" }, { "Two" },
        { "Three" } }, new String[] { "Ordinal" });
    table.addRowSelectionInterval(1, 1);
    f.add(new JScrollPane(table));
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);

  }
}