How to refactor a duplicate line of java code

I need to create many buttons with information from excel file, each button has different information, but now the method that creates the buttons exceeds the 65535 byte limit, so I was thinking about refactoring the method that creates the buttons but I don't know if its possible treat each button a little differently than the previous one, here's an example of what they do:

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))+"% ");

        }
    });

      

Hope I explained it well, thanks in advance.

+3


source to share


2 answers


Refactor the code you have, or continue JRadioButton

by passing the "differences" to the constructor:

public class MyJRadioButton extends JRadioButton {
    public MyJRadioButton(String title, final int x, final double y) {
        super(title);
        addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                IOE.set(x, y);
                StringBuilder text = new StringBuilder("IOE:");
                for (int i = 0; i < 22; i++)
                    text.append(IOE.get(i));
                label_IOE.setText(text + "% ");
            }
        });
    }
}

      

Then to use for example:

JRadioButton rdbtnNoDataIOE1 = new MyJRadioButton("No Data", 0, 0.0);

      



Or, if you'd rather not extend the component, here's a version of the factory method:

public static JRadioButton create(String title, final int x, final double y) {
    JRadioButton button = JRadioButton(title);
    button.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            IOE.set(x, y);
            StringBuilder text = new StringBuilder("IOE:");
            for (int i = 0; i < 22; i++)
                text.append(IOE.get(i));
            label_IOE.setText(text + "% ");
        }
    });
    return button;
}

      

which you are using for example:

JRadioButton rdbtnNoDataIOE1 = create("No Data", 0, 0.0);

      

0


source


It seems to me that you can create one ActionListener subclass with a constructor that takes two parameters that you pass to the 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 might just be (for example) rdbtn1IOE1.addActionListener(new IOESetActionListener(0,0.8));

+2


source







All Articles