Setting the height of a row in a JTable in java
Asked Answered
F

4

22

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 no row[s] have been increased. When I use the getRowHeight(int) method of the row I set the height to, it does print out the size I increased the row to, so I'm not sure what is wrong. The code below is a rough illustration how I am trying to solve it.

My class extends JFrame.

String[] columnNames = {"Column 1", "Column 2", "Column 1 3"};

JTable table = new JTable(new DefaultTableModel(columnNames, people.size()));

DefaultTableModel model = (DefaultTableModel) table.getModel();

int count =1;
for(Person p: people)
{
    model.insertRow(count,(new Object[]{count, p.getName(), p.getAge()+"", 
    p.getNationality}));
    count++;
}

table.setRowHeight(1, 15);//Try set height to 15 (I've tried higher)

Can anyone tell me where I am going wrong? I am trying to increase the height of row 1 to 15 pixels?

Financier answered 6/4, 2013 at 17:10 Comment(2)
I re arranged my code as that example, and I even increased to height to 100, and still visable increase. This is why you should post your SSCCE that demonstrates the problem. Just because you say you are doing something doesn't mean you actually are doing it. Maybe you defined two tables by mistake and you are changing the property of a table that isn't actually displayed.Ternary
Sorry my bad, I was using the setRowHeight(int int) method in the loop, but changed it to setRowHeight(int) and it works perfectly now. Thanks for the replies, much appreicatedFinancier
L
24

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();
            }
        });
    }
}
Legate answered 6/4, 2013 at 18:19 Comment(1)
Thanks very much for the reply. As I re arranged the code as your example, it works now. Much appreciatedFinancier
S
30

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

Shrimp answered 6/4, 2013 at 17:14 Comment(7)
Thanks for the reply. As you can see in my code, I have already invoked it, but doesn't seem to do anything.Financier
Try using model.setRowHeight()? since your invoking the getModel() method from the table variableShrimp
I have tried that there, and the compiler outputs : cant find symbol setRowHeight at location DefaultTableModel ? Thanks againFinancier
Try not using DefaultTableModel, I dont see why you need to use itShrimp
How big do you want it to be? On my machine 15 pixels seems to be slightly smaller than the default size. Trying something much larger like 60 proves your code works just as it should. Maybe you want it 15 pixels larger than the default? table.setRowHeight(1, table.getRowHeight(1) + 15);Kalong
-1 for the wild guess about setting the row height in the model. A TableModel doesn't have a row height. A model stores the data and notifies the view when the data changes. This has nothing to do whether you use a the DefaultTableModel or any other TableModel.Ternary
Thanks for all your help. I re arranged my code and got the height sizes working. Much apprecicatedFinancier
L
24

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();
            }
        });
    }
}
Legate answered 6/4, 2013 at 18:19 Comment(1)
Thanks very much for the reply. As I re arranged the code as your example, it works now. Much appreciatedFinancier
P
3

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

set rowHeight value

Proviso answered 12/3, 2018 at 17:52 Comment(0)
S
2

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
            }
        });
    }
});
Shrimp answered 6/4, 2013 at 17:40 Comment(2)
Thanks for the reply. Would I Override those methods outside of the Constructor? Along with the JTableFinancier
That's what I did @DouglasGrealis. My JTable class became a TableModelListener and my table model added it as a TableModelListener.Rosary

© 2022 - 2024 — McMap. All rights reserved.