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:MainClass.java

public MainClass() {
    super("Selection Model Test");
    setSize(450, 350);/*from   w  ww  .  j  a  v a  2  s .  c  om*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    TableModel tm = new AbstractTableModel() {
        public int getRowCount() {
            return 10;
        }

        public int getColumnCount() {
            return 10;
        }

        public Object getValueAt(int r, int c) {
            return "0";
        }
    };

    final JTable jt = new JTable(tm);

    JScrollPane jsp = new JScrollPane(jt);
    getContentPane().add(jsp, BorderLayout.CENTER);

    JPanel controlPanel, buttonPanel, columnPanel, rowPanel;

    buttonPanel = new JPanel();
    final JCheckBox cellBox, columnBox, rowBox;
    cellBox = new JCheckBox("Cells", jt.getCellSelectionEnabled());
    columnBox = new JCheckBox("Columns", jt.getColumnSelectionAllowed());
    rowBox = new JCheckBox("Rows", jt.getRowSelectionAllowed());

    cellBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            jt.setCellSelectionEnabled(cellBox.isSelected());
            columnBox.setSelected(jt.getColumnSelectionAllowed());
            rowBox.setSelected(jt.getRowSelectionAllowed());
        }
    });

    columnBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            jt.setColumnSelectionAllowed(columnBox.isSelected());
            cellBox.setSelected(jt.getCellSelectionEnabled());
        }
    });

    rowBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            jt.setRowSelectionAllowed(rowBox.isSelected());
            cellBox.setSelected(jt.getCellSelectionEnabled());
        }
    });

    buttonPanel.add(new JLabel("Selections allowed:"));
    buttonPanel.add(cellBox);
    buttonPanel.add(columnBox);
    buttonPanel.add(rowBox);

    columnPanel = new JPanel();
    ListSelectionModel csm = jt.getColumnModel().getSelectionModel();
    JLabel columnCounter = new JLabel("Selected Column Indices:");
    csm.addListSelectionListener(new SelectionDebugger(columnCounter, csm));
    columnPanel.add(new JLabel("Selected columns:"));
    columnPanel.add(columnCounter);

    rowPanel = new JPanel();
    ListSelectionModel rsm = jt.getSelectionModel();
    JLabel rowCounter = new JLabel("Selected Row Indices:");
    rsm.addListSelectionListener(new SelectionDebugger(rowCounter, rsm));
    rowPanel.add(new JLabel("Selected rows:"));
    rowPanel.add(rowCounter);

    controlPanel = new JPanel(new GridLayout(0, 1));
    controlPanel.add(buttonPanel);
    controlPanel.add(columnPanel);
    controlPanel.add(rowPanel);

    getContentPane().add(controlPanel, BorderLayout.SOUTH);
}

From source file:MainClass.java

public MainClass() {
    super("Customer Editor Test");
    setSize(600, 160);//  ww w .  j  a  v  a  2 s . co  m
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    MixerModel test = new MixerModel();
    JTable jt = new JTable(test);
    jt.setDefaultRenderer(Volume.class, new VolumeRenderer());
    jt.setDefaultEditor(Volume.class, new VolumeEditor());
    JScrollPane jsp = new JScrollPane(jt);
    getContentPane().add(jsp, BorderLayout.CENTER);
}

From source file:DatabaseTest.java

public DatabaseTest() {
    super("Database Test Frame");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(350, 200);/* w  ww.java 2  s . c  o  m*/

    qtm = new QueryTableModel();
    JTable table = new JTable(qtm);
    JScrollPane scrollpane = new JScrollPane(table);
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(3, 2));
    p1.add(new JLabel("Enter the Host URL: "));
    p1.add(hostField = new JTextField());
    p1.add(new JLabel("Enter your query: "));
    p1.add(queryField = new JTextField());
    p1.add(new JLabel("Click here to send: "));

    JButton jb = new JButton("Search");
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            qtm.setHostURL(hostField.getText().trim());
            qtm.setQuery(queryField.getText().trim());
        }
    });
    p1.add(jb);
    getContentPane().add(p1, BorderLayout.NORTH);
    getContentPane().add(scrollpane, BorderLayout.CENTER);
}

From source file:ModelJTable.java

public ModelJTable() {
    super();//from   w ww . j ava 2 s  .c o  m
    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);
}

From source file:TimeViewer.java

public TimeViewer() {
    tableModel = new LocaleTableModel();
    JTable table = new JTable(tableModel);
    add(new JScrollPane(table));

    refreshTable();//from  ww  w  .j a  v a2 s. co  m
}

From source file:Main.java

public Main() {
    setLayout(new BorderLayout());

    String[] items1 = { "Red", "Blue", "Green" };
    JComboBox<String> comboBox1 = new JComboBox<String>(items1);
    DefaultCellEditor dce1 = new DefaultCellEditor(comboBox1);
    editors.add(dce1);/*from   ww  w .j  a  va  2 s.  c  om*/

    String[] items2 = { "Circle", "Square", "Triangle" };
    JComboBox<String> comboBox2 = new JComboBox<String>(items2);
    DefaultCellEditor dce2 = new DefaultCellEditor(comboBox2);
    editors.add(dce2);

    String[] items3 = { "Apple", "Orange", "Banana" };
    JComboBox<String> comboBox3 = new JComboBox<String>(items3);
    DefaultCellEditor dce3 = new DefaultCellEditor(comboBox3);
    editors.add(dce3);

    Object[][] data = { { "Color", "Red" }, { "Shape", "Square" }, { "Fruit", "Banana" }, { "Plain", "Text" } };
    String[] columnNames = { "Type", "Value" };
    DefaultTableModel model = new DefaultTableModel(data, columnNames);
    JTable table = new JTable(model) {
        public TableCellEditor getCellEditor(int row, int column) {
            int modelColumn = convertColumnIndexToModel(column);

            if (modelColumn == 1 && row < 3)
                return editors.get(row);
            else
                return super.getCellEditor(row, column);
        }
    };

    JScrollPane scrollPane = new JScrollPane(table);
    add(scrollPane);
}

From source file:com.stam.batchmove.BatchMoveUtils.java

public static void showFilesFrame(Object[][] data, String[] columnNames, final JFrame callerFrame) {
    final FilesFrame filesFrame = new FilesFrame();

    DefaultTableModel model = new DefaultTableModel(data, columnNames) {

        private static final long serialVersionUID = 1L;

        @Override/*from  w  w w .jav a  2 s.c o  m*/
        public Class<?> getColumnClass(int column) {
            return getValueAt(0, column).getClass();
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return column == 0;
        }
    };
    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setHorizontalAlignment(JLabel.CENTER);
    final JTable table = new JTable(model);
    for (int i = 1; i < table.getColumnCount(); i++) {
        table.setDefaultRenderer(table.getColumnClass(i), renderer);
    }
    //            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setRowHeight(30);
    table.getTableHeader().setFont(new Font("Serif", Font.BOLD, 14));
    table.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
    table.setRowSelectionAllowed(false);
    table.getColumnModel().getColumn(0).setMaxWidth(35);
    table.getColumnModel().getColumn(1).setPreferredWidth(350);
    table.getColumnModel().getColumn(2).setPreferredWidth(90);
    table.getColumnModel().getColumn(2).setMaxWidth(140);
    table.getColumnModel().getColumn(3).setMaxWidth(90);

    JPanel tblPanel = new JPanel();
    JPanel btnPanel = new JPanel();

    tblPanel.setLayout(new BorderLayout());
    if (table.getRowCount() > 15) {
        JScrollPane scrollPane = new JScrollPane(table);
        tblPanel.add(scrollPane, BorderLayout.CENTER);
    } else {
        tblPanel.add(table.getTableHeader(), BorderLayout.NORTH);
        tblPanel.add(table, BorderLayout.CENTER);
    }

    btnPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

    filesFrame.setMinimumSize(new Dimension(800, 600));
    filesFrame.setLayout(new BorderLayout());
    filesFrame.add(tblPanel, BorderLayout.NORTH);
    filesFrame.add(btnPanel, BorderLayout.SOUTH);

    final JLabel resultsLabel = new JLabel();

    JButton cancelBtn = new JButton("Cancel");
    cancelBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            filesFrame.setVisible(false);
            callerFrame.setVisible(true);
        }
    });

    JButton moveBtn = new JButton("Copy");
    moveBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            fileChooser.setDialogTitle("Choose target directory");
            int selVal = fileChooser.showOpenDialog(null);
            if (selVal == JFileChooser.APPROVE_OPTION) {
                File selection = fileChooser.getSelectedFile();
                String targetPath = selection.getAbsolutePath();

                DefaultTableModel dtm = (DefaultTableModel) table.getModel();
                int nRow = dtm.getRowCount();
                int copied = 0;
                for (int i = 0; i < nRow; i++) {
                    Boolean selected = (Boolean) dtm.getValueAt(i, 0);
                    String filePath = dtm.getValueAt(i, 1).toString();

                    if (selected) {
                        try {
                            FileUtils.copyFileToDirectory(new File(filePath), new File(targetPath));
                            dtm.setValueAt("Copied", i, 3);
                            copied++;
                        } catch (Exception ex) {
                            Logger.getLogger(SelectionFrame.class.getName()).log(Level.SEVERE, null, ex);
                            dtm.setValueAt("Failed", i, 3);
                        }
                    }
                }
                resultsLabel.setText(copied + " files copied. Finished!");
            }
        }
    });
    btnPanel.add(cancelBtn);
    btnPanel.add(moveBtn);
    btnPanel.add(resultsLabel);

    filesFrame.revalidate();
    filesFrame.setVisible(true);

    callerFrame.setVisible(false);
}

From source file:ShowCurrencies.java

public ShowCurrencies() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    final Locale[] locales = Locale.getAvailableLocales();
    TableModel model = new AbstractTableModel() {
        public int getColumnCount() {
            return 3;
        }//w  w  w . ja  va2s  .c o m

        public String getColumnName(int column) {
            if (column == 0)
                return "Locale";
            else if (column == 1)
                return "Currency Code";
            else
                return "Currency Symbol";
        }

        public int getRowCount() {
            return locales.length;
        }

        public Object getValueAt(int row, int col) {
            if (col == 0)
                return locales[row];
            else
                try {
                    if (col == 1)
                        return Currency.getInstance(locales[row]).getCurrencyCode();
                    else
                        return Currency.getInstance(locales[row]).getSymbol(locales[row]);
                } catch (IllegalArgumentException iae) {
                    return null;
                }
        }
    };

    JTable table = new JTable(model);
    JScrollPane sp = new JScrollPane(table);
    getContentPane().add(sp);
    pack();
    setVisible(true);
}

From source file:MixerTest2.java

public MixerTest2() {
    super("Customer Editor Test");
    setSize(600, 160);// ww w .j a  va 2  s. co m
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    MixerModel test = new MixerModel();
    test.dump();
    JTable jt = new JTable(test);
    jt.setDefaultRenderer(Volume.class, new VolumeRenderer());
    jt.setDefaultEditor(Volume.class, new VolumeEditor());
    JScrollPane jsp = new JScrollPane(jt);
    getContentPane().add(jsp, BorderLayout.CENTER);
}

From source file:SimpleTableModel.java

public Main() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    JTable table = new JTable(new SimpleTableModel());

    Container contentPane = this.getContentPane();
    contentPane.add(new JScrollPane(table), BorderLayout.CENTER);
}