Example usage for javax.swing JTable JTable

List of usage examples for javax.swing JTable JTable

Introduction

In this page you can find the example usage for javax.swing JTable JTable.

Prototype

public JTable(TableModel dm) 

Source Link

Document

Constructs a JTable that is initialized with dm as the data model, a default column model, and a default selection model.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);

    model.addColumn("Col1");
    model.addRow(new Object[] { "r1" });
    model.addRow(new Object[] { "r2" });
    model.addRow(new Object[] { "r3" });

    // Move the last two rows to the start of the table
    model.moveRow(model.getRowCount() - 2, model.getRowCount() - 1, 0);

    JFrame f = new JFrame();
    f.setSize(300, 300);//www. j  a va  2 s  .  co m
    f.add(new JScrollPane(table));
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);

    model.addColumn("Col1");
    model.addRow(new Object[] { "r1" });
    model.addRow(new Object[] { "r2" });
    model.addRow(new Object[] { "r3" });

    // Move the first two rows to the end of the table
    model.moveRow(0, 1, model.getRowCount() - 2);

    JFrame f = new JFrame();
    f.setSize(300, 300);//from  ww  w.j  a v  a2  s. c  o  m
    f.add(new JScrollPane(table));
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);

    model.addColumn("Col1");
    model.addRow(new Object[] { "r1" });
    model.addRow(new Object[] { "r2" });
    model.addRow(new Object[] { "r3" });

    // Move the first row to the end of the table
    model.moveRow(0, 0, model.getRowCount() - 1);

    JFrame f = new JFrame();
    f.setSize(300, 300);/*from www  .j a  va 2 s  .  c  o  m*/
    f.add(new JScrollPane(table));
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);

    // Create some data
    model.addColumn("Col1");
    model.addRow(new Object[] { "r1" });
    model.addRow(new Object[] { "r2" });
    model.addRow(new Object[] { "r3" });

    model.removeRow(model.getRowCount() - 1);

    JFrame f = new JFrame();
    f.setSize(300, 300);/*from   w  w  w  . jav a 2 s . c  o m*/
    f.add(new JScrollPane(table));
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);

    model.addColumn("Col1");
    model.addColumn("Col2");

    // Create the first row
    model.insertRow(0, new Object[] { "r1" });

    // Append a row
    model.insertRow(model.getRowCount(), new Object[] { "r5" });

    JFrame f = new JFrame();
    f.setSize(300, 300);//from www.  j ava  2s  .  c om
    f.add(new JScrollPane(table));
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);

    model.addColumn("Col1");
    model.addColumn("Col2");

    // Create the first row
    model.insertRow(0, new Object[] { "r1" });

    // Insert a row at position p
    int p = 1;//from w ww . j ava  2  s .c o m
    model.insertRow(p, new Object[] { "r3" });

    JFrame f = new JFrame();
    f.setSize(300, 300);
    f.add(new JScrollPane(table));
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);

    model.addColumn("Col1");
    model.addColumn("Col2");

    model.addRow(new Object[] { "v1", "v2" });

    model.addRow(new Object[] { "v1" });

    model.addRow(new Object[] { "v1", "v2", "v3" });

    JFrame f = new JFrame();
    f.setSize(300, 300);/*from w ww .  ja v  a 2  s  .  c o  m*/
    f.add(new JScrollPane(table));
    f.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    JTable t = new JTable(new TimesTableModel());
    t.setDefaultRenderer(Object.class, new TimesTableRenderer());
    JOptionPane.showMessageDialog(null, t);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new MyDefaultTableModel();
    JTable table = new JTable(model);
    table.setModel(model);// w w  w.  j  a  va 2s. c o  m

    model.addColumn("Col1");
    model.addColumn("Col2");
    model.addColumn("Col3");
    model.addRow(new Object[] { "v1" });

    table.removeColumn(table.getColumnModel().getColumn(0));

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
    table.setAutoCreateColumnsFromModel(false);

    boolean ascending = false;
    Vector data = model.getDataVector();
    Object[] colData = new Object[model.getRowCount()];

    for (int i = 0; i < colData.length; i++) {
        colData[i] = ((Vector) data.get(i)).get(0);
    }//w  w w . java  2s .  c om
    Arrays.sort(colData, new ColumnSorter());
    for (int i = 0; i < colData.length; i++) {
        ((Vector) data.get(i)).set(0, colData[i]);
    }
    model.fireTableStructureChanged();
}