Java JTable get value by row and column index

Description

Java JTable get value by row and column index

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class Main extends JPanel {
   public Main() {
      setLayout(new BorderLayout(5, 5));
      String[][] data = { //w  ww.  j av  a2  s  .c om
            { "0", "0", "1", "8", "8", "9", "7" },// 
            { "0", "0", "2", "7", "8", "8", "8" },//
            { "0", "0", "3", "8", "8", "9", "6" },//
            { "0", "0", "4", "8", "8", "9", "8" },//
            { "0", "0", "5", "8", "8", "9", "8" } //
          };
      String[] colHeaders = { "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri" };
      final JTable table = new JTable(data, colHeaders);

      Object o = table.getValueAt(3, 3);
      System.out.println(o);
      
      JScrollPane sp = new JScrollPane(table);

      add(sp, BorderLayout.CENTER);
   }
   public static void main(String[] args) {
      JFrame frame = new JFrame("java2s.com");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      Main m = new Main();
      frame.add(m);
      frame.setSize(300, 210);
      frame.setVisible(true);
   }
}



PreviousNext

Related