Mega Code Archive

 
Categories / Java Tutorial / Swing
 

Add logic to get value method

import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; public class GetValueAtLogic extends JFrame {   public GetValueAtLogic() {     JTable table = new JTable(new CoordinateTableModel());     getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);     pack();   }   public static void main(String arg[]) {     GetValueAtLogic ex1 = new GetValueAtLogic();     ex1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     ex1.setVisible(true);   } } class CoordinateTableModel extends AbstractTableModel {   private int[] x = { 2, 3, 5, 7, 11, 13, 17, 19 }; // x is prime < 20   public int getColumnCount() {     return 2;   }   public int getRowCount() {     return x.length;   }   public Object getValueAt(int r, int c) {     return (c == 0) ? new Integer(x[r]) : new Integer(2 * x[r] + 1);   }   public String getColumnName(int c) {     return (c == 0) ? "x" : "2x+1";   } }