1

I am creating a program for a personal project and I ended up creating many JButtons, which are taking up quite bit of lines in the project, is it possible to reduce the amount of lines that still produces that same amount of JButton?

JButton button1 = new JButton("Empty"); 
JButton button2 = new JButton("Empty"); 
JButton button3 = new JButton("Empty"); 
JButton button4 = new JButton("Empty"); 
JButton button5 = new JButton("Empty"); 
JButton button6 = new JButton("Empty"); 
JButton button7 = new JButton("Empty"); 
JButton button8 = new JButton("Empty"); 
JButton button9 = new JButton("Empty"); 
JButton button10 = new JButton("Empty"); 
JButton button11 = new JButton("Empty"); 
and about 5 more
Roman C
  • 49,761
  • 33
  • 66
  • 176
Seeker
  • 509
  • 4
  • 19

5 Answers5

2

For repetitive tasks with similar functionality, I like to use methods. Most of the time it will make the code cleaner. It won't shorten the current code you have, but after you add listener or anything else you may want to add, it will. You can pass any number of variables you want to it.

public JButton createButton(String name, Color color) {
    JButton button = new JButton(name);
    button.setForeground(color);
    button.addActionListener(listener);
    return button;
}

Then you can just call it a bunch of times like

JButton button = createButton("Hello", Color.BLUE);

Overall though it's really a case by case basis on how you create your components. If you concern is really to shorten your code. You can always use loops, and some data structure for your the names

String[] names = { "Hey", "Ya", "Yada" };
JButton[] buttons = new JButton[names.lenght];
for (int i = 0; i < buttons.length; i++) {
    buttons[i] = createButton(names[i], Color.BLUE);
} 

Maybe even look at some of the answers from this question

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • hi, so the first three button name variable will be Hey, Ya and Yada. if you use your second method. so for example, JButton Hey = new JButton(); and so on. – Seeker Feb 28 '14 at 11:25
  • Nope, only the text of the button. _But_ the action command will be `"Hey"`. If you need to access it somewhere else in the code, you could loop through the array and check if `if ("Hey".equals(buttons[i].getActionCommand())) {}` – Paul Samsotha Feb 28 '14 at 11:28
  • so, you know your first example, the 'String name' parameter in the method, is that the text the button will have on it or is it the name for the button? – Seeker Feb 28 '14 at 11:59
  • It is the text and the actionCommand. The button will have no access variable. It can be accessed by the actionCommand. Or in a listener is can be accessed by checked if the event object is == to the button at a certain index – Paul Samsotha Feb 28 '14 at 12:04
1

U can create button with loops

Vector<Button> lists = new Vector<Button>();

for(int i = 0 ; i < 10000 ; i++) {
  JButton button = new JButton("Empty"); 
  lists.add(button);
}

And more, more.. other way

Thanh Le
  • 763
  • 4
  • 13
1
JButton[] buttons = new JButton[n];

int x=0;
while(x<n){

buttons[n]= new JButton("Button");
x++;
}

While n is the number of buttons you want

EDIT

If you want to give Separate Names to Buttons Like button1, 2 ,3

int x=0;
while(x<n){

buttons[n]= new JButton("Button "+(x+1));   //x+1 if you want to start from 1, because x is 0
x++;
}
Adnan Ahmad Khan
  • 634
  • 2
  • 7
  • 25
1

As others have noted, you can make a loop and add buttons, to an array, but Java doesn't support macros [1], which seems to be the thing you wish to do (that is have the code process automated, but still retain sensible variable names).

Note: While it's possible to hack some kind of simple macro format in Java but I'd advice not to.

Your best bet would be to look into code generators that generate the Java - this might be a good starting point.

For generating code I just use FreeMarker Template. It's not perfect, but it's decent. Basically you'll have some kind of program (Java program, Ant task, etc.) that will call FreeMarker libraries, give them templates and data, in return that will generate the expected code.

Community
  • 1
  • 1
Daniel Fath
  • 16,453
  • 7
  • 47
  • 82
1
import java.awt.Component;
import java.awt.Container;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;

public class ComponentFactory {

  public static void main(String[] args) {
    Box box=Box.createVerticalBox();
    addAll(box, createButtons("foo", "bar", "baz")); // <----------
    JFrame f=new JFrame("Example");
    f.setContentPane(box);
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }

  static void addAll(Container target, Component... all) {
    for(Component c:all) target.add(c);
  }

  static JButton[] createButtons(String... label) {
    final int num=label.length;
    JButton[] all=new JButton[num];
    for(int i=0; i<num; i++) all[i]=new JButton(label[i]);
    return all;
  }
}
Holger
  • 285,553
  • 42
  • 434
  • 765