Sebi
Sebi

Reputation: 15

Center text in JTable

I've a problem with my JTable. I tried to center the text like this:

DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment( JLabel.CENTER );
table.setDefaultRenderer(String.class, centerRenderer);
table.setDefaultRenderer(Integer.class, centerRenderer);

at first time it worket, but then I made my program bigger and then it doesn't work anymore. I've the JTable in a JPane that is in a JTabbedPane. I've in the JTabbedPane some other JPanes also with other JTable's.

My Question is, if there is any way to get the text in the cell's centered.

Upvotes: 1

Views: 8420

Answers (1)

Blastoise Opressor
Blastoise Opressor

Reputation: 476

You must explicitly specify the column type in TableModel. If you are using DefaultTableModel, the default type returned by thegetColumnClass method is type Object, and because the DefaultTableCellRenderer class uses a JLabel to render the cells, the default alignment is left for this type.

You can set the type of each column or let java identify the type by changing the getColumnClass method:

@Override
public Class<?> getColumnClass(int column) {
    System.out.println(getValueAt(0, column).getClass());
    return getValueAt(0, column).getClass();
}

But if you want to define for more than one column with different types, the setDefaultRenderer method will not work because it defines only for the type passed by parameter and applies to the whole table. The ideal is to identify the column and define the alignment separately for it:

//change 0 for your column index
table.getColumnModel().getColumn(0).setCellRenderer(centerRenderer);

See this applied on a example:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.time.LocalDate;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class JTableCreatingDemo extends JFrame {

    public void createAndShowGUI() {

        Object columnNames[] = { "String-Column", "Number-Column", "Date-Column", "Boolean-Column" };

        Object rowData[][] = { { "some text", 89, LocalDate.now(), new Boolean(true) },
                { "other text", 546, LocalDate.now(), new Boolean(false) } };

        JTable table = new JTable(rowData, columnNames) {

            @Override
            public Class<?> getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };

        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(JLabel.CENTER);

        table.getColumnModel().getColumn(0).setCellRenderer(centerRenderer);
        table.getColumnModel().getColumn(1).setCellRenderer(centerRenderer);

        JScrollPane scrollPane = new JScrollPane(table);
        this.add(scrollPane, BorderLayout.CENTER);
        this.setSize(350, 150);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String args[]) throws IllegalAccessException {

        EventQueue.invokeLater(() -> new JTableCreatingDemo().createAndShowGUI());
    }
}

See works:

enter image description here

Upvotes: 4

Related Questions