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

public TableCellRendererJComboBox() {
    Container pane = getContentPane();
    pane.setLayout(new BorderLayout());
    TableValues tv = new TableValues();
    table = new JTable(tv);
    TableColumnModel tcm = table.getColumnModel();
    TableColumn tc = tcm.getColumn(TableValues.GENDER);
    tc.setCellRenderer(new GenderRenderer());
    JScrollPane jsp = new JScrollPane(table);
    pane.add(jsp, BorderLayout.CENTER);
}

From source file:SelectionExample.java

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

    TableModel tm = new AbstractTableModel() {
        // We'll create a simple multiplication table to serve as a
        // noneditable
        // table with several rows and columns
        public int getRowCount() {
            return 10;
        }

        public int getColumnCount() {
            return 10;
        }

        public Object getValueAt(int r, int c) {
            return "" + (r + 1) * (c + 1);
        }
    };

    final JTable jt = new JTable(tm);

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

    // Now set up our selection controls
    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 Go Here)");
    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 Go Here)");
    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:SimpleTableModel.java

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

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

    TableRowSorter sorter = new TableRowSorter(table.getModel());
    table.setRowSorter(sorter);/*from   ww w.  j  a v  a 2  s .c  o m*/

    RowFilter<SimpleTableModel, Integer> IDFilter = new RowFilter<SimpleTableModel, Integer>() {
        @Override
        public boolean include(Entry<? extends SimpleTableModel, ? extends Integer> entry) {
            SimpleTableModel model = entry.getModel();
            int rowIndex = entry.getIdentifier().intValue();
            Integer ID = (Integer) model.getValueAt(rowIndex, 0);
            if (ID.intValue() <= 100) {
                return false; // Do not show rows with an ID <= 100
            }
            return true;
        }
    };

    sorter.setRowFilter(IDFilter);

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

From source file:InvestmentTable.java

public InvestmentTableFrame() {
    setTitle("InvestmentTable");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    TableModel model = new InvestmentTableModel(30, 5, 10);
    JTable table = new JTable(model);
    add(new JScrollPane(table));
}

From source file:example.table.TableButton3.java

public TableButton3() {
    String[] columnNames = { "Date", "String", "Integer", "Decimal", "" };
    Object[][] data = { { new Date(), "A", new Integer(1), new Double(5.1), "Delete0" },
            { new Date(), "B", new Integer(2), new Double(6.2), "Delete1" },
            { new Date(), "C", new Integer(3), new Double(7.3), "Delete2" },
            { new Date(), "D", new Integer(4), new Double(8.4), "Delete3" } };

    DefaultTableModel model = new DefaultTableModel(data, columnNames);
    JTable table = new JTable(model) {
        private static final long serialVersionUID = 1L;

        // Returning the Class of each column will allow different
        // renderers to be used based on Class
        public Class<?> getColumnClass(int column) {
            return getValueAt(0, column).getClass();
        }//from   w  w  w.  j a  v  a  2s.  c o  m
    };

    JScrollPane scrollPane = new JScrollPane(table);
    getContentPane().add(scrollPane);

    // Create button column
    ButtonColumn buttonColumn = new ButtonColumn(table, 4);
    log.debug("buttonColumn = " + buttonColumn);
}

From source file:table.FrequencyTablePanel.java

public FrequencyTablePanel(String[] datas, String[] values) {
    super(new GridLayout(1, 0));
    super.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    data = new ArrayList<>();
    tableModel = new MyTableModel(data);
    table = new JTable(tableModel);
    table.setRowHeight(40);/*from  w  ww .  j a v a  2 s .com*/
    // table.getColumnModel().getColumn(0).setResizable(false);
    // table.getColumnModel().getColumn(0).setMaxWidth(120);
    // table.getColumnModel().getColumn(1).setResizable(false);
    // table.getColumnModel().getColumn(1).setMaxWidth(120);
    table.setDefaultRenderer(Object.class, new MyTableCellRenderer2());
    for (int i = 0; i < tableModel.getColumnCount(); i++) {
        table.getTableHeader().getColumnModel().getColumn(i)
                .setHeaderRenderer(new HeaderRenderer(table, alignments[1]));
        // table.getColumnModel().getColumn(i).setCellRenderer(new
        // MyTableCellRenderer());
    }
    table.getTableHeader().setReorderingAllowed(false);
    table.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    // table.setPreferredScrollableViewportSize(new Dimension(10000, 200));

    // Create the scroll pane and add the table to it.
    for (int i = 0; i < datas.length; i++) {
        String[] a = new String[2];
        a[0] = datas[i];
        a[1] = values[i];
        this.tableModel.addRow(a);
    }
    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setPreferredSize(new Dimension(7000, 20));
    // scrollPane.setHorizontalScrollBar(new
    // JScrollBar(JScrollBar.HORIZONTAL));
    // Add the scroll pane to this panel.
    add(scrollPane);
}

From source file:net.sourceforge.jabm.view.JFrameReportVariables.java

@Override
public void afterPropertiesSet() throws Exception {
    table = new JTable(this);
    //      this.setModel(this);
    //      this.setPreferredSize(new Dimension(400, 200));
}

From source file:TableValues.java

public Main() {
    Container pane = getContentPane();
    pane.setLayout(new BorderLayout());
    TableValues tv = new TableValues();
    table = new JTable(tv);
    pane.add(table, BorderLayout.CENTER);
}

From source file:com.softenido.findrepe.showgroup.JImageDiffFrame.java

/** Creates new form JImageDiffFrame */
public JImageDiffFrame(ImageShowGroup sg) {
    initComponents();// w  ww .j  a  v a 2 s.co m
    this.setTitle("findrepe");
    this.sg = sg;
    model = null;
    table = new JTable(model);
    JScrollPane sc = new JScrollPane(table);
    jpnTable.add(sc);
}

From source file:edu.ku.brc.af.ui.forms.validation.FormValidatorInfo.java

/**
 * CReate a FormValidator.//from  w  w  w.  j  a va  2 s  .  c  o m
 * @param formViewObj the FormViewObj that will be validated
 */
public FormValidatorInfo(final FormValidator formValidator) {
    model = new FormValidatorInfoModel(formValidator);
    table = new JTable(model);

    this.formValidator = formValidator;

    setLayout(new BorderLayout());
    add(UIHelper.createScrollPane(table), BorderLayout.CENTER);
}