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

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

From source file:FolderViewer.java

public FolderViewer(Folder what) {
    super(new GridLayout(1, 1));

    table = new JTable(model);
    table.setShowGrid(false);// w  w w .j  a  v  a  2  s .  c o  m

    scrollpane = new JScrollPane(table);

    // setup the folder we were given
    setFolder(what);

    // find out what is pressed
    table.getSelectionModel().addListSelectionListener(new FolderPressed());
    scrollpane.setPreferredSize(new Dimension(700, 300));
    add(scrollpane);
}

From source file:components.TableSortDemo.java

public TableSortDemo() {
    super(new GridLayout(1, 0));

    JTable table = new JTable(new MyTableModel());
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);/*  w  w  w .j a  v  a2  s. co  m*/
    table.setAutoCreateRowSorter(true);

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Add the scroll pane to this panel.
    add(scrollPane);
}

From source file:com.emental.mindraider.ui.outline.OutlineArchiveJPanel.java

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

    // table with archived concepts (title)
    // let table model to load discarded concepts itself
    tableModel = new ArchiveTableModel(this);
    table = new JTable(tableModel);
    table.setAutoCreateRowSorter(true);/*from  w  ww  . j  av a 2  s . c om*/
    JScrollPane scroll = new JScrollPane(table);
    table.setFillsViewportHeight(true);
    table.setShowHorizontalLines(false);
    table.setShowVerticalLines(false);
    table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    JToolBar toolbar = new JToolBar();

    undoButton = new JButton("", IconsRegistry.getImageIcon("trashUndo.png"));
    undoButton.setEnabled(false);
    undoButton.setToolTipText("Restore Note");
    undoButton.addActionListener(new UndiscardConceptActionListener(tableModel, table));
    toolbar.add(undoButton);

    deleteButton = new JButton("", IconsRegistry.getImageIcon("explorerDeleteSmall.png"));
    deleteButton.setEnabled(true);
    deleteButton.setToolTipText("Delete Note");
    deleteButton.addActionListener(new DeleteConceptActionListener(tableModel, table));
    toolbar.add(deleteButton);

    purgeButton = new JButton("", IconsRegistry.getImageIcon("trashFull.png"));
    purgeButton.setEnabled(true);
    purgeButton.setToolTipText("Empty Notes Archive");
    purgeButton.addActionListener(new EmptyArchiveActionListener());
    toolbar.add(purgeButton);

    add(toolbar, BorderLayout.NORTH);

    add(scroll, BorderLayout.CENTER);
}

From source file:components.TableDemo.java

public TableDemo() {
    super(new GridLayout(1, 0));

    JTable table = new JTable(new MyTableModel());
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);// ww w  .java  2 s  . c  o  m

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Add the scroll pane to this panel.
    add(scrollPane);
}

From source file:TableIt.java

public TableIt() {
    JFrame f = new JFrame();
    TableModel tm = new MyTableModel();
    final JTable table = new JTable(tm);

    TableColumnModel tcm = table.getColumnModel();
    TableColumn column = tcm.getColumn(tcm.getColumnCount() - 1);
    TableCellRenderer renderer = new MyTableCellRenderer();
    column.setCellRenderer(renderer);//from   w  w w  .j a  v  a2  s.  c o m

    JButton selectionType = new JButton("Next Type");
    selectionType.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            boolean rowSet = table.getRowSelectionAllowed();
            boolean colSet = table.getColumnSelectionAllowed();
            boolean cellSet = table.getCellSelectionEnabled();

            boolean setRow = !rowSet;
            boolean setCol = rowSet ^ colSet;
            boolean setCell = rowSet & colSet;

            table.setCellSelectionEnabled(setCell);
            table.setColumnSelectionAllowed(setCol);
            table.setRowSelectionAllowed(setRow);
            System.out.println("Row Selection Allowed? " + setRow);
            System.out.println("Column Selection Allowed? " + setCol);
            System.out.println("Cell Selection Enabled? " + setCell);
            table.repaint();
        }
    });
    JButton selectionMode = new JButton("Next Mode");
    selectionMode.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ListSelectionModel lsm = table.getSelectionModel();
            int mode = lsm.getSelectionMode();
            int nextMode;
            String nextModeString;
            if (mode == ListSelectionModel.SINGLE_SELECTION) {
                nextMode = ListSelectionModel.SINGLE_INTERVAL_SELECTION;
                nextModeString = "Single Interval Selection";
            } else if (mode == ListSelectionModel.SINGLE_INTERVAL_SELECTION) {
                nextMode = ListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
                nextModeString = "Multiple Interval Selection";
            } else {
                nextMode = ListSelectionModel.SINGLE_SELECTION;
                nextModeString = "Single Selection";
            }
            lsm.setSelectionMode(nextMode);
            System.out.println("Selection Mode: " + nextModeString);
            table.repaint();
        }
    });
    JPanel jp = new JPanel();
    jp.add(selectionType);
    jp.add(selectionMode);
    JScrollPane jsp = new JScrollPane(table);
    Container c = f.getContentPane();
    c.add(jsp, BorderLayout.CENTER);
    c.add(jp, BorderLayout.SOUTH);
    f.setSize(300, 250);
    f.show();
}

From source file:TableRenderDemo.java

public TableRenderDemo() {
    super(new GridLayout(1, 0));

    JTable table = new JTable(new MyTableModel());
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));

    // Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    // Set up column sizes.
    initColumnSizes(table);/*from  w  w w .j  av  a2  s.c  o  m*/

    // Fiddle with the Sport column's cell editors/renderers.
    setUpSportColumn(table, table.getColumnModel().getColumn(2));

    // Add the scroll pane to this panel.
    add(scrollPane);
}

From source file:com.intuit.tank.tools.debugger.VariableDialog.java

public VariableDialog(AgentDebuggerFrame f, Map<String, String> variables) {
    super(f, true);
    this.f = f;//from   w  w  w .ja  va2 s.c om
    setLayout(new BorderLayout());
    setTitle("View Edit Project Variables");
    DefaultTableModel model = new DefaultTableModel();
    model.addColumn("Variable Name");
    model.addColumn("Variable Value");
    List<String> keys = new ArrayList<String>(variables.keySet());
    Collections.sort(keys);
    for (String key : keys) {
        Object[] data = new Object[2];
        data[0] = key;
        data[1] = variables.get(key);
        model.addRow(data);
    }
    table = new JTable(model);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.getSelectionModel().addListSelectionListener(this);
    table.setGridColor(Color.GRAY);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    table.setBorder(BorderFactory.createLineBorder(Color.GRAY));
    table.setShowGrid(true);
    table.getTableHeader().setReorderingAllowed(false);

    JScrollPane sp = new JScrollPane(table);
    JPanel panel = new JPanel(new BorderLayout());

    panel.add(table.getTableHeader(), BorderLayout.NORTH);
    panel.add(sp, BorderLayout.CENTER);

    add(panel, BorderLayout.CENTER);
    add(createButtonPanel(), BorderLayout.SOUTH);
    setSize(new Dimension(800, 600));
    setBounds(new Rectangle(getSize()));
    setPreferredSize(getSize());
    WindowUtil.centerOnParent(this);
}

From source file:components.TableFTFEditDemo.java

public TableFTFEditDemo() {
    super(new GridLayout(1, 0));

    JTable table = new JTable(new MyTableModel());
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);/*  w ww .ja  v a 2  s  .co m*/

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Set up stricter input validation for the integer column.
    table.setDefaultEditor(Integer.class, new IntegerEditor(0, 100));

    //If we didn't want this editor to be used for other
    //Integer columns, we'd do this:
    //table.getColumnModel().getColumn(3).setCellEditor(
    //   new IntegerEditor(0, 100));

    //Add the scroll pane to this panel.
    add(scrollPane);
}

From source file:components.TablePrintDemo.java

public TablePrintDemo() {
    super();//from  w w w  .  jav  a 2 s.com
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

    table = new JTable(new MyTableModel());
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Add the scroll pane to this panel.
    add(scrollPane);

    //Add a print button.
    JButton printButton = new JButton("Print");
    printButton.setAlignmentX(Component.CENTER_ALIGNMENT);
    printButton.addActionListener(this);
    add(printButton);

}