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:de.codesourcery.eve.skills.ui.components.impl.planning.ShoppingListEditorComponent.java

protected ShoppingListEditorComponent(ShoppingList shoppingList, ShoppingList existing) {
    tableModel = new ShoppingListModel(shoppingList);
    table = new JTable(tableModel);
    table.setRowSorter(tableModel.getRowSorter());

    title.setText(shoppingList.getTitle());
    description.setText(shoppingList.getDescription());
    this.existing = existing;
}

From source file:org.jfree.chart.demo.selection.SelectionDemo5Category.java

public SelectionDemo5Category(String title) {
    super(title);
    JPanel chartPanel = createDemoPanel();
    chartPanel.setPreferredSize(new Dimension(500, 270));

    JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    split.add(chartPanel);/* w w w . j  a va 2s  .  com*/

    this.model = new DefaultTableModel(new String[] { "row:", "column:", "value:" }, 0);
    this.table = new JTable(this.model);
    TableColumnModel tcm = this.table.getColumnModel();
    JPanel p = new JPanel(new BorderLayout());
    JScrollPane scroller = new JScrollPane(this.table);
    p.add(scroller);
    p.setBorder(BorderFactory.createCompoundBorder(new TitledBorder("Selected Items: "),
            new EmptyBorder(4, 4, 4, 4)));
    split.add(p);
    setContentPane(split);
}

From source file:org.jfree.chart.demo.selection.SelectionDemo4.java

/**
 * Creates a new demo.//from w  ww.j  a  v  a 2s  .  c om
 * 
 * @param title  the frame title.
 */
public SelectionDemo4(String title) {
    super(title);
    ChartPanel chartPanel = (ChartPanel) createDemoPanel();
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));

    JFreeChart chart = chartPanel.getChart();
    XYPlot plot = (XYPlot) chart.getPlot();
    this.dataset = (HistogramDataset) plot.getDataset();
    JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    split.add(chartPanel);

    this.model = new DefaultTableModel(new String[] { "Item:", "Bin Start:", "Bin End:", "Value:" }, 0);
    this.table = new JTable(this.model);
    JPanel p = new JPanel(new BorderLayout());
    JScrollPane scroller = new JScrollPane(this.table);
    p.add(scroller);
    p.setBorder(BorderFactory.createCompoundBorder(new TitledBorder("Selected Items: "),
            new EmptyBorder(4, 4, 4, 4)));
    split.add(p);
    setContentPane(split);
}

From source file:org.jfree.chart.demo.selection.SelectionDemo2.java

/**
 * A demonstration application showing a scatter plot.
 *
 * @param title  the frame title.//w w  w.  j  av  a 2  s  .c  o  m
 */
public SelectionDemo2(String title) {
    super(title);
    ChartPanel chartPanel = (ChartPanel) createDemoPanel();
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));

    JFreeChart chart = chartPanel.getChart();
    XYPlot plot = (XYPlot) chart.getPlot();
    this.dataset = (XYSeriesCollection) plot.getDataset();
    JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    split.add(chartPanel);

    this.model = new DefaultTableModel(new String[] { "Series:", "Item:", "X:", "Y:" }, 0);
    this.table = new JTable(this.model);
    TableColumnModel tcm = this.table.getColumnModel();
    tcm.getColumn(2).setCellRenderer(new NumberCellRenderer());
    tcm.getColumn(3).setCellRenderer(new NumberCellRenderer());
    JPanel p = new JPanel(new BorderLayout());
    JScrollPane scroller = new JScrollPane(this.table);
    p.add(scroller);
    p.setBorder(BorderFactory.createCompoundBorder(new TitledBorder("Selected Items: "),
            new EmptyBorder(4, 4, 4, 4)));
    split.add(p);
    setContentPane(split);
}

From source file:org.jfree.chart.demo.selection.SelectionDemo3.java

/**
 * A demonstration application showing a scatter plot.
 *
 * @param title  the frame title./* w w w. j  av a 2s .c  o  m*/
 */
public SelectionDemo3(String title) {
    super(title);
    ChartPanel chartPanel = (ChartPanel) createDemoPanel();
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));

    JFreeChart chart = chartPanel.getChart();
    XYPlot plot = (XYPlot) chart.getPlot();
    this.dataset = (XYSeriesCollection) plot.getDataset();
    JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    split.add(chartPanel);

    this.model = new DefaultTableModel(new String[] { "Series:", "Item:", "X:", "Y:" }, 0);
    this.table = new JTable(this.model);
    TableColumnModel tcm = this.table.getColumnModel();
    tcm.getColumn(2).setCellRenderer(new NumberCellRenderer());
    tcm.getColumn(3).setCellRenderer(new NumberCellRenderer());
    JPanel p = new JPanel(new BorderLayout());
    JScrollPane scroller = new JScrollPane(this.table);
    p.add(scroller);
    p.setBorder(BorderFactory.createCompoundBorder(new TitledBorder("Selected Items: "),
            new EmptyBorder(4, 4, 4, 4)));
    split.add(p);
    setContentPane(split);
}

From source file:channel.metadata.ChannelMetadataPanel.java

/**
 * Initializes the panel/*w ww  .j av  a  2  s . c  om*/
 */
private void init() {
    setLayout(new MigLayout("insets 0 0 0 0", "[grow,fill]", "[grow,fill]"));

    mTable = new JTable(mChannelProcessingManager.getChannelMetadataModel());

    DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) mTable.getDefaultRenderer(String.class);
    renderer.setHorizontalAlignment(SwingConstants.CENTER);

    mTable.getSelectionModel().addListSelectionListener(this);
    mTable.addMouseListener(new MouseSupport());

    mTable.getColumnModel().getColumn(ChannelMetadataModel.COLUMN_STATE)
            .setCellRenderer(new ColoredStateCellRenderer());
    mTable.getColumnModel().getColumn(ChannelMetadataModel.COLUMN_PRIMARY_TO)
            .setCellRenderer(new AliasedValueCellRenderer(Attribute.PRIMARY_ADDRESS_TO));
    mTable.getColumnModel().getColumn(ChannelMetadataModel.COLUMN_PRIMARY_FROM)
            .setCellRenderer(new AliasedValueCellRenderer(Attribute.PRIMARY_ADDRESS_FROM));
    mTable.getColumnModel().getColumn(ChannelMetadataModel.COLUMN_SECONDARY_TO)
            .setCellRenderer(new AliasedValueCellRenderer(Attribute.SECONDARY_ADDRESS_TO));
    mTable.getColumnModel().getColumn(ChannelMetadataModel.COLUMN_SECONDARY_FROM)
            .setCellRenderer(new AliasedValueCellRenderer(Attribute.SECONDARY_ADDRESS_FROM));

    JScrollPane scrollPane = new JScrollPane(mTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    add(scrollPane);

    //        new TableColumnWidthMonitor(mTable, PROPERTY_PREFIX, new int[] {15,15,15,15,25,25,25,25,15,40});
}

From source file:net.sf.jabref.importer.ZipFileChooser.java

/**
 * New Zip file chooser.//from ww w . j  a  va  2s.c o  m
 *
 * @param owner  Owner of the file chooser
 * @param zipFile  Zip-Fle to choose from, must be readable
 */
public ZipFileChooser(ImportCustomizationDialog importCustomizationDialog, ZipFile zipFile) {
    super(importCustomizationDialog, Localization.lang("Select file from ZIP-archive"), false);

    ZipFileChooserTableModel tableModel = new ZipFileChooserTableModel(zipFile,
            getSelectableZipEntries(zipFile));
    JTable table = new JTable(tableModel);
    TableColumnModel cm = table.getColumnModel();
    cm.getColumn(0).setPreferredWidth(200);
    cm.getColumn(1).setPreferredWidth(150);
    cm.getColumn(2).setPreferredWidth(100);
    JScrollPane sp = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.setPreferredScrollableViewportSize(new Dimension(500, 150));
    if (table.getRowCount() > 0) {
        table.setRowSelectionInterval(0, 0);
    }

    // cancel: no entry is selected
    JButton cancelButton = new JButton(Localization.lang("Cancel"));
    cancelButton.addActionListener(e -> dispose());
    // ok: get selected class and check if it is instantiable as an importer
    JButton okButton = new JButton(Localization.lang("OK"));
    okButton.addActionListener(e -> {
        int row = table.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(this, Localization.lang("Please select an importer."));
        } else {
            ZipFileChooserTableModel model = (ZipFileChooserTableModel) table.getModel();
            ZipEntry tempZipEntry = model.getZipEntry(row);
            CustomImporter importer = new CustomImporter();
            importer.setBasePath(model.getZipFile().getName());
            String className = tempZipEntry.getName().substring(0, tempZipEntry.getName().lastIndexOf('.'))
                    .replace("/", ".");
            importer.setClassName(className);
            try {
                ImportFormat importFormat = importer.getInstance();
                importer.setName(importFormat.getFormatName());
                importer.setCliId(importFormat.getId());
                importCustomizationDialog.addOrReplaceImporter(importer);
                dispose();
            } catch (IOException | ClassNotFoundException | InstantiationException
                    | IllegalAccessException exc) {
                LOGGER.warn("Could not instantiate importer: " + importer.getName(), exc);
                JOptionPane.showMessageDialog(this, Localization.lang("Could not instantiate %0 %1",
                        importer.getName() + ":\n", exc.getMessage()));
            }
        }
    });

    // Key bindings:
    JPanel mainPanel = new JPanel();
    //ActionMap am = mainPanel.getActionMap();
    //InputMap im = mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    //im.put(Globals.getKeyPrefs().getKey(KeyBinds.CLOSE_DIALOG), "close");
    //am.put("close", closeAction);
    mainPanel.setLayout(new BorderLayout());
    mainPanel.add(sp, BorderLayout.CENTER);

    JPanel optionsPanel = new JPanel();
    optionsPanel.add(okButton);
    optionsPanel.add(cancelButton);
    optionsPanel.add(Box.createHorizontalStrut(5));

    getContentPane().add(mainPanel, BorderLayout.CENTER);
    getContentPane().add(optionsPanel, BorderLayout.SOUTH);
    this.setSize(getSize());
    pack();
    this.setLocationRelativeTo(importCustomizationDialog);
    new FocusRequester(table);
}

From source file:TableToolTipsDemo.java

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

    JTable table = new JTable(new MyTableModel()) {

        //Implement table cell tool tips.
        public String getToolTipText(MouseEvent e) {
            String tip = null;//  w ww . j a v  a  2 s . co  m
            java.awt.Point p = e.getPoint();
            int rowIndex = rowAtPoint(p);
            int colIndex = columnAtPoint(p);
            int realColumnIndex = convertColumnIndexToModel(colIndex);

            if (realColumnIndex == 2) { //Sport column
                tip = "This person's favorite sport to " + "participate in is: "
                        + getValueAt(rowIndex, colIndex);
            } else if (realColumnIndex == 4) { //Veggie column
                TableModel model = getModel();
                String firstName = (String) model.getValueAt(rowIndex, 0);
                String lastName = (String) model.getValueAt(rowIndex, 1);
                Boolean veggie = (Boolean) model.getValueAt(rowIndex, 4);
                if (Boolean.TRUE.equals(veggie)) {
                    tip = firstName + " " + lastName + " is a vegetarian";
                } else {
                    tip = firstName + " " + lastName + " is not a vegetarian";
                }
            } else {
                //You can omit this part if you know you don't
                //have any renderers that supply their own tool
                //tips.
                tip = super.getToolTipText(e);
            }
            return tip;
        }

        //Implement table header tool tips.
        protected JTableHeader createDefaultTableHeader() {
            return new JTableHeader(columnModel) {
                public String getToolTipText(MouseEvent e) {
                    String tip = null;
                    java.awt.Point p = e.getPoint();
                    int index = columnModel.getColumnIndexAtX(p.x);
                    int realIndex = columnModel.getColumn(index).getModelIndex();
                    return columnToolTips[realIndex];
                }
            };
        }
    };

    table.setPreferredScrollableViewportSize(new Dimension(500, 70));

    //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:org.jfree.chart.demo.selection.SelectionDemo1.java

/**
 * A demonstration application showing how to create a simple time series
 * chart. This example uses monthly data.
 * //w w w  . ja v  a 2s . c  o  m
 * @param title  the frame title.
 */
public SelectionDemo1(String title) {
    super(title);
    ChartPanel chartPanel = (ChartPanel) createDemoPanel();
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));

    JFreeChart chart = chartPanel.getChart();
    XYPlot plot = (XYPlot) chart.getPlot();
    this.dataset = (TimeSeriesCollection) plot.getDataset();
    JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    split.add(chartPanel);

    this.model = new DefaultTableModel(new String[] { "Series:", "Item:", "Period:", "Value:" }, 0);
    this.table = new JTable(this.model);
    TableColumnModel tcm = this.table.getColumnModel();
    tcm.getColumn(3).setCellRenderer(new NumberCellRenderer());
    JPanel p = new JPanel(new BorderLayout());
    JScrollPane scroller = new JScrollPane(this.table);
    p.add(scroller);
    p.setBorder(BorderFactory.createCompoundBorder(new TitledBorder("Selected Items: "),
            new EmptyBorder(4, 4, 4, 4)));
    split.add(p);
    setContentPane(split);

}

From source file:org.cytoscape.dyn.internal.graphMetrics.GraphMetricsPanel.java

public GraphMetricsPanel(org.cytoscape.dyn.internal.CyActivator<T, C> cyActivator,
        DynNetwork<T> dynamicNetwork) {

    this.cyactivator = cyActivator;
    this.dynamicNetwork = dynamicNetwork;
    attributesTable = new JTable(new MyTableModel(dynamicNetwork.getNodeAttributes()));
    attributesTable.setPreferredScrollableViewportSize(new Dimension(300, 400));
    attributesTable.setFillsViewportHeight(true);
    attributesTable.setShowGrid(false);//ww w  . j a va 2  s.com
    JScrollPane tablePanel = new JScrollPane(attributesTable);
    tablePanel.setSize(new Dimension(250, 400));

    edgeAttributesTable = new JTable(new MyTableModel(dynamicNetwork.getEdgeAttributes()));
    edgeAttributesTable.setPreferredScrollableViewportSize(new Dimension(300, 400));
    edgeAttributesTable.setFillsViewportHeight(true);
    edgeAttributesTable.setShowGrid(false);
    JScrollPane edgeTablePanel = new JScrollPane(edgeAttributesTable);
    edgeTablePanel.setSize(new Dimension(250, 400));

    plotChartButton = new JButton("Plot Selected Attributes");
    closeTab = new JButton("Close Tab");

    plotChartButton.addActionListener(this);
    closeTab.addActionListener(this);

    buttonPanel = new JPanel();

    buttonPanel.setLayout(new FlowLayout());

    buttonPanel.add(plotChartButton);
    buttonPanel.add(closeTab);
    buttonPanel.setBorder(BorderFactory.createTitledBorder(null, "Options", TitledBorder.DEFAULT_JUSTIFICATION,
            TitledBorder.DEFAULT_POSITION, new Font("SansSerif", 0, 12), Color.darkGray));

    tablePanel.setBorder(
            BorderFactory.createTitledBorder(null, "Node Attributes", TitledBorder.DEFAULT_JUSTIFICATION,
                    TitledBorder.DEFAULT_POSITION, new Font("SansSerif", 0, 12), Color.darkGray));

    edgeTablePanel.setBorder(
            BorderFactory.createTitledBorder(null, "Edge Attributes", TitledBorder.DEFAULT_JUSTIFICATION,
                    TitledBorder.DEFAULT_POSITION, new Font("SansSerif", 0, 12), Color.darkGray));

    GroupLayout cytoLayout = new GroupLayout(this);
    this.setLayout(cytoLayout);
    this.add(tablePanel);
    this.add(buttonPanel);

    cytoLayout.setHorizontalGroup(cytoLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(tablePanel, GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
            .addComponent(edgeTablePanel, GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
            .addComponent(buttonPanel, GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE));
    cytoLayout.setVerticalGroup(
            cytoLayout.createSequentialGroup().addComponent(tablePanel, 200, GroupLayout.DEFAULT_SIZE, 300)
                    .addComponent(edgeTablePanel, 200, GroupLayout.DEFAULT_SIZE, 300)
                    .addComponent(buttonPanel, GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE));

    this.setVisible(true);
}