Dynamic Graphical Interface in Java

3

I need to create something like this:

I usually do the GUI with the help of the IDE but in this case I can not (I think) because I need the GUI to repeat a number of undefined times, depending on the user in question.

What is the best way to do this? How do I deal with size problems such as component positions? In this case I think I will only need to display values but how do I SETtext/GETtext of component values?

    
asked by anonymous 06.08.2014 / 18:32

1 answer

5

The ideal is to create lists and place these components so you can easily access them later. And for the layout issue I recommend using box encapsulation (Box) as it can create two vertical and one horizontal to keep the layout in an organized way. (Of course you will need to make adjustments to make the program "pretty" .) But, just do some accounts and position the elements and you will not get an error.

importjavax.swing.Box;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JTextField;importjava.util.ArrayList;importjava.util.List;publicclassGuiApp1{publicstaticvoidmain(String[]args){newGuiApp1();}publicGuiApp1(){JFrameguiFrame=newJFrame();guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);guiFrame.setTitle("Example GUI");
        guiFrame.setSize(300,250);
        guiFrame.setLocationRelativeTo(null);

        Box caixaVertical = Box.createVerticalBox();

        List<JLabel> labelLista = new ArrayList<JLabel>();
        List<JTextField> inputLista = new ArrayList<JTextField>();
        for(int i=0;i<10;i++){
            Box caixaHorizontal = Box.createHorizontalBox();
            JLabel label = new JLabel("Label" + i + "         ");
            JTextField input = new JTextField(10);
            labelLista.add(label);
            inputLista.add(input);
            caixaHorizontal.add(label);
            caixaHorizontal.add(input);
            caixaVertical.add(caixaHorizontal);
        }
        guiFrame.add(caixaVertical);
        guiFrame.setVisible(true);
    }

}

Note 1: I did not bother with the aesthetics of this program

Note 2: I particularly prefer programming a lot of the Interface always in the hand.

(but I use an image editing program to view the desired placement i.e photoshop / paintshop etc)

    
06.08.2014 / 18:54