Setting the height of a row in a JTable in java

我们两清 提交于 2019-12-05 00:40:53

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:

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();
            }
        });
    }
}

You can use:

table.setRowHeight(int par1);

or if you wanted to set the row height for a specific row, use:

table.setRowHeight(int par1, int par2);

madcrazydrumma

You can also add a tableModelListener?

model.addTableModelListener(new TableModelListener() {
    @Override public void tableChanged(final TableModelEvent e) {
        EventQueue.invokeLater(new Runnable() {
            @Override public void run() {
                table.setRowHeight(e.getFirstRow(), 15); //replace 15 with your own height
            }
        });
    }
});

Right click on the JTable in JFrame and click Properties. Scroll down and set the rowHeight value.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!