Java Code Examples for javax.swing.JTable#getModel()

The following examples show how to use javax.swing.JTable#getModel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TableCheckBoxColumn.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 7 votes vote down vote up
@Override
public void mouseClicked(MouseEvent e) {
    JTableHeader header = (JTableHeader) e.getSource();
    JTable table = header.getTable();
    TableColumnModel columnModel = table.getColumnModel();
    int vci = columnModel.getColumnIndexAtX(e.getX());
    int mci = table.convertColumnIndexToModel(vci);
    if (mci == targetColumnIndex) {
        if (SwingUtilities.isLeftMouseButton(e)) {
            TableColumn column = columnModel.getColumn(vci);
            Object v = column.getHeaderValue();
            boolean b = Status.DESELECTED.equals(v);
            TableModel m = table.getModel();
            for (int i = 0; i < m.getRowCount(); i++) {
                m.setValueAt(b, i, mci);
            }
            column.setHeaderValue(b ? Status.SELECTED : Status.DESELECTED);
        } else if (SwingUtilities.isRightMouseButton(e)) {
            if (popupMenu != null) {
                popupMenu.show(table, e.getX(), 0);
            }
        }
    }
}
 
Example 2
Source File: ManageCalibration.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
private void refreshTimes(JTable table) {
    final TableModel model = table.getModel();
    for (int row = 0; row < model.getRowCount(); row++) {
        String javaPlatform = javaPlatforms[row];
        Long modified = null;
        String s = CalibrationSupport.getCalibrationDataFileName(javaPlatform);
        if (s != null) {
            File f = new File(s);
            if (f.isFile()) modified = Long.valueOf(f.lastModified());
        }
        final int index = row;
        final Long _modified = modified;
        SwingUtilities.invokeLater(new Runnable() {
            public void run() { model.setValueAt(_modified, index, 1); }
        });
    }
}
 
Example 3
Source File: TableViewTopComponent.java    From constellation with Apache License 2.0 6 votes vote down vote up
private void setDefaultColumns(final JTable table) {
    final GraphTableModel model = (GraphTableModel) table.getModel();
    final TableColumnModel tcm = table.getColumnModel();

    // A blank graph with no attributes still has dummy attributes in the table model.
    // When new attributes are added, we only only want to add them to the table if the only
    // existing attributes are dummies.
    final boolean addDefaults
            = currentElementType == GraphElementType.VERTEX && tcm.getColumnCount() <= 1
            || currentElementType == GraphElementType.TRANSACTION && tcm.getColumnCount() <= 3;

    if (addDefaults) {
        for (int index = 0; index < model.getColumnCount(); index++) {
            final String name = model.getColumnName(index);
            if (isImportant(name)) {
                final TableColumn tc = new TableColumn(index);
                tc.setHeaderValue(name);
                tcm.addColumn(tc);
            }
        }
    }
}
 
Example 4
Source File: MessagesTableCellRender.java    From quickfix-messenger with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
		boolean isSelected, boolean hasFocus, int row, int column)
{
	JLabel component = (JLabel) super.getTableCellRendererComponent(table,
			value, isSelected, hasFocus, row, column);

	if (!isSelected)
	{
		int modelRow = table.convertRowIndexToModel(row);
		MessagesTableModel model = (MessagesTableModel) table.getModel();
		MessagesTableModelData data = model.getData(modelRow);

		if (data.getDirection().equals(QFixMessageListener.RECV))
		{
			component.setBackground(Color.ORANGE);
		} else
		{
			component.setBackground(Color.GREEN);
		}
	}

	return component;
}
 
Example 5
Source File: LabelActionTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testShowLabelHistory() {
	env.open(program);

	cb.goTo(new LabelFieldLocation(program, program.getAddressFactory().getAddress("0x1002d2b"),
		"AnotherLocal", null, 0));
	ProgramLocation loc = cb.getCurrentLocation();
	assertEquals(0x01002d2b, loc.getAddress().getOffset());
	assertTrue(loc instanceof LabelFieldLocation);

	LabelMgrPlugin labelPlugin = getPlugin(tool, LabelMgrPlugin.class);
	DockingActionIf historyAction = getAction(labelPlugin, "Show Label History");
	performAction(historyAction, cb.getProvider(), false);

	DialogComponentProvider provider = waitForDialogComponent(DialogComponentProvider.class);
	JComponent historyPanel = (JComponent) TestUtils.getInstanceField("workPanel", provider);
	JTable table = (JTable) TestUtils.getInstanceField("historyTable", historyPanel);
	TableModel model = table.getModel();
	Object label = model.getValueAt(0, 1);
	assertEquals(label, "AnotherLocal");

	Object author = model.getValueAt(0, 2);
	assertTrue(author.toString().startsWith(System.getProperty("user.name")));
}
 
Example 6
Source File: TableUtils.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private static SortedTableModel getSortedTableModel(JTable table) {
	TableModel model = table.getModel();
	if (!(model instanceof SortedTableModel)) {
		return null;
	}
	return (SortedTableModel) model;
}
 
Example 7
Source File: SwingUtil.java    From Repeat with Apache License 2.0 5 votes vote down vote up
public static void ensureRowNumber(JTable table, int maxSize) {
	// Make sure enough space
	if (maxSize > table.getRowCount()) {
		int rowCount = table.getRowCount();
		// Add more rows
		DefaultTableModel model = (DefaultTableModel) table.getModel();
		for (int i = 0; i < maxSize - rowCount; i++) {
			model.addRow(new Object[model.getColumnCount()]);
		}
	}
}
 
Example 8
Source File: GridSwing.java    From evosql with Apache License 2.0 5 votes vote down vote up
public static void autoSizeTableColumns(JTable table) {

        TableModel  model        = table.getModel();
        TableColumn column       = null;
        Component   comp         = null;
        int         headerWidth  = 0;
        int         maxCellWidth = Integer.MIN_VALUE;
        int         cellWidth    = 0;
        TableCellRenderer headerRenderer =
            table.getTableHeader().getDefaultRenderer();

        for (int i = 0; i < table.getColumnCount(); i++) {
            column = table.getColumnModel().getColumn(i);
            comp = headerRenderer.getTableCellRendererComponent(table,
                    column.getHeaderValue(), false, false, 0, 0);
            headerWidth  = comp.getPreferredSize().width + 10;
            maxCellWidth = Integer.MIN_VALUE;

            for (int j = 0; j < Math.min(model.getRowCount(), 30); j++) {
                TableCellRenderer r = table.getCellRenderer(j, i);

                comp = r.getTableCellRendererComponent(table,
                                                       model.getValueAt(j, i),
                                                       false, false, j, i);
                cellWidth = comp.getPreferredSize().width;

                if (cellWidth >= maxCellWidth) {
                    maxCellWidth = cellWidth;
                }
            }

            column.setPreferredWidth(Math.max(headerWidth, maxCellWidth)
                                     + 10);
        }
    }
 
Example 9
Source File: TableUISupport.java    From jeddict with Apache License 2.0 5 votes vote down vote up
@Override
public Component getTableCellRendererComponent(JTable jTable, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    boolean joinTable = false;
    boolean validClass = true;
    boolean existentUpdate = false;
    String problemDisplayName = null;

    if (jTable.getModel() instanceof TableClassNamesModel) {
        TableClassNamesModel model = (TableClassNamesModel) jTable.getModel();
        Table table = model.getTableAt(row);
        joinTable = table.isJoin();
        if (column == 1) {
            existentUpdate = table.getDisabledReason() instanceof Table.ExistingDisabledReason;
            validClass = model.isValidClass(table);
            if (!validClass) {
                problemDisplayName = model.getProblemDisplayName(table);
            }
        }
    }

    Object realValue = null;
    if (joinTable && column == 1) {
        realValue = NbBundle.getMessage(TableUISupport.class, "LBL_JoinTable");
    } else {
        realValue = value;
    }
    JComponent component = (JComponent) super.getTableCellRendererComponent(jTable, realValue, isSelected, hasFocus, row, column);
    component.setEnabled(!joinTable && !existentUpdate);
    component.setToolTipText(joinTable ? NbBundle.getMessage(TableUISupport.class, "LBL_JoinTableDescription") : problemDisplayName);
    component.setForeground((validClass) ? nonErrorForeground : errorForeground);

    return component;
}
 
Example 10
Source File: PixelInfoView.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private boolean selectCurrentRaster(String rasterName, JTable table) {
    final TableModel model = table.getModel();
    for (int i = 0; i < model.getRowCount(); i++) {
        final String s = model.getValueAt(i, NAME_COLUMN).toString();
        if (rasterName.equals(s)) {
            table.changeSelection(i, NAME_COLUMN, false, false);
            return true;
        }
    }
    return false;
}
 
Example 11
Source File: JtableUtils.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 5 votes vote down vote up
/**
 * Loads the Data Array into the tmodel
 *
 * @param table to be populated
 * @param header column header
 * @param rows nullRoww data
 * @return populated tmodel
 */
public static JTable populatetable(JTable table, String[] header, List<String[]> rows) {
    removeRowSelection(table);
    DefaultTableModel tablemodel = (DefaultTableModel) table.getModel();
    tablemodel.setRowCount(0);
    for (String col : header) {
        tablemodel.addColumn(col);
    }
    for (String[] row : rows) {
        tablemodel.addRow(row);
    }
    table.setModel(tablemodel);
    return table;
}
 
Example 12
Source File: AttributeListAttributeCellRenderer.java    From MogwaiERDesignerNG with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

	AttributeTableModel theModel = (AttributeTableModel) table.getModel();
	Attribute<Table> theAttribute = theModel.getRow(row);

	label.setText(theAttribute.getName());
	label.setForeground(Color.black);

	boolean isPrimaryKey = editor.isPrimaryKey(theAttribute);

	if (isPrimaryKey || theAttribute.isForeignKey()) {
		label.setForeground(Color.red);
	}

	keyLabel.setVisible(isPrimaryKey);

	UIInitializer initializer = UIInitializer.getInstance();

	if (isSelected) {
		labelPanel.setBackground(initializer.getConfiguration().getDefaultListSelectionBackground());
		labelPanel.setForeground(initializer.getConfiguration().getDefaultListSelectionForeground());
		keyLabel.setBackground(initializer.getConfiguration().getDefaultListSelectionBackground());
		keyLabel.setForeground(initializer.getConfiguration().getDefaultListSelectionForeground());
	} else {
		labelPanel.setBackground(initializer.getConfiguration().getDefaultListNonSelectionBackground());
		labelPanel.setForeground(initializer.getConfiguration().getDefaultListNonSelectionForeground());
		keyLabel.setBackground(initializer.getConfiguration().getDefaultListNonSelectionBackground());
		keyLabel.setForeground(initializer.getConfiguration().getDefaultListNonSelectionForeground());
	}

	return panel;
}
 
Example 13
Source File: SpectrumChooser.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setReadRasterDataNodeNames(String[] readRasterDataNodeNames) {
    for (JTable bandTable : bandTables) {
        BandTableModel bandTableModel = (BandTableModel) bandTable.getModel();
        for (int j = 0; j < bandTableModel.getRowCount(); j++) {
            String bandName = bandTableModel.getValueAt(j, band_name_index).toString();
            boolean selected = ArrayUtils.isMemberOf(bandName, readRasterDataNodeNames);
            bandTableModel.setValueAt(selected, j, band_selected_index);
        }
    }

}
 
Example 14
Source File: TableRowUtilities.java    From swing_library with MIT License 5 votes vote down vote up
/**
 * Adjusts the column width of the row headers table containg the number
 * column. The font metrics are extracted from the label of the row at the
 * bottom of the viewport and used to determing the appropriate width.
 * 
 * The reason why this method is important, is that when the row number increases by an extra digit
 * the column needs to get wider. It also needs to shrink when scrolling to smaller digit numbers.
 * 
 * @param rowHeadersTable - single column table in the row header
 * @param label - label used to get font metrics
 * @param scrollBarValue - int value for determing point of lowest row
 */
private static void adjustColumnWidth(final JTable rowHeadersTable, final JLabel label, int scrollBarValue) {
	
	label.setFont(rowHeadersTable.getFont());
	label.setOpaque(true);
	label.setHorizontalAlignment(JLabel.CENTER);

	int v = rowHeadersTable.getVisibleRect().height;

	int row = rowHeadersTable.rowAtPoint(new Point(0, v + scrollBarValue));

	Integer modelValue = null;
	if (row != -1) {
		modelValue = (Integer) rowHeadersTable.getModel().getValueAt(row, 0);
	} else {
		RowHeadersTableModel tm = (RowHeadersTableModel) rowHeadersTable.getModel();
		modelValue = new Integer(tm.getMaxIntValue());
	}

	label.setText("" + modelValue);
	FontMetrics fontMetrics = label.getFontMetrics(label.getFont());

	int widthFactor = 0;

	if (fontMetrics != null && label.getText() != null) {
		widthFactor = fontMetrics.stringWidth(label.getText());

		rowHeadersTable.setPreferredScrollableViewportSize(new Dimension(widthFactor + 8, 100)); // height
																									// is
																									// ignored
		rowHeadersTable.repaint();
	}
}
 
Example 15
Source File: TableRenderDemo.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
private void initColumnSizes(JTable table) {
    MyTableModel model = (MyTableModel)table.getModel();
    TableColumn column = null;
    Component comp = null;
    int headerWidth = 0;
    int cellWidth = 0;
    Object[] longValues = model.longValues;
    TableCellRenderer headerRenderer =
        table.getTableHeader().getDefaultRenderer();

    for (int i = 0; i < 5; i++) {
        column = table.getColumnModel().getColumn(i);

        comp = headerRenderer.getTableCellRendererComponent(
                             null, column.getHeaderValue(),
                             false, false, 0, 0);
        headerWidth = comp.getPreferredSize().width;

        comp = table.getDefaultRenderer(model.getColumnClass(i)).
                         getTableCellRendererComponent(
                             table, longValues[i],
                             false, false, 0, i);
        cellWidth = comp.getPreferredSize().width;

        if (DEBUG) {
            System.out.println("Initializing width of column "
                               + i + ". "
                               + "headerWidth = " + headerWidth
                               + "; cellWidth = " + cellWidth);
        }

        column.setPreferredWidth(Math.max(headerWidth, cellWidth));
    }
}
 
Example 16
Source File: VariablesTableAdapterTest.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void variablesProperty() {
    final JTable table = new JTable();
    bindingContext.bind("variables", new VariablesTableAdapter(table));
    assertTrue(table.getModel() instanceof DefaultTableModel);

    final DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
    tableModel.addRow(new String[]{"", ""});
    tableModel.addRow(new String[]{"", ""});

    assertEquals(2, table.getRowCount());

    table.setValueAt("a", 0, 0);
    assertEquals("a", table.getValueAt(0, 0));
    table.setValueAt("A", 0, 1);
    assertEquals("A", table.getValueAt(0, 1));

    table.setValueAt("b", 1, 0);
    assertEquals("b", table.getValueAt(1, 0));
    table.setValueAt("B", 1, 1);
    assertEquals("B", table.getValueAt(1, 1));

    bindingContext.getPropertySet().setValue("variables", new MosaicOp.Variable[]{
            new MosaicOp.Variable("d", "D")
    });

    assertEquals(1, table.getRowCount());
    assertEquals("d", table.getValueAt(0, 0));
    assertEquals("D", table.getValueAt(0, 1));
}
 
Example 17
Source File: GTableCellRenderer.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Provide basic cell rendering -- setting foreground and background colors, font, text,
 * alignment, drop color, and border. Additional data that may be of use to the renderer
 * is passed through the {@link docking.widgets.table.GTableCellRenderingData} object.
 * @param data Context data used in the rendering of a data cell.
 * @return The component used for drawing the table cell.
 */
public Component getTableCellRendererComponent(GTableCellRenderingData data) {

	Object value = data.getValue();
	JTable table = data.getTable();
	int row = data.getRowViewIndex();
	int column = data.getColumnViewIndex();
	boolean isSelected = data.isSelected();
	boolean hasFocus = data.hasFocus();
	Settings settings = data.getColumnSettings();

	if (value instanceof Number) {
		setHorizontalAlignment(SwingConstants.RIGHT);
		setText(formatNumber((Number) value, settings));
	}
	else {
		setText(getText(value));
		setHorizontalAlignment(SwingConstants.LEFT);
	}

	TableModel model = table.getModel();
	configureFont(table, model, column);

	if (isSelected) {
		setForeground(table.getSelectionForeground());
		setBackground(table.getSelectionBackground());
		setOpaque(true);
	}
	else {
		setForegroundColor(table, model, value);

		if (row == dropRow) {
			setBackground(Color.CYAN);
		}
		else {
			setBackground(getOSDependentBackgroundColor(table, row));
		}
	}

	setBorder(hasFocus ? focusBorder : noFocusBorder);
	return this;
}
 
Example 18
Source File: EncapsulateFieldOperator.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void setValueAt(int y, int x, String val){
    JTableOperator table = new JTableOperator(this, 0); 
    JTable t = ((JTable)table.getSource());                
    TableModel model = t.getModel();
    model.setValueAt(val, y, x);
}
 
Example 19
Source File: AbstractRenderer.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 4 votes vote down vote up
protected TestCase getTestCase(JTable table) {
    if (table.getModel() instanceof TestCase) {
        return (TestCase) table.getModel();
    }
    return null;
}
 
Example 20
Source File: LogTableManager.java    From NBANDROID-V2 with Apache License 2.0 4 votes vote down vote up
public LogTableManager(JTable table) {
    this.table = table;
    this.model = (LogTableModel) table.getModel();
    this.columnModel = table.getColumnModel();
    initTable();
}