Dynamic graphical user interface in Java

Asked

Viewed 1,357 times

3

I need to create something like this:

IMG

img

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

What’s the best way to do this? How do I deal with the problems of sizes as component positions? In this case I think I’ll just need to show values but how do I SETtext/GETtext of the values of the components?

1 answer

5


The ideal is to create lists and place these components so you can easily access them later. And for the question of the layout I recommend using the emcapsulamento in boxes (Box) because it can create two vertical and one horizontal to keep the layout in an organized way. (Of course you will need adjustments to make the program "beautiful". But, just make some accounts and position the elements and you will not have error.

inserir a descrição da imagem aqui

import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.util.ArrayList;
import java.util.List;

public class GuiApp1 {

    public static void main(String[] args) {

        new GuiApp1();
    }

    public GuiApp1()
    {
        JFrame guiFrame = new JFrame();
        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 didn’t care about the aesthetics of this program

Note 2: I particularly prefer to program much of the interface always at hand.

(but use an image editing program to preview the desired positioning i.e photoshop/paintshop etc)

  • Sorry just to clarify But I don’t want the components to repeat themselves, I want the whole structure to repeat itself, but as far as the components are concerned, I figured, try to use some structure to organize them right away, so I’m not defining the position of component by component. But I don’t understand how to access them, you can post an example?

  • I wasn’t thinking right, as the structure(panel) repeats itself is quite easy, I created a function that returns me a panel, then just add to the frame OK, but I’m having trouble accessing the components, I read the logic of the lists but in your example it is easy only these to use label and textbox but I wanted more components do not know any better way to do it?

  • @jsantos1991 as you want to repeat something standardized, create a class that you inherit from the class JPanel and create public methods to access what you need for each component. (Note that you said what you need and not the components). For example in your class MeuPanel create a setCheckBox1() and work and directly return the value from it. Then make a list of these components.

  • Thank you very much, that’s what I was thinking, when I asked the question I was really confused about the dynamic GUI, for being dynamic and mainly for my lack of experience I am now much more enlightened... Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.