create dynamic buttons with java jframe code

Technology

create dynamic buttons with java jframe code

code to create dynamic buttons with java using jframe


code to create dynamic buttons with java using jframe

In some projects that we face in the study of the university, at work or MBA project, we are faced with the need to find a simple and easy code in which to create Components in Java dynamically, just clicking on a button can generate several components of swing and interface so that the client can interact with the program that we develop.

The way we generate set code or simple code is not the best way, since from experience the client for whom we are developing a software wants more changes in the presentation or functionality that is why I present the best way to create panels (Jpanel) , Labels (Jlabel), Texarea (JTextArea) with a button without the need to program them from scratch.

The first thing we should do is create a java Jframe form as in the following image:

create a java Jframe


we will call this form "Aplicacion", inside this form we are going to add a button named add button (Button), we are going to add a scrollpanel (JScrollPane) and inside this JScrollPane we are going to add a panel (JPanel):


add button in java

We create the following variables:





      ArrayList<JButton> botones;
    private int indice;

and inside the constructor we place the following code:


       botones = new ArrayList<>();
        indice=0;

the code would look like the following image:

code to create dynamic buttons with java using jframe 2
We proceed to create the action of the button or the component. I remind you that you can place any interface jlabel, Jtexarea, Jpanel etc, within this action we place the following code:





 




JButton boton= new JButton("https://www.mbajava.com/ "+indice);
        panel.add(boton);
        botones.add(boton);
        indice++;
        crearActionListenerButon(boton);
        panel.updateUI();
the code looks like the following image:


code to create dynamic buttons in java 3


You may be interested in the following entry recursively organize an array


Post a Comment

0 Comments