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:net.sf.jabref.openoffice.CitationManager.java

public CitationManager(final JabRefFrame frame, OOBibBase ooBase)
        throws NoSuchElementException, WrappedTargetException, UnknownPropertyException {
    diag = new JDialog(frame, Localization.lang("Manage citations"), true);
    this.ooBase = ooBase;

    list = new BasicEventList<>();
    XNameAccess nameAccess = ooBase.getReferenceMarks();
    java.util.List<String> names = ooBase.getJabRefReferenceMarks(nameAccess);
    for (String name : names) {
        list.add(new CitEntry(name,
                "<html>..." + ooBase.getCitationContext(nameAccess, name, 30, 30, true) + "...</html>",
                ooBase.getCustomProperty(name)));
    }//from  ww w .  ja  va 2s.c  o  m
    tableModel = new DefaultEventTableModel<>(list, new CitEntryFormat());
    table = new JTable(tableModel);
    diag.add(new JScrollPane(table), BorderLayout.CENTER);

    ButtonBarBuilder bb = new ButtonBarBuilder();
    bb.addGlue();
    JButton ok = new JButton(Localization.lang("OK"));
    bb.addButton(ok);
    JButton cancel = new JButton(Localization.lang("Cancel"));
    bb.addButton(cancel);
    bb.addGlue();
    bb.getPanel().setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    diag.add(bb.getPanel(), BorderLayout.SOUTH);

    diag.pack();
    diag.setSize(700, 400);

    ok.addActionListener(e -> {
        try {
            storeSettings();
        } catch (UnknownPropertyException | NotRemoveableException | PropertyExistException
                | IllegalTypeException | IllegalArgumentException ex) {
            LOGGER.warn("Problem modifying citation", ex);
            JOptionPane.showMessageDialog(frame, Localization.lang("Problem modifying citation"));
        }
        diag.dispose();
    });

    Action cancelAction = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            diag.dispose();
        }
    };
    cancel.addActionListener(cancelAction);

    bb.getPanel().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
            .put(Globals.getKeyPrefs().getKey(KeyBinding.CLOSE_DIALOG), "close");
    bb.getPanel().getActionMap().put("close", cancelAction);

    table.getColumnModel().getColumn(0).setPreferredWidth(600);
    table.getColumnModel().getColumn(1).setPreferredWidth(90);
    table.setPreferredScrollableViewportSize(new Dimension(700, 500));
    table.addMouseListener(new TableClickListener());
}

From source file:org.drugis.mtc.gui.results.ResultsComponentFactory.java

private static JTable createTableWithoutHeaders(NetworkRelativeEffectTableModel dm) {
    final JTable table = new JTable(dm);
    table.setTableHeader(null);//  w ww  .  ja v a 2s  .  com
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    for (final TableColumn c : Collections.list(table.getColumnModel().getColumns())) {
        c.setMinWidth(170);
        c.setPreferredWidth(170);
    }

    TableCopyHandler.registerCopyAction(table);
    return table;
}

From source file:logdruid.ui.table.StatRecordingEditorTable.java

/**
 * @wbp.parser.constructor/* w  ww .j  av a  2  s .c  o  m*/
 */
@SuppressWarnings("unchecked")
public StatRecordingEditorTable(JTextPane textPane) {
    super(new GridLayout(1, 0));

    model = new MyTableModel(data, header);
    table = new JTable(model);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);
    this.theLine = textPane.getText();
    this.examplePane = textPane;
    // Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    // Set up column sizes.
    initColumnSizes(table);

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

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

}

From source file:logdruid.ui.table.EventRecordingEditorTable.java

/**
 * @wbp.parser.constructor/*from   w w w  .  j a  va2  s .  co  m*/
 */
@SuppressWarnings("unchecked")
public EventRecordingEditorTable(JTextPane textPane) {
    super(new GridLayout(1, 0));

    model = new MyTableModel(data, header);
    table = new JTable(model);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.setFont(new Font("SansSerif", Font.PLAIN, 11));
    // table.setPreferredScrollableViewportSize(new Dimension(500, 200));
    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    table.setFillsViewportHeight(true);

    this.theLine = textPane.getText();
    this.examplePane = textPane;
    // Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    // Set up column sizes.
    initColumnSizes(table);

    // Fiddle with the type and processing column's cell editors/renderers.
    setUpTypeColumn(table, table.getColumnModel().getColumn(2));
    setUpProcessingColumn(table, table.getColumnModel().getColumn(3));

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

}

From source file:GenderEditor.java

public TableCellEditorJComboBox() {
    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.setCellEditor(new GenderEditor());
    JScrollPane jsp = new JScrollPane(table);
    pane.add(jsp, BorderLayout.CENTER);
}

From source file:edu.ku.brc.specify.dbsupport.cleanuptools.AgentNameCleanupParserDlg.java

@Override
public void createUI() {

    CellConstraints cc = new CellConstraints();
    PanelBuilder pb = new PanelBuilder(new FormLayout("f:p:g", "p,2px,f:p:g,2px,p"));

    this.contentPanel = pb.getPanel();

    setOkLabel(getResourceString("CLNUP_PROCESS"));
    //setCancelLabel(getResourceString("CLOSE"));
    super.createUI();

    fillModel();//w w w. ja  v  a  2  s  .c  o  m

    model = new NameDataModel();
    table = new JTable(model);

    autoResizeColWidth(table, model);

    pb.addSeparator(getResourceString("CLNUP_VERIFY_NMS"), cc.xy(1, 1));
    pb.add(createScrollPane(table, true), cc.xy(1, 3));

    PanelBuilder btnPB = new PanelBuilder(new FormLayout("f:p:g,p,f:p:g,p,f:p:g", "p")); //$NON-NLS-1$ //$NON-NLS-2$
    selectAllBtn = createI18NButton("SELECTALL"); //$NON-NLS-1$
    deselectAllBtn = createI18NButton("DESELECTALL"); //$NON-NLS-1$
    btnPB.add(selectAllBtn, cc.xy(2, 1));
    btnPB.add(deselectAllBtn, cc.xy(4, 1));

    pb.add(btnPB.getPanel(), cc.xy(1, 5));

    pb.setDefaultDialogBorder();

    pack();

    Dimension d = getPreferredSize();
    d.width += 150;
    setSize(d);

    selectAllBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            for (DataItem di : list) {
                di.setIncluded(true);
            }
            model.fireTableDataChanged();
        }
    });
    deselectAllBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            for (DataItem di : list) {
                di.setIncluded(false);
            }
            model.fireTableDataChanged();
        }
    });
}

From source file:components.SharedModelDemo.java

public SharedModelDemo() {
    super(new BorderLayout());

    Vector data = new Vector(7);
    String[] columnNames = { "French", "Spanish", "Italian" };
    String[] oneData = { "un", "uno", "uno" };
    String[] twoData = { "deux", "dos", "due" };
    String[] threeData = { "trois", "tres", "tre" };
    String[] fourData = { "quatre", "cuatro", "quattro" };
    String[] fiveData = { "cinq", "cinco", "cinque" };
    String[] sixData = { "six", "seis", "sei" };
    String[] sevenData = { "sept", "siete", "sette" };

    //Build the model.
    SharedDataModel dataModel = new SharedDataModel(columnNames);
    dataModel.addElement(oneData);//from  w w w . j a  va  2s  . co m
    dataModel.addElement(twoData);
    dataModel.addElement(threeData);
    dataModel.addElement(fourData);
    dataModel.addElement(fiveData);
    dataModel.addElement(sixData);
    dataModel.addElement(sevenData);

    list = new JList(dataModel);
    list.setCellRenderer(new DefaultListCellRenderer() {
        public Component getListCellRendererComponent(JList l, Object value, int i, boolean s, boolean f) {
            String[] array = (String[]) value;
            return super.getListCellRendererComponent(l, array[0], i, s, f);
        }
    });

    listSelectionModel = list.getSelectionModel();
    listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
    JScrollPane listPane = new JScrollPane(list);

    table = new JTable(dataModel);
    table.setSelectionModel(listSelectionModel);
    JScrollPane tablePane = new JScrollPane(table);

    //Build control area (use default FlowLayout).
    JPanel controlPane = new JPanel();
    String[] modes = { "SINGLE_SELECTION", "SINGLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL_SELECTION" };

    final JComboBox comboBox = new JComboBox(modes);
    comboBox.setSelectedIndex(2);
    comboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String newMode = (String) comboBox.getSelectedItem();
            if (newMode.equals("SINGLE_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            } else if (newMode.equals("SINGLE_INTERVAL_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            } else {
                listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            }
            output.append("----------" + "Mode: " + newMode + "----------" + newline);
        }
    });
    controlPane.add(new JLabel("Selection mode:"));
    controlPane.add(comboBox);

    //Build output area.
    output = new JTextArea(10, 40);
    output.setEditable(false);
    JScrollPane outputPane = new JScrollPane(output, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

    //Do the layout.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    add(splitPane, BorderLayout.CENTER);

    JPanel topHalf = new JPanel();
    topHalf.setLayout(new BoxLayout(topHalf, BoxLayout.X_AXIS));
    JPanel listContainer = new JPanel(new GridLayout(1, 1));
    listContainer.setBorder(BorderFactory.createTitledBorder("List"));
    listContainer.add(listPane);
    JPanel tableContainer = new JPanel(new GridLayout(1, 1));
    tableContainer.setBorder(BorderFactory.createTitledBorder("Table"));
    tableContainer.add(tablePane);
    tablePane.setPreferredSize(new Dimension(300, 100));
    topHalf.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
    topHalf.add(listContainer);
    topHalf.add(tableContainer);

    topHalf.setMinimumSize(new Dimension(400, 50));
    topHalf.setPreferredSize(new Dimension(400, 110));
    splitPane.add(topHalf);

    JPanel bottomHalf = new JPanel(new BorderLayout());
    bottomHalf.add(controlPane, BorderLayout.NORTH);
    bottomHalf.add(outputPane, BorderLayout.CENTER);
    //XXX: next line needed if bottomHalf is a scroll pane:
    //bottomHalf.setMinimumSize(new Dimension(400, 50));
    bottomHalf.setPreferredSize(new Dimension(450, 135));
    splitPane.add(bottomHalf);
}

From source file:net.sf.jabref.gui.openoffice.CitationManager.java

public CitationManager(final JabRefFrame frame, OOBibBase ooBase)
        throws NoSuchElementException, WrappedTargetException, UnknownPropertyException {
    diag = new JDialog(frame, Localization.lang("Manage citations"), true);
    this.ooBase = ooBase;

    list = new BasicEventList<>();
    XNameAccess nameAccess = ooBase.getReferenceMarks();
    List<String> names = ooBase.getJabRefReferenceMarks(nameAccess);
    for (String name : names) {
        list.add(new CitationEntry(name,
                "<html>..." + ooBase.getCitationContext(nameAccess, name, 30, 30, true) + "...</html>",
                ooBase.getCustomProperty(name)));
    }//  w ww  .  j  av a  2s . c  om
    tableModel = new DefaultEventTableModel<>(list, new CitationEntryFormat());
    table = new JTable(tableModel);
    diag.add(new JScrollPane(table), BorderLayout.CENTER);

    ButtonBarBuilder bb = new ButtonBarBuilder();
    bb.addGlue();
    JButton ok = new JButton(Localization.lang("OK"));
    bb.addButton(ok);
    JButton cancel = new JButton(Localization.lang("Cancel"));
    bb.addButton(cancel);
    bb.addGlue();
    bb.getPanel().setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    diag.add(bb.getPanel(), BorderLayout.SOUTH);

    diag.pack();
    diag.setSize(700, 400);

    ok.addActionListener(e -> {
        try {
            storeSettings();
        } catch (UnknownPropertyException | NotRemoveableException | PropertyExistException
                | IllegalTypeException | IllegalArgumentException ex) {
            LOGGER.warn("Problem modifying citation", ex);
            JOptionPane.showMessageDialog(frame, Localization.lang("Problem modifying citation"));
        }
        diag.dispose();
    });

    Action cancelAction = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            diag.dispose();
        }
    };
    cancel.addActionListener(cancelAction);

    bb.getPanel().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
            .put(Globals.getKeyPrefs().getKey(KeyBinding.CLOSE_DIALOG), "close");
    bb.getPanel().getActionMap().put("close", cancelAction);

    table.getColumnModel().getColumn(0).setPreferredWidth(580);
    table.getColumnModel().getColumn(1).setPreferredWidth(110);
    table.setPreferredScrollableViewportSize(new Dimension(700, 500));
    table.addMouseListener(new TableClickListener());
}

From source file:FillViewportHeightDemo.java

public FillViewportHeightDemo() {
    super("Empty Table DnD Demo");

    tableModel = getDefaultTableModel();
    table = new JTable(tableModel);
    table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    table.setDropMode(DropMode.INSERT_ROWS);

    table.setTransferHandler(new TransferHandler() {

        public boolean canImport(TransferSupport support) {
            // for the demo, we'll only support drops (not clipboard paste)
            if (!support.isDrop()) {
                return false;
            }//  www .  j a  v a  2 s .com

            // we only import Strings
            if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                return false;
            }

            return true;
        }

        public boolean importData(TransferSupport support) {
            // if we can't handle the import, say so
            if (!canImport(support)) {
                return false;
            }

            // fetch the drop location
            JTable.DropLocation dl = (JTable.DropLocation) support.getDropLocation();

            int row = dl.getRow();

            // fetch the data and bail if this fails
            String data;
            try {
                data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
            } catch (UnsupportedFlavorException e) {
                return false;
            } catch (IOException e) {
                return false;
            }

            String[] rowData = data.split(",");
            tableModel.insertRow(row, rowData);

            Rectangle rect = table.getCellRect(row, 0, false);
            if (rect != null) {
                table.scrollRectToVisible(rect);
            }

            // demo stuff - remove for blog
            model.removeAllElements();
            model.insertElementAt(getNextString(count++), 0);
            // end demo stuff

            return true;
        }
    });

    JList dragFrom = new JList(model);
    dragFrom.setFocusable(false);
    dragFrom.setPrototypeCellValue(getNextString(100));
    model.insertElementAt(getNextString(count++), 0);
    dragFrom.setDragEnabled(true);
    dragFrom.setBorder(BorderFactory.createLoweredBevelBorder());

    dragFrom.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent me) {
            if (SwingUtilities.isLeftMouseButton(me) && me.getClickCount() % 2 == 0) {
                String text = (String) model.getElementAt(0);
                String[] rowData = text.split(",");
                tableModel.insertRow(table.getRowCount(), rowData);
                model.removeAllElements();
                model.insertElementAt(getNextString(count++), 0);
            }
        }
    });

    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
    JPanel wrap = new JPanel();
    wrap.add(new JLabel("Drag from here:"));
    wrap.add(dragFrom);
    p.add(Box.createHorizontalStrut(4));
    p.add(Box.createGlue());
    p.add(wrap);
    p.add(Box.createGlue());
    p.add(Box.createHorizontalStrut(4));
    getContentPane().add(p, BorderLayout.NORTH);

    JScrollPane sp = new JScrollPane(table);
    getContentPane().add(sp, BorderLayout.CENTER);
    fillBox = new JCheckBoxMenuItem("Fill Viewport Height");
    fillBox.addActionListener(this);

    JMenuBar mb = new JMenuBar();
    JMenu options = new JMenu("Options");
    mb.add(options);
    setJMenuBar(mb);

    JMenuItem clear = new JMenuItem("Reset");
    clear.addActionListener(this);
    options.add(clear);
    options.add(fillBox);

    getContentPane().setPreferredSize(new Dimension(260, 180));
}

From source file:gui.TraitViewerDialog.java

private JPanel createControls() {
    model = new TraitTableModel();
    for (int i = 0; i < tFile.getNames().size(); i++) {
        model.addRow(new Object[] { i + 1, tFile.getEnabled().get(i), tFile.getNames().get(i) });
    }/*from  w ww. ja v  a  2s.com*/

    table = new JTable(model);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.getColumnModel().getColumn(0).setPreferredWidth(13);
    table.getColumnModel().getColumn(1).setPreferredWidth(30);
    table.getColumnModel().getColumn(2).setPreferredWidth(130);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.getSelectionModel().addListSelectionListener(this);

    splits = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    splits.setLeftComponent(new JScrollPane(table));

    splits.setDividerLocation(250);
    splits.setResizeWeight(0.5);
    splits.setRightComponent(new JPanel());
    JPanel p1 = new JPanel(new BorderLayout());
    p1.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 10));
    p1.add(splits);

    return p1;
}