Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

80
Views
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++;
        }
    }
}
8 months ago · Santiago Trujillo
1 answers
Answer question

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.

8 months ago · Santiago Trujillo Report
Answer question
Find remote jobs