20

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?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Douglas Grealis
  • 599
  • 2
  • 11
  • 25
  • 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](http://sscce.org/) 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. – camickr Apr 06 '13 at 19:19
  • 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 appreicated – Douglas Grealis Apr 06 '13 at 19:26

4 Answers4

28

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
  • 1,847
  • 3
  • 20
  • 38
  • Thanks for the reply. As you can see in my code, I have already invoked it, but doesn't seem to do anything. – Douglas Grealis Apr 06 '13 at 17:17
  • Try using model.setRowHeight()? since your invoking the getModel() method from the table variable – madcrazydrumma Apr 06 '13 at 17:30
  • I have tried that there, and the compiler outputs : cant find symbol setRowHeight at location DefaultTableModel ? Thanks again – Douglas Grealis Apr 06 '13 at 17:36
  • Try not using DefaultTableModel, I dont see why you need to use it – madcrazydrumma Apr 06 '13 at 17:39
  • 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); – jmclellan Apr 06 '13 at 17:43
  • 3
    -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. – camickr Apr 06 '13 at 19:16
  • Thanks for all your help. I re arranged my code and got the height sizes working. Much apprecicated – Douglas Grealis Apr 06 '13 at 19:22
23

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();
            }
        });
    }
}
tenorsax
  • 21,123
  • 9
  • 60
  • 107
4

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

set rowHeight value

msl
  • 109
  • 1
  • 4
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
            }
        });
    }
});
Amarnath
  • 8,736
  • 10
  • 54
  • 81
madcrazydrumma
  • 1,847
  • 3
  • 20
  • 38