How to insert column title in a jTable?

0

I could not display the table title, could they help? Note: I entered "JScrollPane scroll = new JScrollPane(tabela); "and" contentPane.add(scroll); "or" getContentPane.add(scroll) "as I saw in questions / answers from that 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);        

    }

}
    
asked by anonymous 18.03.2018 / 14:42

1 answer

1

First is a suggestion about using absolute layout:

  

Avoid using absolute layout unless you are in dire need and know the consequences , since absolute layout makes it difficult to maintain the interface and makes your application look different depending on the monitor and resolution being executed.

     

There are several layouts managers for you to you do not have to worry about manual positioning or organization of components. Not to mention that the use of layouts makes your code easier to maintain than inserting a lot of setbounds , and in case you need to change the position of any component, in the absolute layout, you will have to reposition them all manually.

Just by removing the line below the code would theoretically work:

j.setLayout(null);

But you also add the direct table in the JFrame , and further down in the code, add a JScrollPane . Remove the direct addition in JFrame and the size definition using setBounds , because as you have already searched here, tables need to be added in "rolling" components so they are displayed correctly. If you want to control the size of the table on the screen, see the links to the Layout Managers warning above or see examples right here on the site through this link .

Another thing that needs to be attacked is you do not start the interface inside the correct thread. Interfaces made with the swing API are dispatched to a specific thread , and doing operations outside this loop to control the screen may thread competition issues occur because the main method runs on a different thread.

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

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:

    
18.03.2018 / 14:44