开发者

JTable data is invisible unless cell is selected

I'm creating a JTable with data contained in 2 Vector, rowData and columnNames. I'm using a renderer to give the colour I want to the JTable. But data is invisible unless I click a cell: then only that cell data is visible.

My code:

   // Creating table
    final JTable tablaCurvas = new JTable();
    // Applng colours and column sizes with renderer
    TableCellRenderer tableRender = new TableRenderer();
    tablaCurvas.setDefaultRenderer(Object.class, tableRender);
    // Create an easy model to add data to table
    tablaCurvas.setModel(new DefaultTableModel(rowData, columnNames){
        private static final long开发者_StackOverflow serialVersionUID = 1L;
    @Override
       public boolean isCellEditable(int row, int column) {
           //Only the second column
           return column == 1;
       }

    });
    // Necessary clicks to edit cell
    ((DefaultCellEditor) tablaCurvas.getDefaultEditor(Object.class)).setClickCountToStart(1);
    // Add table into a scrollPane
    JScrollPane scrollPane = new JScrollPane(tablaCurvas);
    // Fill the pane
    tablaCurvas.setFillsViewportHeight(true);
    // Preferred size
    tablaCurvas.setPreferredScrollableViewportSize(new Dimension(150,100));

And the renderer:

class TableRenderer extends DefaultTableCellRenderer
{
    private static final long serialVersionUID = 1L;
    public Component getTableCellRendererComponent(
                    JTable table,
                    Object value,
                    boolean isSelected,
                    boolean hasFocus,
                    int row,
                    int column)
    {       
        setEnabled(table == null || table.isEnabled());
        if (column == 0)
            setHorizontalAlignment(LEFT);
        else // (column == 1)
            setHorizontalAlignment(RIGHT);
        for (int i=0; i<2; i++) {
            TableColumn columna = table.getColumnModel().getColumn(i);
            if (i==0){
                columna.setPreferredWidth(150);
            }
            if (i==1) columna.setPreferredWidth(50);
        }
        setBackground(table.getBackground());
        setForeground(table.getForeground());
        if (row%2==1) setBackground(java.awt.Color.white);
        else setBackground(new java.awt.Color(211, 217, 255)); 

        return this;
    }

Any way, appart from this, I'm finding much more difficult to learn how to use JTables than other Objects, because Oracle Tutorial is not very well explained in that chapter. Any book-chapter or online tutorial for JTables recommended?


You have to set the text for the DefaultTableCellRenderer component.

public Component getTableCellRendererComponent(JTable table,
                    Object value, boolean isSelected, boolean hasFocus, int row,
                    int column) {

    //.... your code

    setText(value != null ? value.toString() : ""); // suppress null values
    return this;
}

This screenshot was taken with some example data:

JTable data is invisible unless cell is selected

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜