How to insert column titles into a jTable?

Asked

Viewed 862 times

0

I couldn’t display the title of the chart, could you help? Obs.: I inserted it "JScrollPane scroll = new JScrollPane(tabela);" and "contentPane.add(scroll);" or "getContentPane.add(scroll)" as I saw in questions/answers of this site, but I was not successful.

Code:

package tabelas;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Tabelas {

    public static void main(String[] args) {
        // TODO code application logic here
        JFrame j = new JFrame("Bora mecao");
        j.setBounds(0,0,400,400);

        JTable tabela = new JTable();
        tabela.setBounds(10,10,200,200);

        j.add(tabela);

        String[] colunas = {"Marca", "Local"};

        DefaultTableModel modelo = (new DefaultTableModel(){
            @Override
            public boolean isCellEditable(int row, int column){
                return false;
            }
        });

        modelo.setColumnIdentifiers(colunas);
        modelo.setRowCount(0);

        Object[] objetos = new Object[2];
        objetos[0] = "LG";
        objetos[1] = "Chalé";
        modelo.addRow(objetos);

        tabela.setModel(modelo);

        JScrollPane scroll = new JScrollPane(tabela);
        contentPane.add(scroll);

        j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        j.setLayout(null);
        j.setVisible(true);        

    }

}

1 answer

1


First follows a suggestion regarding using absolute layout:

Avoid using absolute layout, unless it is of extreme necessity and you know the consequences of it, because absolute layout makes it difficult to maintain the interface and makes your application look different depending on the monitor and resolution that is running.

There are several layouts so you don’t have to worry about positioning or manually organizing components. Not to mention that using layouts makes your code easier to maintain than inserting a lot of setbounds, and if you need to change the position of any component, in the absolute layout, you will have to reposition all manually.

Just removing the line below the code would theoretically work:

j.setLayout(null);

But you also add the direct table on JFrame, and further down in the code, add in a JScrollPane. Remove direct addition to JFrame and size setting using the setBounds, As Voce even already searched here, tables need to be added in "scrollable" component in order to be displayed correctly. If you want to control the size of the table on the screen, see the warning links above for Layout Managers or see examples right here on the site through of this link.

Another thing that needs to be attempted is that you don’t start the interface within the correct thread. Interfaces made with the swing API are dispatched to a specific thread, and perform operations outside this loop to control the screen may result in threads competing problems, since the main method runs on a different thread.

And to add lines using DefaultTableModel you need to pass an array of a dimension that will represent the columns and another array of arrays, where the internal arrays represent the rows to be populated. I recommend reading this topic: How do I popular a Jtable?

With the fixes, the code looks like this:

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class Tabelas {

    public Tabelas() {

        JFrame j = new JFrame("Bora mecao");
        j.setSize(400,400);

        JTable tabela = new JTable();      

        String[] colunas = {"Marca", "Local"};
        String[][] objetos = {{"LG", "chalé"}};

        DefaultTableModel modelo = (new DefaultTableModel(objetos, colunas){
            @Override
            public boolean isCellEditable(int row, int column){
                return false;
            }
        });

        tabela.setModel(modelo);

        JScrollPane scroll = new JScrollPane(tabela);
        j.getContentPane().add(scroll);

        j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        j.setVisible(true);  
    }

    public static void main(String[] args) {
      SwingUtilities.invokeLater(() -> new Tabelas());;
    }
}

The result:

inserir a descrição da imagem aqui

  • Thanks for all your help!

Browser other questions tagged

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