Java Swing DefaultTableModel create custom table

Description

Java Swing DefaultTableModel create custom table



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

public class Main {
  public static void main(String args[]) {
    JFrame guiFrame = new JFrame();
    guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    guiFrame.setSize(500, 500);/*from ww w  .  j a va  2s  . c  o m*/

    JPanel panel = new JPanel();
    panel.setSize(450, 450);

    JTable scheduleTable = new JTable(new TimesTableModel());
    JScrollPane scrollPane = new JScrollPane(scheduleTable);
    panel.add(scrollPane);

    guiFrame.add(panel);
    guiFrame.setVisible(true);
  }
}

class TimesTableModel extends DefaultTableModel {
  @Override
  public int getColumnCount() {
    return 7;
  }

  @Override
  public int getRowCount() {
    return 5;
  }

  @Override
  public Object getValueAt(int row, int column) {
    return new Integer((row + 1) * (column + 1));
  }
}



PreviousNext

Related