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
3
  • could you please give me an example. Commented Feb 28, 2014 at 10:58
  • JButton[] btnBtns = new JButton[n]; Commented Feb 28, 2014 at 11:00
  • 1
    +1 for looking for a more efficient solution : ) I really like that way of thinking ;) Commented Feb 28, 2014 at 14:05

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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())) {}
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?
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
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

6 Comments

It's demo...use list, arraylist, array, and more
but I can't give each button a name, can i? for example button1, button2 and so on.
why not, change "Empty" to "Button" + index
You can access each button as JButton button = lists.get(i).
What did u do it for ??? you can get your button by index, or you can use Hash with key is your button's name
|
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++;
}

Comments

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;
  }
}

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.