Setting the height of a row in a JTable in java

前端 未结 4 1697
小蘑菇
小蘑菇 2021-02-19 13:04

I have been searching for a solution to be able to increase the height of a row in a JTable. I have been using the setRowHeight(int int) method which compiles and runs OK, but n

4条回答
  •  生来不讨喜
    2021-02-19 14:03

    Not sure what is the intention of leaving the first row at index 0 empty. Rows in JTable run from index 0. It is best if you could post a complete example (ie SSCCE) that demonstrates the issues. Compare to this simple example that works OK:

    enter image description here

    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    
    public class DemoTable {
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("DemoTable");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            DefaultTableModel model = new DefaultTableModel();
            model.setColumnIdentifiers(new Object[] {
                    "Column 1", "Column 2", "Column 3" });
    
            JTable table = new JTable(model);
            for (int count = 0; count < 3; count++){
                model.insertRow(count, new Object[] { count, "name", "age"});
            }
            table.setRowHeight(1, 30);
    
            frame.add(new JScrollPane(table));
            frame.setLocationByPlatform(true);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String args[]) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    

提交回复
热议问题