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

The following examples show how to use javax.swing.JTable#getWidth() . 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: AnnotationPanel.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
        int column) {
    component.setText(((Annotation) value).getText());
    Dimension d = component.getSize();
    d.width = table.getWidth();
    component.setSize(d);
    table.setRowHeight(row, component.getPreferredSize().height);
    if (isSelected) {
        component.setBackground(getSelectionBackground());
        component.setForeground(getSelectionForeground());
    } else {
        if (row % 2 == 0) {
            component.setBackground(new Color(0xFE, 0xF4, 0x9C));
        } else {
            component.setBackground(new Color(0xA6, 0xE9, 0xF4));
        }
        component.setForeground(getForeground());
    }
    return component;
}
 
Example 2
Source File: AnnotationPanel.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
    annotation = (Annotation) value;
    component.setText(annotation.getText());
    Dimension d = component.getSize();
    d.width = table.getWidth();
    component.setSize(d);
    table.setRowHeight(row, component.getPreferredSize().height);
    if (row % 2 == 0) {
        component.setBackground(new Color(0xFE, 0xF4, 0x9C));
    } else {
        component.setBackground(new Color(0xA6, 0xE9, 0xF4));
    }
    component.setForeground(getForeground());
    this.row = row;
    this.table = table;
    return component;
}
 
Example 3
Source File: ETableColumn.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Forces the table to resize given column.
 */
private void resize(int newWidth, JTable table) {
    int oldWidth = getWidth();
    JTableHeader header = table.getTableHeader();
    if (header == null) {
        return;
    }
    header.setResizingColumn(this);
    final int oldMin = getMinWidth();
    final int oldMax = getMaxWidth();
    setMinWidth(newWidth);
    setMaxWidth(newWidth);
    setWidth(newWidth);
    // The trick is to restore the original values
    // after the table has be layouted. During layout this column
    // has fixed width (by setting min==max==preffered)
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            setMinWidth(oldMin);
            setMaxWidth(oldMax);
        }
    });
    Container container;
    if ((header.getParent() == null) ||
            ((container = header.getParent().getParent()) == null) ||
            !(container instanceof JScrollPane)) {
        header.setResizingColumn(null);
        return;
    }
    
    if (!container.getComponentOrientation().isLeftToRight() &&
            ! header.getComponentOrientation().isLeftToRight()) {
        if (table != null) {
            JViewport viewport = ((JScrollPane)container).getViewport();
            int viewportWidth = viewport.getWidth();
            int diff = newWidth - oldWidth;
            int newHeaderWidth = table.getWidth() + diff;
            
            /* Resize a table */
            Dimension tableSize = table.getSize();
            tableSize.width += diff;
            table.setSize(tableSize);
            
            /* If this table is in AUTO_RESIZE_OFF mode and
             * has a horizontal scrollbar, we need to update
             * a view's position.
             */
            if ((newHeaderWidth >= viewportWidth) &&
                    (table.getAutoResizeMode() == JTable.AUTO_RESIZE_OFF)) {
                Point p = viewport.getViewPosition();
                p.x = Math.max(0, Math.min(newHeaderWidth - viewportWidth, p.x + diff));
                viewport.setViewPosition(p);
            }
        }
    }
    header.setResizingColumn(null);
}
 
Example 4
Source File: SeaGlassTableUI.java    From seaglass with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link Border} that paints any empty space to the right of the
 * last column header in the given {@link JTable}'s {@link JTableHeader}.
 *
 * @param  table DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 */
private static Border createTableHeaderEmptyColumnPainter(final JTable table) {
    return new AbstractBorder() {
        @Override
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            // if this JTableHeader is parented in a JViewport, then paint
            // the table header background to the right of the last column
            // if neccessary.
            Container viewport = table.getParent();

            if ((viewport instanceof JViewport) && table.getWidth() < viewport.getWidth()) {
                int startX           = table.getWidth();
                int emptyColumnWidth = viewport.getWidth() - table.getWidth();

                TableCellRenderer renderer  = table.getTableHeader().getDefaultRenderer();
                // Rossi: Fix for indexoutofbounds exception: A try catch might be good too?
                Component         component = renderer.getTableCellRendererComponent(table, "", false, false, 0, table.getColumnCount()-1);

                component.setBounds(0, 0, emptyColumnWidth, table.getTableHeader().getHeight());

                ((JComponent) component).setOpaque(true);
                CELL_RENDER_PANE.paintComponent(g, component, null, startX, 0, emptyColumnWidth + 1,
                                                table.getTableHeader().getHeight(), true);
            }
        }
    };
}