Fill Table with my data received in Eclipse WindowsBuilder

0

For the first time I am trying to learn to work with tables but, reading manuals and videos, I can not understand much since almost all are in English.

My program is this, with the table added:

In the code, I only have this.

I declare:

private final JTable table;

Code of the table itself:

table =  new JTable();
        table.setBounds(10, 297, 445, 143);
        contentPane.add(table);

According to what I managed to understand, you have to create two arrangements:

  • arreglo1 [] = {"#", "origen", "destino", "duracion", "precio"};
  • arreglo2 [][] = null;
  • Being the 1st the title. So far I could not understand anything. I hope you can help me please. I am also interested in having a Scroll to be able to download and upload and view the data.

        
    asked by Robert Gomez 14.11.2016 в 21:05
    source

    1 answer

    0

    Good if you're right, you have to create two arrangements

    1) for the headers (or column name) of your table

    String[] columnas= {"#", "origen", "destino", "duracion", "precio"};
    

    2) for the data in your table

    String [][] data = {
                    {"1", "Casa", "Trabajo", "20 minutos", "4000"},
                    {"2", "Trabajo", "Casa", "30 minutos", "5000"}
    };
    

    Then you have your table

    DefaultTableModel model = new DefaultTableModel(data, columnas);
    JTable tabla = new JTable(model);
    

    To add a scroll to your table

    JScrollPane scroll = new JScrollPane(tabla);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    add(scroll);
    

    Now to add different values to your table, as we have a model that is the order in which you want the data to be displayed

    Object[] row = {"3", "Casa", "Playa", "50 minutos", "10000"};
    DefaultTableModel model = (DefaultTableModel) tabla.getModel();
    
    model.addRow(row);
    

    EDIT

    Do not add all your code because I do not have the classes or anything like that, I could not accommodate visually how you want to get the view, but this solves the problem. As the class extends of JFrame we give add to the elements and it is not necessary to create a panel.

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        GUI frame = new GUI();
         frame.pack();
         frame.setVisible(true);
    }
    
    public GUI(){
        JTextField txtNumeroOrigen;
        JTextField txtNumeroDestino;
        JTextField txtDuracion;
        JTextField txtPrecioTotal;
        JCheckBox checkboxProvincial;
        JCheckBox checkboxLocal;
        JCheckBox checkboxFranja1;
        JCheckBox checkboxFranja2;
        JCheckBox checkboxFranja3;
        JTextField txtTotalDeLlamadas;
        JTextField txtPrecioActual;
        JTable table;
        setTitle("Call Center");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 471, 480);
    
        JLabel lblTipo = new JLabel("Tipo:");
        lblTipo.setBounds(16, 30, 46, 14);
        add(lblTipo);
    
        JLabel lblFraja = new JLabel("Franja:");
        lblFraja.setBounds(125, 30, 46, 14);
        add(lblFraja);
    
        JLabel lblNmeroOrigen = new JLabel("N\u00FAmero Origen");
        lblNmeroOrigen.setBounds(200, 30, 108, 14);
        add(lblNmeroOrigen);
    
        JLabel lblNmeroDestino = new JLabel("N\u00FAmero Destino");
        lblNmeroDestino.setBounds(200, 55, 108, 14);
        add(lblNmeroDestino);
    
        JLabel lblDuracion = new JLabel("Duraci\u00F3n");;
        lblDuracion.setBounds(200, 81, 108, 14);
        add(lblDuracion);
    
        txtNumeroOrigen = new JTextField();
        txtNumeroOrigen.setBounds(318, 27, 130, 20);
        add(txtNumeroOrigen);
        txtNumeroOrigen.setColumns(10);
    
        txtNumeroDestino = new JTextField();
        txtNumeroDestino.setBounds(318, 55, 130, 20);
        add(txtNumeroDestino);
        txtNumeroDestino.setColumns(10);
    
        txtDuracion = new JTextField();
        txtDuracion.setBounds(318, 86, 130, 20);
        add(txtDuracion);
        txtDuracion.setColumns(10);
    
        Object[][] data = {
                {"1", "Casa", "Trabajo", "20 minutos", "4000"},
                {"2", "Trabajo", "Casa", "30 minutos", "5000"}
         };
         //array de String's con los títulos de las columnas
         String[] columnNames = {"#", "origen", "destino", "duracion", "precio"};
         //se crea la Tabla
         table = new JTable(data, columnNames);
         //table.setPreferredScrollableViewportSize(new Dimension(500, 70));
         //Creamos un JscrollPane y le agregamos la JTable
         JScrollPane scrollPane = new JScrollPane(table);
         add(scrollPane);
    }
    
        
    answered by 14.11.2016 / 21:37
    source