调整标题表中的列值以显示名称

时间:2014-11-17 10:30:19

标签: java swing jtable

我创建了下表,它为我设置了一个标题列。它看起来基本上是这样的:

enter image description here

正如您所看到的,rowNamesString[] rowNames = {"Production Valuation", "Sales Production", "R&D Department", "Cost Valuation"}; 的值未完整显示。

我试着设置:

    headerTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    headerTable.getColumnModel().getColumn(0).setPreferredWidth(360); 

然而,没有任何改变。这是一个示例程序:

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Font;
    import java.util.Calendar;

    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingConstants;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    import javax.swing.event.RowSorterEvent;
    import javax.swing.event.RowSorterListener;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableColumnModel;
    import javax.swing.table.TableModel;
    import javax.swing.table.TableRowSorter;

    public class JTableRowHeader {

        private JFrame frame = new JFrame("JTable");
        private JScrollPane scrollPane;
        private JTable tableAnalytics;
        private DefaultTableModel model;
        private DefaultTableModel model1;
        private TableRowSorter<TableModel> sorter;
        private JTable headerTable;

        public JTableRowHeader() {

            analyticsTable();
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.add(scrollPane);

            frame.pack();
            frame.setLocation(150, 150);
            frame.setVisible(true);
        }

        /**
         * Is the analytics table for each values.
         */
        public void analyticsTable() {
            String[] columnNames = {"2012", "2013", "2014"};
            String[] rowNames = {"Production Valuation", "Sales Production", "R&D Department", "Cost Valuation"};

            model1 = new DefaultTableModel(rowNames.length, columnNames.length) {
                /**
                 * UUID
                 */
                private static final long serialVersionUID = 8538648271391086030L;

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

            tableAnalytics = new JTable(model1);
            //set Values
            for (int i = 0; i < model1.getRowCount(); i++) {
                for (int ii = 0; ii < model1.getColumnCount(); ii++) {
                    model1.setValueAt(i, i, ii);
                }
            }

            TableColumnModel tcm = tableAnalytics.getColumnModel();  
            for (int k = 0; k < tcm.getColumnCount(); k++) {
                tcm.getColumn(k).setHeaderValue(columnNames[k]);
            }

            sorter = new TableRowSorter<TableModel>(tableAnalytics.getModel());
            tableAnalytics.setRowSorter(sorter);
            model = new DefaultTableModel() {
                private static final long serialVersionUID = 12312432141L;

                @Override
                public int getColumnCount() {
                    return 1;
                }

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

                @Override
                public int getRowCount() {
                    return tableAnalytics.getRowCount();
                }

                @Override
                public Class<?> getColumnClass(int colNum) {
                    switch (colNum) {
                    case 0:
                        return String.class;
                    default:
                        return super.getColumnClass(colNum);
                    }
                }
            };
            headerTable = new JTable(model);
            for (int o = 0; o < rowNames.length; o++) {
                headerTable.setValueAt(rowNames[o], o, 0);
            }

            headerTable.setShowGrid(false);
            headerTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            headerTable.getColumnModel().getColumn(0).setPreferredWidth(360); 
            headerTable.setPreferredScrollableViewportSize(new Dimension(50, 0));
            headerTable.getColumnModel().getColumn(0).setPreferredWidth(50);
            headerTable.getColumnModel().getColumn(0).setCellRenderer(new TableCellRenderer() {
                @Override
                public Component getTableCellRendererComponent(JTable x, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

                    boolean selected = tableAnalytics.getSelectionModel().isSelectedIndex(row);
                    Component component = tableAnalytics.getTableHeader().getDefaultRenderer().getTableCellRendererComponent(tableAnalytics, value, false, false, -1, -2);
                    ((JLabel) component).setHorizontalAlignment(SwingConstants.CENTER);
                    if (selected) {
                        component.setFont(component.getFont().deriveFont(Font.BOLD));
                        component.setForeground(Color.red);
                    } else {
                        component.setFont(component.getFont().deriveFont(Font.PLAIN));
                    }
                    return component;
                }
            });
            tableAnalytics.getRowSorter().addRowSorterListener(new RowSorterListener() {
                @Override
                public void sorterChanged(RowSorterEvent e) {
                    model.fireTableDataChanged();
                }
            });
            tableAnalytics.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    model.fireTableRowsUpdated(0, model.getRowCount() - 1);
                }
            });
            scrollPane = new JScrollPane(tableAnalytics);
            scrollPane.setRowHeaderView(headerTable);
            tableAnalytics.setPreferredScrollableViewportSize(tableAnalytics.getPreferredSize());
        }

        public static void main(String[] args) {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InstantiationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (UnsupportedLookAndFeelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    @SuppressWarnings("unused")
                    JTableRowHeader TestTableRowHeader = new JTableRowHeader();
                }
            });
        }
    }

任何建议我做错了什么?

感谢您的回答!

1 个答案:

答案 0 :(得分:2)

设置scrollpane rowheader维度,如

scrollPane.getRowHeader().setPreferredSize(new Dimension(120, 50));

之后添加上述声明
scrollPane.setRowHeaderView(headerTable);

并评论该行

//headerTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);