1

I have an ArrayString with some data that i want to be printed on a GUI, i started using JPanel and the such and ended up with windowbuilder on Eclipse to help me modify the visual aspects, i think that my code is horrible (or at least looks like it) I have found some similar threads to my question but i don't really grasp the idea:

Could i not use a Jtable instead of what looks like a load of crap? I just can't get my head round the JTable, i want it to look just like my code output but i don't understand how the table would read the arraystring and the "output it" like my table, i don't know how to implement the combo boxes either... any help would be of much use, thanks!

Edit: my first idea was that each "Jtextfield" would "import" each data line but im thinking that this is very unefficient and probably prone to coding errors when i could make my life simpler?

here's an image of the rendered code: http://i57.tinypic.com/34j7mtd.jpg

enter image description here

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;


public class InfoVehiculos extends JFrame {
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;
    private JTextField textField_5;
    private JTextField textField_6;
    private JTextField textField_7;
    private JTextField textField_8;
    private JTextField textField_9;
    private JTextField textField_10;
    private JTextField textField_11;
    private JTextField textField_12;
    private JTextField textField_13;
    private JTextField textField_14;
    private JTextField textField_15;
    private JTextField textField_16;
    private JTextField textField_17;
    private JTextField textField_18;
    private JTextField textField_19;
    private JTextField textField_20;
    private JTextField textField_21;
    private JTextField textField_22;
    private JTextField textField_23;
    private JTextField textField_24;
    private JTextField textField_25;
    private JTextField textField_26;
    private JTextField textField_27;
    private JTextField textField_28;
    private JTextField textField_29;
    private JTextField textField_30;
    private JTextField textField_31;
    private JTextField textField_32;
    private JTextField textField_33;
    private JTextField textField_34;

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

    /**
     * Create the frame.
     */
    public InfoVehiculos() 
    {
        setTitle("Informacion");
        setBounds(100, 100, 495, 309);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        JLabel lblTipo = new JLabel("Tipo");
        lblTipo.setBounds(39, 24, 46, 14);
        getContentPane().add(lblTipo);

        JLabel lblMarca = new JLabel("Marca");
        lblMarca.setBounds(95, 24, 46, 14);
        getContentPane().add(lblMarca);

        JLabel lblModelo = new JLabel("Modelo");
        lblModelo.setBounds(151, 24, 46, 14);
        getContentPane().add(lblModelo);

        JLabel lblMatrcula = new JLabel("Matricula");
        lblMatrcula.setBounds(207, 24, 46, 14);
        getContentPane().add(lblMatrcula);

        JLabel lblItv = new JLabel("ITV");
        lblItv.setBounds(281, 24, 46, 14);
        getContentPane().add(lblItv);

        JLabel lblParking = new JLabel("Parking");
        lblParking.setBounds(337, 24, 46, 14);
        getContentPane().add(lblParking);

        JLabel lblEstado = new JLabel("Estado");
        lblEstado.setBounds(410, 24, 46, 14);
        getContentPane().add(lblEstado);

        //Cada combo box se puede desplegar para A/B/C
        JComboBox ComboTipo = new JComboBox();
        ComboTipo.setBounds(25, 49, 35, 20);
        ComboTipo.addItem("A");
        ComboTipo.addItem("B");
        ComboTipo.addItem("C");
        getContentPane().add(ComboTipo);

        JComboBox ComboTipo1 = new JComboBox();
        ComboTipo1.setBounds(25, 80, 35, 20);
        ComboTipo1.addItem("A");
        ComboTipo1.addItem("B");
        ComboTipo1.addItem("C");
        getContentPane().add(ComboTipo1);

        JComboBox ComboTipo2 = new JComboBox();
        ComboTipo2.setBounds(25, 111, 35, 20);
        ComboTipo2.addItem("A");
        ComboTipo2.addItem("B");
        ComboTipo2.addItem("C");
        getContentPane().add(ComboTipo2);

        JComboBox ComboTipo3 = new JComboBox();
        ComboTipo3.setBounds(25, 142, 35, 20);
        ComboTipo3.addItem("A");
        ComboTipo3.addItem("B");
        ComboTipo3.addItem("C");
        getContentPane().add(ComboTipo3);

        JComboBox ComboTipo4 = new JComboBox();
        ComboTipo4.setBounds(25, 173, 35, 20);
        ComboTipo4.addItem("A");
        ComboTipo4.addItem("B");
        ComboTipo4.addItem("C");
        getContentPane().add(ComboTipo4);

        JComboBox ComboTipo5 = new JComboBox();
        ComboTipo5.setBounds(25, 204, 35, 20);
        ComboTipo5.addItem("A");
        ComboTipo5.addItem("B");
        ComboTipo5.addItem("C");
        getContentPane().add(ComboTipo5);

        JComboBox ComboTipo6 = new JComboBox();
        ComboTipo6.setBounds(25, 235, 35, 20);
        ComboTipo6.addItem("A");
        ComboTipo6.addItem("B");
        ComboTipo6.addItem("C");
        getContentPane().add(ComboTipo6);

        //Cada combobox tiene activo o inactivo
        JComboBox ComboEstado = new JComboBox();
        ComboEstado.setBounds(408, 49, 50, 20);
        ComboEstado.addItem("Activo");
        ComboEstado.addItem("Inactivo");
        getContentPane().add(ComboEstado);

        JComboBox ComboEstado1 = new JComboBox();
        ComboEstado1.setBounds(408, 80, 50, 20);
        ComboEstado1.addItem("Activo");
        ComboEstado1.addItem("Inactivo");
        getContentPane().add(ComboEstado1);

        JComboBox ComboEstado2 = new JComboBox();
        ComboEstado2.setBounds(408, 111, 50, 20);
        ComboEstado2.addItem("Activo");
        ComboEstado2.addItem("Inactivo");
        getContentPane().add(ComboEstado2);

        JComboBox ComboEstado3 = new JComboBox();
        ComboEstado3.setBounds(408, 142, 50, 20);
        ComboEstado3.addItem("Activo");
        ComboEstado3.addItem("Inactivo");
        getContentPane().add(ComboEstado3);

        JComboBox ComboEstado4 = new JComboBox();
        ComboEstado4.setBounds(408, 173, 50, 20);
        ComboEstado4.addItem("Activo");
        ComboEstado4.addItem("Inactivo");
        getContentPane().add(ComboEstado4);

        JComboBox ComboEstado5 = new JComboBox();
        ComboEstado5.setBounds(408, 204, 50, 20);
        ComboEstado5.addItem("Activo");
        ComboEstado5.addItem("Inactivo");
        getContentPane().add(ComboEstado5);

        JComboBox ComboEstado6 = new JComboBox();
        ComboEstado6.setBounds(408, 235, 50, 20);
        ComboEstado6.addItem("Activo");
        ComboEstado6.addItem("Inactivo");
        getContentPane().add(ComboEstado6);

        textField = new JTextField();
        textField.setBounds(70, 49, 71, 20);
        getContentPane().add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setBounds(70, 80, 71, 20);
        getContentPane().add(textField_1);
        textField_1.setColumns(10);

        textField_2 = new JTextField();
        textField_2.setBounds(70, 111, 71, 20);
        getContentPane().add(textField_2);
        textField_2.setColumns(10);

        textField_3 = new JTextField();
        textField_3.setBounds(70, 142, 71, 20);
        getContentPane().add(textField_3);
        textField_3.setColumns(10);

        textField_4 = new JTextField();
        textField_4.setBounds(70, 173, 71, 20);
        getContentPane().add(textField_4);
        textField_4.setColumns(10);

        textField_5 = new JTextField();
        textField_5.setBounds(70, 204, 72, 20);
        getContentPane().add(textField_5);
        textField_5.setColumns(10);

        textField_6 = new JTextField();
        textField_6.setBounds(70, 235, 72, 20);
        getContentPane().add(textField_6);
        textField_6.setColumns(10);

        textField_7 = new JTextField();
        textField_7.setBounds(151, 49, 48, 20);
        getContentPane().add(textField_7);
        textField_7.setColumns(10);

        textField_8 = new JTextField();
        textField_8.setBounds(151, 80, 48, 20);
        getContentPane().add(textField_8);
        textField_8.setColumns(10);

        textField_9 = new JTextField();
        textField_9.setBounds(151, 111, 48, 20);
        getContentPane().add(textField_9);
        textField_9.setColumns(10);

        textField_10 = new JTextField();
        textField_10.setBounds(151, 142, 48, 20);
        getContentPane().add(textField_10);
        textField_10.setColumns(10);

        textField_11 = new JTextField();
        textField_11.setBounds(151, 173, 48, 20);
        getContentPane().add(textField_11);
        textField_11.setColumns(10);

        textField_12 = new JTextField();
        textField_12.setBounds(151, 204, 48, 20);
        getContentPane().add(textField_12);
        textField_12.setColumns(10);

        textField_13 = new JTextField();
        textField_13.setBounds(151, 235, 48, 20);
        getContentPane().add(textField_13);
        textField_13.setColumns(10);

        textField_14 = new JTextField();
        textField_14.setBounds(207, 49, 59, 20);
        getContentPane().add(textField_14);
        textField_14.setColumns(10);

        textField_15 = new JTextField();
        textField_15.setBounds(207, 80, 59, 20);
        getContentPane().add(textField_15);
        textField_15.setColumns(10);

        textField_16 = new JTextField();
        textField_16.setBounds(207, 111, 59, 20);
        getContentPane().add(textField_16);
        textField_16.setColumns(10);

        textField_17 = new JTextField();
        textField_17.setBounds(207, 142, 59, 20);
        getContentPane().add(textField_17);
        textField_17.setColumns(10);

        textField_18 = new JTextField();
        textField_18.setBounds(207, 173, 59, 20);
        getContentPane().add(textField_18);
        textField_18.setColumns(10);

        textField_19 = new JTextField();
        textField_19.setBounds(207, 204, 59, 20);
        getContentPane().add(textField_19);
        textField_19.setColumns(10);

        textField_20 = new JTextField();
        textField_20.setBounds(207, 235, 59, 20);
        getContentPane().add(textField_20);
        textField_20.setColumns(10);

        textField_21 = new JTextField();
        textField_21.setBounds(269, 49, 32, 20);
        getContentPane().add(textField_21);
        textField_21.setColumns(10);

        textField_22 = new JTextField();
        textField_22.setBounds(269, 80, 32, 20);
        getContentPane().add(textField_22);
        textField_22.setColumns(10);

        textField_23 = new JTextField();
        textField_23.setBounds(269, 111, 32, 20);
        getContentPane().add(textField_23);
        textField_23.setColumns(10);

        textField_24 = new JTextField();
        textField_24.setBounds(269, 142, 32, 20);
        getContentPane().add(textField_24);
        textField_24.setColumns(10);

        textField_25 = new JTextField();
        textField_25.setBounds(269, 173, 32, 20);
        getContentPane().add(textField_25);
        textField_25.setColumns(10);

        textField_26 = new JTextField();
        textField_26.setBounds(269, 204, 32, 20);
        getContentPane().add(textField_26);
        textField_26.setColumns(10);

        textField_27 = new JTextField();
        textField_27.setBounds(269, 235, 32, 20);
        getContentPane().add(textField_27);
        textField_27.setColumns(10);

        textField_28 = new JTextField();
        textField_28.setBounds(312, 49, 86, 20);
        getContentPane().add(textField_28);
        textField_28.setColumns(10);

        textField_29 = new JTextField();
        textField_29.setBounds(312, 80, 86, 20);
        getContentPane().add(textField_29);
        textField_29.setColumns(10);

        textField_30 = new JTextField();
        textField_30.setBounds(312, 111, 86, 20);
        getContentPane().add(textField_30);
        textField_30.setColumns(10);

        textField_31 = new JTextField();
        textField_31.setBounds(312, 142, 86, 20);
        getContentPane().add(textField_31);
        textField_31.setColumns(10);

        textField_32 = new JTextField();
        textField_32.setBounds(312, 173, 86, 20);
        getContentPane().add(textField_32);
        textField_32.setColumns(10);

        textField_33 = new JTextField();
        textField_33.setBounds(312, 204, 86, 20);
        getContentPane().add(textField_33);
        textField_33.setColumns(10);

        textField_34 = new JTextField();
        textField_34.setBounds(312, 235, 86, 20);
        getContentPane().add(textField_34);
        textField_34.setColumns(10);

    }
}
Braj
  • 46,415
  • 5
  • 60
  • 76
pablo
  • 17
  • 4
  • use .toArray() method to port the ArrayList to Array, then use it in the constructor or JTable – Marco Acierno May 16 '14 at 16:11
  • Read about DefaultTableModel and JTable. It will be very easy if you just want to display data. – Abu Sulaiman May 16 '14 at 16:16
  • `Could i not use a Jtable instead` - that is what I would suggest. It would get rid of all the variables and your null layout code. `i want it to look just like my code output but i don't understand how the table would read the arraystring` Check out the section from the Swing tutorial on [How to Use Tables](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) for working examples. You would need to load the data from the Array into the TableModel, using the `addRow(...)` method of the `DefaultTableModel`. It also shows how to use combo boxes. – camickr May 16 '14 at 17:46
  • Thanks for the info! I used to null to make sure each Jtext etc was in the position i wanted but i now see how gridlayout will do this automatically for me – pablo May 17 '14 at 12:53

2 Answers2

0
  • Don't use null Layout at all. Avoid setBounds() if possible.

  • There is no need of calling setBounds() if you uses a proper layout.

  • Respect the Layout Manager and handover the responsibility of the component positioning and sizing to the Layout Manager that's why it's made for it.


The same layout you can achieved by using GridLayout or GridBagLayout.

For more info have a look at below links:

Braj
  • 46,415
  • 5
  • 60
  • 76
  • Thanks! I did think it would help but i chose null to make sure everything was where i wanted but as you said if this does it automatically.... – pablo May 17 '14 at 12:55
  • @pablo If you uses any Layout then it's the responsibility of the Layout Manager itself to manage the position and size of the components but in case of `null` Layout. – Braj May 17 '14 at 14:44
  • GridLayout is good for discrete components (e.g. numbered buttons in a phone keyboard arrangement) but the question is about a row-by-row GUI for an arbitrary sequence of similar entities, the most typical use case for JTable and TableModel. A GridLayout containing appropriate controls for each column would be laid out correctly, but it would be a primitive and cumbersome reimplementation of JTable functionality (managing arbitrary row counts, maintaining a model, dynamically creating and swapping widgets, etc.). – Lorenzo Gatti May 19 '14 at 14:10
0

Create table and model, then create column headers:

DefaultTableModel dftModel = new DefaultTableModel();
JTable mytable = new JTable(dftModel);
myTable.setColumnIdentifiers(new String [] {"Tipo","Column 1","Column 2","Column 3"});

Add your rows from you arraylist however you want:

myTable.addRow(new String[] {"",yourArrayList.get(1),"blah","blah"});

Add your Tipo Combo Box like this:

String [] tipArray = {"A","B","C"};
DefaultComboBoxModel comboModel = new DefaultComboBoxModel(tipArray);
JComboBox ComboTipo = new JComboBox(comboModel);
mytable.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(ComboTipo));
Abu Sulaiman
  • 1,477
  • 2
  • 18
  • 32
  • And how would i change the cells in the table to combo boxes? just far left and right? – pablo May 17 '14 at 12:54
  • http://stackoverflow.com/questions/20526917/load-arraylist-data-into-jtable i was thinking of something like the last example – pablo May 17 '14 at 12:56
  • Take a look at this example and let me know if this helps: http://stackoverflow.com/questions/14355712/adding-jcombobox-to-a-jtable-cell – Abu Sulaiman May 19 '14 at 00:21
  • @pablo, You probably already figured it out, but I edited the code above to show you how I would add the combo boxes to the table. – Abu Sulaiman May 19 '14 at 14:01