Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the height of a row in a JTable in java

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?

like image 659
Douglas Grealis Avatar asked Apr 06 '13 17:04

Douglas Grealis


People also ask

How do you set the size of a table in Java?

By default the width of a JTable is fixed, we can also change the width of each column by using table. getColumnModel(). getColumn(). setPreferredWidth() method of JTable class.

How do I sort a row in JTable?

We can sort a JTable in a particular column by using the method setAutoCreateRowSorter() and set to true of JTable class.

What is JTable TableModel?

The TableModel interface specifies the methods the JTable will use to interrogate a tabular data model. The JTable can be set up to display any data model which implements the TableModel interface with a couple of lines of code: TableModel myData = new MyTableModel(); JTable table = new JTable(myData);


Video Answer


2 Answers

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

like image 63
madcrazydrumma Avatar answered Sep 22 '22 02:09

madcrazydrumma


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();
            }
        });
    }
}
like image 40
tenorsax Avatar answered Sep 21 '22 02:09

tenorsax