jTable does not name the columns

0

I have a jTable for a future pool that I want to make. The problem is that when I create the jTable, I open where it says model, I put the number of columns and the number of rows and I also put the name but in the jFrame only the jTable leaves completely empty, pls help.

PS: I'm working with Eclipse Luna

PS2: I do not know why the Hello from the beginning is deleted lmao

PS3: I've edited this about 20 times

package com.ucab.ve;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTable;
import java.awt.BorderLayout;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JLabel;
import java.awt.Font;

public class Quiniela {
private String[] columnNames = {"Fecha", "Equipo 1", "Equipo 2", "Goles 
Equipo 1", "Goles Equipo 2"};
private JFrame frame;
private JTable table;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    String[] equipos={};
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Quiniela window = new Quiniela(equipos);
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Quiniela(String[] equipos) {
    initialize(equipos);
}

/**
 * Initialize the contents of the frame.
 */
private void initialize(String[] equipos) {
    frame = new JFrame();
    frame.setBounds(100, 100, 658, 612);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    table = new JTable();
    table.setBounds(0, 33, 642, 573);
    table.setModel(new DefaultTableModel(
        new Object[][] {
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
        },
        new String[] {
            "Fecha", "Equipo 1", "Equipo 2", "Goles Equipo 1", "Goles Equipo 2"
        }
    ) {
        boolean[] columnEditables = new boolean[] {
            false, false, false, true, true
        };
        public boolean isCellEditable(int row, int column) {
            return columnEditables[column];
        }
    });
    table.getColumnModel().getColumn(0).setResizable(false);
    table.getColumnModel().getColumn(0).setPreferredWidth(83);
    table.getColumnModel().getColumn(1).setPreferredWidth(88);
    table.getColumnModel().getColumn(2).setPreferredWidth(89);
    table.getColumnModel().getColumn(3).setPreferredWidth(93);
    table.getColumnModel().getColumn(4).setPreferredWidth(107);
    frame.getContentPane().setLayout(null);
    table.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
    frame.getContentPane().add(table);

    JLabel lblFecha = new JLabel("Fecha");
    lblFecha.setFont(new Font("Lato", Font.PLAIN, 13));
    lblFecha.setBounds(36, 11, 46, 14);
    frame.getContentPane().add(lblFecha);

    JLabel lblEquipo1 = new JLabel("Equipo 1");
    lblEquipo1.setFont(new Font("Lato", Font.PLAIN, 13));
    lblEquipo1.setBounds(152, 11, 50, 16);
    frame.getContentPane().add(lblEquipo1);

    JLabel lblEquipo2 = new JLabel("Equipo 2");
    lblEquipo2.setFont(new Font("Lato", Font.PLAIN, 13));
    lblEquipo2.setBounds(274, 11, 50, 16);
    frame.getContentPane().add(lblEquipo2);

    JLabel lblGolesEquipo1 = new JLabel("Goles Equipo 1");
    lblGolesEquipo1.setFont(new Font("Lato", Font.PLAIN, 13));
    lblGolesEquipo1.setBounds(389, 11, 86, 16);
    frame.getContentPane().add(lblGolesEquipo1);

    JLabel lblGolesEquipo2 = new JLabel("Goles Equipo 2");
    lblGolesEquipo2.setFont(new Font("Lato", Font.PLAIN, 13));
    lblGolesEquipo2.setBounds(525, 11, 86, 16);
    frame.getContentPane().add(lblGolesEquipo2);
}

public String regresar(String info){
    return info;
}

}
    
asked by Mary 06.01.2018 в 09:22
source

2 answers

0

Really the columns if they are, but since they do not have an edge, they do not see it. If you want to add a border to the cells you should overwrite the method preparerender like this:

  table = new JTable(){   
                @Override
                public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {   
                    Component result = super.prepareRenderer(renderer, row, column);   
                    if (result instanceof JComponent) {   
                        ((JComponent) result).setBorder(new LineBorder(Color.gray));   
                    }   
                    return result;   
                }   
            }; 
    
answered by 06.01.2018 в 17:37
0

I can provide you with a code format that you can use to model your jTable, I hope and it will be useful to you.

   public static DefaultTableModel modelo3; -- definimos el modelo

private void CrearModelo1() --creamos el metodo void y realizamos un try-catch
    try {
        modelo3 = (new DefaultTableModel(
                null, new String[]{   -- aqui declaras las columnas
                    "Id Empleado",
                    "RFC", "Nombre", "Cargo"}) {
            Class[] types = new Class[]{
                java.lang.String.class,  --quitas o repites los java lang dependiendo de la cantidad de columnas que llegues a necesitar
                java.lang.String.class,
                java.lang.String.class,
                java.lang.String.class
            };
            boolean[] canEdit = new boolean[]{ --posibilidad de editar tu determinas si deseas que las celdas puedas ser editables o no; la cantidad de false o true es dependiendo del número de columnas que hayas realizado
                false, false, false, false
            };

            @Override
            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            @Override
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return canEdit[colIndex];
            }
        });
        jTable1.setModel(modelo3);
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e.toString() + "error2");
    }
}
    
answered by 11.01.2018 в 05:02