仅更改1个JTable列渲染器的默认值

时间:2015-05-29 17:18:42

标签: java jtable

大家好我有以下情况,我想将JTable中的所有列设置为特定的渲染器(居中),我希望最右边的列的背景颜色为黄色。我尝试过以下代码:

public class LView extends JInternalFrame {

private static final long serialVersionUID = -4248667075312220111L;
private LModel model;
private JPanel mainPanel;
private JButton btnRefresh;

private JTable lTable;
private DefaultTableModel lTableModel;

private String [] columnNames = {"col1", "col2", "col3", "col4", "col5"};


public LView() {
    super("L Title", true, true, true, true);
    setSize(650,300);

    mainPanel = new JPanel(new FlowLayout());
    JPanel topPanel = new JPanel();

    btnRefresh = new JButton("Refresh");
    btnRefresh.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            try { model.refresh(); }
            catch (Exception e) { model.logException(e); }
        }
    });

    topPanel.add(btnRefresh);

    lTableModel = new DefaultTableModel() {
        private static final long serialVersionUID = 2130745775009581358L;

        @Override
        public boolean isCellEditable(int row, int col) { 
            return false; 
        }
    };

    for (String col : columnNames)
        lTableModel.addColumn(col);

    lTable = new JTable();

    DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
    centerRenderer.setHorizontalAlignment( SwingConstants.CENTER );
    lTable.setDefaultRenderer(Object.class, centerRenderer);

    DefaultTableCellRenderer estColumnRenderer = new DefaultTableCellRenderer();
    estColumnRenderer.setBackground(java.awt.Color.YELLOW);
    estColumnRenderer.setFont(new Font(null, Font.BOLD, 12));
    estColumnRenderer.setHorizontalAlignment( SwingConstants.CENTER );

    lTable.setModel(lTableModel);
    lTable.getColumnModel().getColumn(columnNames.length-1).setCellRenderer(estColumnRenderer);


    lTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    lTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    lTable.getColumn(columnNames[0]).setPreferredWidth(225);

    JScrollPane scrollPane = new JScrollPane(lTable);

    JPanel tablePanel = new JPanel();
    tablePanel.add(scrollPane);

    mainPanel.add(topPanel);
    mainPanel.add(tablePanel);
    add(mainPanel);
}

public LModel getModel() {
    return model;
}

public void setModel(LModel model) {
    this.model = model;

}

public void addData(List<Object []> dataList) throws Exception {

    lTableModel.getDataVector().removeAllElements();
    lTableModel.fireTableDataChanged();

    for (Object [] data : dataList) {

        lmeTableModel.addRow(new Object[] {data[0], 
                                        data[1],
                                        data[2],
                                        data[3],
                                        data[4]});
    }

    lTable.setPreferredScrollableViewportSize(lTable.getPreferredSize());
    lTable.setFillsViewportHeight(true);
    lTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

    lTableModel.fireTableStructureChanged();
}   

}

它将所有列设置为顶部“centerRenderer”,但最后一列不会以任何方式更改。我错过了什么吗?谢谢 -

编辑:完整代码

0 个答案:

没有答案