Josef Sigron
Josef Sigron

Reputation: 65

How to prevent duplication while creating multiple similar jButtons

I'm trying to learn about java GUI , and I can't seem but notice that there is a lot of duplication going around when trying to create multiple similar buttons. For example in this piece of code :


JButton btnAddCar = new JButton("Add Car");
btnAddCar.setFont(new Font("Tahoma", Font.PLAIN, 20));
btnAddCar.setBounds(5, 5, 355, 155);
addInternalFrame.getContentPane().add(btnAddCar);

JButton btnAddHybridCar = new JButton("Add Hybrid Car");
btnAddHybridCar.setFont(new Font("Tahoma", Font.PLAIN, 20));
btnAddHybridCar.setBounds(5, 165, 355, 155);
addInternalFrame.getContentPane().add(btnAddHybridCar);

JButton btnAddVan = new JButton("Add Van");
btnAddVan.setFont(new Font("Tahoma", Font.PLAIN, 20));
btnAddVan.setBounds(5, 325, 355, 155);
addInternalFrame.getContentPane().add(btnAddVan);

JButton btnAddMotorcycle = new JButton("Add Motorcycle");
btnAddMotorcycle.setFont(new Font("Tahoma", Font.PLAIN, 20));
btnAddMotorcycle.setBounds(5, 485, 355, 155);
addInternalFrame.getContentPane().add(btnAddMotorcycle);

JButton btnAddHybridMotorcycle = new JButton("Add Hybrid Motorcycle");
btnAddHybridMotorcycle.setFont(new Font("Tahoma", Font.PLAIN, 20));
btnAddHybridMotorcycle.setBounds(5, 645, 355, 155);
addInternalFrame.getContentPane().add(btnAddHybridMotorcycle);

JButton btnAddCustomer = new JButton("Add Customer");
btnAddCustomer.setFont(new Font("Tahoma", Font.PLAIN, 20));
btnAddCustomer.setBounds(365, 5, 355, 155);
addInternalFrame.getContentPane().add(btnAddCustomer);

JButton btnAddVIPCustomer = new JButton("Add VIP Customer");
btnAddVIPCustomer.setFont(new Font("Tahoma", Font.PLAIN, 20));
btnAddVIPCustomer.setBounds(365, 165, 355, 155);
addInternalFrame.getContentPane().add(btnAddVIPCustomer);

JButton btnAddEmployee = new JButton("Add Employee");
btnAddEmployee.setFont(new Font("Tahoma", Font.PLAIN, 20));
btnAddEmployee.setBounds(365, 325, 355, 155);
addInternalFrame.getContentPane().add(btnAddEmployee);

JButton btnAddDepartmentManager = new JButton("Add Department Manager");
btnAddDepartmentManager.setFont(new Font("Tahoma", Font.PLAIN, 20));
btnAddDepartmentManager.setBounds(365, 485, 355, 155);
addInternalFrame.getContentPane().add(btnAddDepartmentManager);

Is there a shorter, aesthetic way of writing this code?

Upvotes: 0

Views: 36

Answers (1)

da-Kai
da-Kai

Reputation: 28

you could try to outsource the button creation into a method like this

Note: you might also have to add addInternalFrame as a parameter

private JButton createButton(String name, int x, int y){
      JButton button = new JButton(name);
      button.setFont(new Font("Tahoma", Font.PLAIN, 20));
      button.setBounds(x, y, 355, 155);
      addInternalFrame.getContentPane().add(button);
      return button;
    }

and create your buttons with it

JButton btnAddCar = createButton("Add Car", 5, 5);
JButton btnAddHybridCar = createButton("Add Hybrid Car", 5, 165);

Upvotes: 1

Related Questions