Java by API/javax.swing.table/DefaultTableModel — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:17, 31 мая 2010

DefaultTableModel: addColumn(Object columnName)

 
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class ModelJTable extends JFrame {
  private DefaultTableModel model;
  private JTable table;
  public ModelJTable() {
    super();
    model = new DefaultTableModel();
    model.addColumn("First Name");
    model.addColumn("Last Name");
    model.addColumn("Years");
    String[] socrates = { "Socrates", "", "469-399 B.C." };
    model.addRow(socrates);
    String[] plato = { "Plato", "", "428-347 B.C." };
    model.addRow(plato);
    String[] aquinas = { "Thomas", "Aquinas", "1225-1274" };
    model.addRow(aquinas);
    String[] kierkegaard = { "Soren", "Kierkegaard", "1813-1855" };
    model.addRow(kierkegaard);
    String[] kant = { "Immanuel", "Kant", "1724-1804" };
    model.addRow(kant);
    String[] nietzsche = { "Friedrich", "Nietzsche", "1844-1900" };
    model.addRow(nietzsche);
    String[] arendt = { "Hannah", "Arendt", "1906-1975" };
    model.addRow(arendt);
    table = new JTable(model);
    JButton addButton = new JButton("Add Philosopher");
    addButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        String[] philosopher = { "", "", "" };
        model.addRow(philosopher);
      }
    });
    JButton removeButton = new JButton("Remove Selected Philosopher");
    removeButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        model.removeRow(table.getSelectedRow());
      }
    });
    JPanel inputPanel = new JPanel();
    inputPanel.add(addButton);
    inputPanel.add(removeButton);
    Container container = getContentPane();
    container.add(new JScrollPane(table), BorderLayout.CENTER);
    container.add(inputPanel, BorderLayout.NORTH);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(400, 300);
    setVisible(true);
  } 
  public static void main(String args[]) {
    new ModelJTable();
  }
}





DefaultTableModel: addRow(Object[] rowData)

 
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class ModelJTable extends JFrame {
  private DefaultTableModel model;
  private JTable table;
  public ModelJTable() {
    super();
    model = new DefaultTableModel();
    model.addColumn("First Name");
    model.addColumn("Last Name");
    model.addColumn("Years");
    String[] socrates = { "Socrates", "", "469-399 B.C." };
    model.addRow(socrates);
    String[] plato = { "Plato", "", "428-347 B.C." };
    model.addRow(plato);
    String[] aquinas = { "Thomas", "Aquinas", "1225-1274" };
    model.addRow(aquinas);
    String[] kierkegaard = { "Soren", "Kierkegaard", "1813-1855" };
    model.addRow(kierkegaard);
    String[] kant = { "Immanuel", "Kant", "1724-1804" };
    model.addRow(kant);
    String[] nietzsche = { "Friedrich", "Nietzsche", "1844-1900" };
    model.addRow(nietzsche);
    String[] arendt = { "Hannah", "Arendt", "1906-1975" };
    model.addRow(arendt);
    table = new JTable(model);
    JButton addButton = new JButton("Add Philosopher");
    addButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        String[] philosopher = { "", "", "" };
        model.addRow(philosopher);
      }
    });
    JButton removeButton = new JButton("Remove Selected Philosopher");
    removeButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        model.removeRow(table.getSelectedRow());
      }
    });
    JPanel inputPanel = new JPanel();
    inputPanel.add(addButton);
    inputPanel.add(removeButton);
    Container container = getContentPane();
    container.add(new JScrollPane(table), BorderLayout.CENTER);
    container.add(inputPanel, BorderLayout.NORTH);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(400, 300);
    setVisible(true);
  } 
  public static void main(String args[]) {
    new ModelJTable();
  }
}





DefaultTableModel: insertRow(int row, Object[] rowData)

  

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Main {
  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);
    f.add(new JScrollPane(table));
    f.setVisible(true);
  }
}





DefaultTableModel: moveRow(int start, int end, int to)

  
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Main {
  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 row to the beginning of the table
    model.moveRow(model.getRowCount() - 1, model.getRowCount() - 1, 0);
    JFrame f = new JFrame();
    f.setSize(300, 300);
    f.add(new JScrollPane(table));
    f.setVisible(true);
  }
}





DefaultTableModel: removeRow(int row)

 
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class ModelJTable extends JFrame {
  private DefaultTableModel model;
  private JTable table;
  public ModelJTable() {
    super();
    model = new DefaultTableModel();
    model.addColumn("First Name");
    model.addColumn("Last Name");
    model.addColumn("Years");
    String[] socrates = { "Socrates", "", "469-399 B.C." };
    model.addRow(socrates);
    String[] plato = { "Plato", "", "428-347 B.C." };
    model.addRow(plato);
    String[] aquinas = { "Thomas", "Aquinas", "1225-1274" };
    model.addRow(aquinas);
    String[] kierkegaard = { "Soren", "Kierkegaard", "1813-1855" };
    model.addRow(kierkegaard);
    String[] kant = { "Immanuel", "Kant", "1724-1804" };
    model.addRow(kant);
    String[] nietzsche = { "Friedrich", "Nietzsche", "1844-1900" };
    model.addRow(nietzsche);
    String[] arendt = { "Hannah", "Arendt", "1906-1975" };
    model.addRow(arendt);
    table = new JTable(model);
    JButton addButton = new JButton("Add Philosopher");
    addButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        String[] philosopher = { "", "", "" };
        model.addRow(philosopher);
      }
    });
    JButton removeButton = new JButton("Remove Selected Philosopher");
    removeButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        model.removeRow(table.getSelectedRow());
      }
    });
    JPanel inputPanel = new JPanel();
    inputPanel.add(addButton);
    inputPanel.add(removeButton);
    Container container = getContentPane();
    container.add(new JScrollPane(table), BorderLayout.CENTER);
    container.add(inputPanel, BorderLayout.NORTH);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(400, 300);
    setVisible(true);
  } 
  public static void main(String args[]) {
    new ModelJTable();
  }
}





new DefaultTableModel(Object[][] data, Object[] columnNames)

 
import java.awt.BorderLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
public class MainClass {
  public static void main(String args[]) {
    String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
    String headers[] = { "Upper", "Lower" };
    JComboBox comboBox = new JComboBox(rows[0]);
    comboBox.setMaximumRowCount(4);
    
    TableCellEditor editor = new DefaultCellEditor(comboBox);
    JFrame frame = new JFrame("JTable Anatomy");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(new DefaultTableModel(rows, headers));
    table.getColumnModel().getColumn(1).setCellEditor(editor);
    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}