How to create Tablecellrenderer for strings?

Asked

Viewed 150 times

2

I wanted to know how to make a Tablecellrenderer for strings.

on this topic here: How to use a selection setBackground in custom Renderer?

I saw that the guy had to "values". I wanted to know if to do for names and etc?

For example:

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class Tabela extends JFrame {

    public static void main(String args[]) {
        new Tabela();
    }

    public Tabela() {

        String[][] dados = new String[][]{{"Brasil", "São Paulo"},
        {"Estados Unidos", "New York"}
        };
        String[] colunas = new String[]{"Pais", "Estado"};
        JTable tabela = new JTable(dados, colunas);
        JScrollPane jsp = new JScrollPane(tabela);
        jsp.setPreferredSize(new Dimension(200, 300));
        getContentPane().add("Center", jsp);
        setSize(500, 300);
        setVisible(true);        
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

I have a table (here only a very simple example), and I wish I could have a render, so where I have tables later, I can format them. I wanted the names there to be centralized (content of the lines)

  • It’s still unclear what you want. Centering columns with string doesn’t even need to create a Renderer. That’s all you want: to centralize column information?

  • Center the contents of rows. Any row q is inserted is centered.

1 answer

2


There are two ways that stand out:

- Centering specific columns

It is possible to set the centralized rendering only for certain columns by setting a default Renderer for this column in isolation:

DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment(JLabel.CENTER);
//onde está zero pode ser alterado para a coluna desejada
tabela.getColumnModel().getColumn(0).setCellRenderer(centerRenderer);

Repeat the last row for all columns of the table,can be using a loop or manually if you have few columns.

Applying the above code in your example, only the column País will be centralized:

inserir a descrição da imagem aqui

- Applying to the whole table

This method would change all columns, but makes it difficult if in the future you want to change the rendering only to certain columns.

To this end, it will be necessary to change the method getColumnClass of TableModel table. In the example, I did using the DefaultTableModel, passing as parameters the row and column array:

DefaultTableModel model = new DefaultTableModel(dados,colunas){
    
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }
};

Then apply your table this way:

JTable tabela = new JTable(model);
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment(JLabel.CENTER);
tabela.setDefaultRenderer(String.class, centerRenderer);

Applied in your example, the result will be:

inserir a descrição da imagem aqui

The problem with this method is that all columns will be treated as String, making it a better option create your own Tablemodel, for the code and its table to be easier to maintain and readable. You choose which method to use.

  • This first method, won’t it just center the columns? I wanted to center the content of the rows. Like, in the Pais column, leave Brazil centralized.

  • 1

    @Javinha yes, as I explained in the answer, the one method will center only on the columns you set in the highlighted row. Gives more control than center. Method 2 centralizes everything, because in Defaulttablemodel I set all columns to be Strings.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.