Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refactor a repetitive line of code java

I need to create a lot of buttons with information from an excel file, each button have different information but right now the method that creates the buttons is exceeding the 65535 bytes limit so I was thinking of refactoring the method that creates the buttons but I don't know if its possible considering each button is a little different than the previous one, here is an example of what im doing:

JRadioButton rdbtn1IOE1 = new JRadioButton("Cruzamiento con algún Cuerpo de Agua - Sí");
    rdbtn1IOE1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            IOE.set(0,0.8);
            label_IOE.setText("IOE:"+(IOE.get(0)+IOE.get(1)+IOE.get(2)+IOE.get(3)+IOE.get(4)+IOE.get(5)+IOE.get(6)+IOE.get(7)+
                    IOE.get(8)+IOE.get(9)+IOE.get(10)+IOE.get(11)+IOE.get(12)+IOE.get(13)+IOE.get(14)+IOE.get(15)+IOE.get(16)+IOE.get(17)+
                    IOE.get(18)+IOE.get(19)+IOE.get(20)+IOE.get(21))+"% ");

        }
    });
    JRadioButton rdbtn2IOE1 = new JRadioButton("Cruzamiento con algún Cuerpo de Agua - No");
    rdbtn2IOE1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            IOE.set(0,0.0);
            label_IOE.setText("IOE:"+(IOE.get(0)+IOE.get(1)+IOE.get(2)+IOE.get(3)+IOE.get(4)+IOE.get(5)+IOE.get(6)+IOE.get(7)+
                    IOE.get(8)+IOE.get(9)+IOE.get(10)+IOE.get(11)+IOE.get(12)+IOE.get(13)+IOE.get(14)+IOE.get(15)+IOE.get(16)+IOE.get(17)+
                    IOE.get(18)+IOE.get(19)+IOE.get(20)+IOE.get(21))+"% ");

        }
    });
    JRadioButton rdbtnNoDataIOE1 = new JRadioButton("No Data");
    rdbtnNoDataIOE1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            IOE.set(0,0.0);
            label_IOE.setText("IOE:"+(IOE.get(0)+IOE.get(1)+IOE.get(2)+IOE.get(3)+IOE.get(4)+IOE.get(5)+IOE.get(6)+IOE.get(7)+
                    IOE.get(8)+IOE.get(9)+IOE.get(10)+IOE.get(11)+IOE.get(12)+IOE.get(13)+IOE.get(14)+IOE.get(15)+IOE.get(16)+IOE.get(17)+
                    IOE.get(18)+IOE.get(19)+IOE.get(20)+IOE.get(21))+"% ");

        }
    });

    JRadioButton rdbtn1IOE2 = new JRadioButton("< 100 metros");
    rdbtn1IOE2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            IOE.set(1,0.1);
            label_IOE.setText("IOE:"+(IOE.get(0)+IOE.get(1)+IOE.get(2)+IOE.get(3)+IOE.get(4)+IOE.get(5)+IOE.get(6)+IOE.get(7)+
                    IOE.get(8)+IOE.get(9)+IOE.get(10)+IOE.get(11)+IOE.get(12)+IOE.get(13)+IOE.get(14)+IOE.get(15)+IOE.get(16)+IOE.get(17)+
                    IOE.get(18)+IOE.get(19)+IOE.get(20)+IOE.get(21))+"% ");

        }
    });
    JRadioButton rdbtnNoDataIOE2 = new JRadioButton("No Data");
    rdbtnNoDataIOE2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            IOE.set(1,0.0);
            label_IOE.setText("IOE:"+(IOE.get(0)+IOE.get(1)+IOE.get(2)+IOE.get(3)+IOE.get(4)+IOE.get(5)+IOE.get(6)+IOE.get(7)+
                    IOE.get(8)+IOE.get(9)+IOE.get(10)+IOE.get(11)+IOE.get(12)+IOE.get(13)+IOE.get(14)+IOE.get(15)+IOE.get(16)+IOE.get(17)+
                    IOE.get(18)+IOE.get(19)+IOE.get(20)+IOE.get(21))+"% ");

        }
    });
    JRadioButton rdbtn2IOE2 = new JRadioButton(">= 100 to <= 200 metros");
    rdbtn2IOE2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            IOE.set(1,0.05);
            label_IOE.setText("IOE:"+(IOE.get(0)+IOE.get(1)+IOE.get(2)+IOE.get(3)+IOE.get(4)+IOE.get(5)+IOE.get(6)+IOE.get(7)+
                    IOE.get(8)+IOE.get(9)+IOE.get(10)+IOE.get(11)+IOE.get(12)+IOE.get(13)+IOE.get(14)+IOE.get(15)+IOE.get(16)+IOE.get(17)+
                    IOE.get(18)+IOE.get(19)+IOE.get(20)+IOE.get(21))+"% ");

        }
    });

I hope I explained this well, thank you in advance.

like image 351
Vhalad Avatar asked Mar 02 '26 11:03

Vhalad


1 Answers

This looks to me like you could create a single ActionListener subclass, with a constructor that takes the two parameters that you are passing to IOE.set.

public class IOESetActionListener extends ActionListener {
    private final int a;
    private final double b;
    public IOESetActionListener(int a, double b) {
        this.a = a;
        this.b = b;
    }

    public void actionPerformed(ActionEvent e) {
        IOE.set(a, b);
        final StringBuilder builder = new StirngBuilder("IOE:");
        for (int i = 0; i < 22; ++i) {  
          builder.append(IOE.get(i));
        }
        label_IOE.setText(builder.append("% ").toString());
    }
}

Then your buttons can just be (for example) rdbtn1IOE1.addActionListener(new IOESetActionListener(0,0.8));

like image 155
Daniel Avatar answered Mar 03 '26 23:03

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!