10

Is there anyway to minimize the number of lines of code to achieve the same thing

    L1.setFont(new Font("Serief", Font.BOLD, 24));
    L2.setFont(new Font("Serief", Font.BOLD, 24));
    L3.setFont(new Font("Serief", Font.BOLD, 24));
    L4.setFont(new Font("Serief", Font.BOLD, 24));
    L5.setFont(new Font("Serief", Font.BOLD, 24));
    L6.setFont(new Font("Serief", Font.BOLD, 24));
    L7.setFont(new Font("Serief", Font.BOLD, 24));
    L8.setFont(new Font("Serief", Font.BOLD, 24));
Nestlewater
  • 133
  • 8
  • 3
    what about grouping it up in a `List`? `for(WhateverThisClassIs item : whateverThisClassIslist) {item.setFont(...); `? – SomeJavaGuy Nov 15 '16 at 09:55
  • Isn't it Serif and not Serief? – Buhake Sindi Nov 15 '16 at 09:55
  • Why? In this case it would be more to the point to create only one `Font` object instead of eight (and also to spell 'Serif" correctly). – user207421 Nov 15 '16 at 09:56
  • Side note: read about java naming conventions. Field/variable names go camelCase; and even worse: there is absolutely **no** sense in saving "typing effort" by restricting yourself to single character names! – GhostCat Nov 15 '16 at 10:15
  • this question seem to belong on [codereview.stackexchange.com](http://codereview.stackexchange.com/) – Eliran Malka Nov 15 '16 at 11:21

4 Answers4

23

You could do

Font serif = new Font("Serif ", Font.BOLD, 24);
for (JLabel l : new JLabel[] { L1, L2, L3, L4, L5, L6, L7, L8 })
    l.setFont(serif);

In Java 8 you could write

Stream.of(L1, L2, L3, L4, L5, L6, L7, L8).forEach(l -> l.setFont(serif));
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
4

The other answers are all correct, but they take your design problem as a given. And simply spoken: I think that is wrong.

The point is: as soon as you start naming variables like L1, L2, L3, ... you are doing something very wrong. Especially when your next idea is to write specific code that deals with L1, and code that deals with L2; and so on. Maybe the other answers help you to fix this portion of your code. But sooner or later, you will have numerous pieces of deficient code; all dealing with those Lx guys in their own ways. Or maybe not even those labels, in this program. but some other piece of code where you start of with the same mistake of organizing your (UI) elements in an inefficient way.

The real solution here: you should use some sort of collection mechanism; either a List; or maybe a plain old array. Because then you can iterate that collection and have exactly one line of code to setup all elements in that collection the same way.

Or the other way round: you query the panel that holds all the label for its elements; and if an element matches a certain condition, then you update its properties as you need to. If you follow that path, you might not even have to keep any references to your elements in your "own code"; you simply use means that already exist; and could be as simply as this.

What I am saying: all nice ideas in those other solutions, but I think they go into the wrong direction. They try to use nice ideas to fix a broken apprach. To follow a mem that is pretty famous nowadays: consider such UI elements to be cattle, not pets. Meaning: they are mere "numbers" to you that you organize so that they can be accessed as a whole in an efficient (versus a pet that is seen as individual, having a name, and being addressed as "single entity" most of the time).

But just to add my short version; a slight variation of Peter's solution:

for (JLabel l : Arrays.asList( L1, L2, L3, L4, L5, L6, L7, L8 ))

would work, too.

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
2

Got it down to ... 4 lines ;-)

setFont(new Font("Serief", Font.BOLD, 24), L1, L2, L3, L4, L5, L6, L7, L8);

private void setFont(Font font, Object... objects){
    for(Object object : objects){
        object.setFont(font);
    }
}

As pointed out in the comments by Hulk, you should replace Object with with whatever class it is that you're calling setFont on.

Remember that whenever you see duplicatecode, you can often replace it with a method call. Here's some more info about why duplicate code is bad: https://en.wikipedia.org/wiki/Duplicate_code

JoeyJubb
  • 2,341
  • 1
  • 12
  • 19
  • 1
    Of course, `Object` has to be replaced with a type that actually has a `setFont`-method in order to make this compile - might be worth changing to something else to avoid confusion for beginners. – Hulk Nov 15 '16 at 10:42
2

Yes create a method called setFont(Object... values) and pass whatever number of objects on which you need to set font. Given below is an example of JLabel object.

void setFont(JLabel... jLabels){
    for(JLabel jLabel : jLabels){
        jLabel.setFont(new Font("Serif", Font.BOLD, 24));
    }
}

You can call it like

setFont(L1,L2,L3,L4); //any number of arguments
Chirag Parmar
  • 833
  • 11
  • 26