Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
How to force the for loop to be called again?

Hello i have this simple code that generates buttons and their actions on a loop. Default loop as 1 go, meaning 1 button.

My goal is to program the "Adicionar" button to increment the loop and make the loop generate more buttons.

here is the code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@SuppressWarnings("serial")
public class GuiClass extends JFrame{
    // PUBLIC VARS
    // VARIAVEIS
    public int numeroButoes = 1;
    // CONSTRUCTOR
    public GuiClass(){
        super("Loop Buttons");
        setLayout(null);
        // BUTOES ADD & REMOVE
        JButton mais = new JButton("Adiciona");
        //JButton menos = new JButton("Remove");
        mais.addActionListener(new handlerBotaoAdd());
        mais.setBounds(50, 0, 100, 40);
        add(mais);
        // LOOP BOTOES
        for (int x = 0; x < numeroButoes; x++ ){
            JButton teste = new JButton("Botão " + x);
            teste.setActionCommand("Botão " +x);
            teste.addActionListener(new handlerBotoesLoop(teste.getActionCommand()));
            teste.setBounds(450, (x == 0 ? 0: x+(40*x)), 100, 40);
            add(teste);
        }
    }
    private class handlerBotoesLoop implements ActionListener{
        String texto;
        public handlerBotoesLoop(String x){
            x = texto;
        }
        public void actionPerformed(ActionEvent event){
            JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
        }
    }
    private class handlerBotaoAdd implements ActionListener{
        public void actionPerformed(ActionEvent event){
            numeroButoes++;
        }
    }
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can use a Method to put the logic to create buttons and call that from button's click event

import javax.swing.*;
import java.awt.event.*;

@SuppressWarnings("serial")
public class Abc extends JFrame{
    // PUBLIC VARS
    // VARIAVEIS
    public int numeroButoes = 1;
    // CONSTRUCTOR
    public Abc(){
        super("Loop Buttons");
        setLayout(null);
        // BUTOES ADD & REMOVE
        JButton mais = new JButton("Adiciona");
        mais.addActionListener(new handlerBotaoAdd());
        //JButton menos = new JButton("Remove");
        mais.addActionListener(new handlerBotaoAdd());
        mais.setBounds(50, 0, 100, 40);
        add(mais);


    }

    // Method for button creating
    private void generateButtons(){
        for (int x = 0; x < numeroButoes; x++ ){
            JButton teste = new JButton("Botão " + x);
            teste.setActionCommand("Botão " +x);
            teste.addActionListener(new handlerBotoesLoop(teste.getActionCommand()));
            teste.setBounds(450, (x == 0 ? 0: x+(40*x)), 100, 40);
            add(teste);
        }
    }

    private class handlerBotoesLoop implements ActionListener{
        String texto;
        public handlerBotoesLoop(String x){
            x = texto;
        }
        @Override
        public void actionPerformed(ActionEvent event){
            JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
        }
    }
    private class handlerBotaoAdd implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent event){
            numeroButoes++;
            generateButtons();
        }
    }
}

I cant understand whether you want to create a single button or more than one button when you click Adicionar button. In my solution, every time you click the Adicionar button will create more than one button.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda