patternjavaMinor
Label with Subtitle
Viewed 0 times
labelsubtitlewith
Problem
This code shows a title with an image and subtitle:
What improvements can I make in this code and is there a better of doing this?
What improvements can I make in this code and is there a better of doing this?
package Components.CustomLabel;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
/**
*
* @author Aamir khan
*/
public class LabelWithSubTitle extends GridPane {
private final Label title = new Label();
private final Label subTitle = new Label();
private final ImageView thumb = new ImageView();
public LabelWithSubTitle(String titleString, String subString, Image thumb) {
setTitle(titleString);
setSubTitle(subString);
setThumb(thumb);
init();
}
private void setSubTitle(String sub) {
subTitle.setText(sub);
}
private void setThumb(Image img) {
thumb.setImage(img);
}
private void setTitle(String titleString) {
title.setText(titleString);
}
private void init() {
add(thumb, 0, 0, 1, 2);
add(title, 1, 0);
add(subTitle, 1, 1);
}
}Solution
Just tome nit picks:
Formatting
This does not make sense:
I am not sure if this is the formatting problem in copy and pasting from IDE or it was like this in the IDE in the first place.
To correctly format your code here, you can copy and paste from your IDE, select all your code, and press
If it is the original way in the IDE, your IDE should have a format option. If you can't find it, search it up on google: you'll eventually find it.
Naming
Subtitle is one word, so you can easily change
Here, you have some shortened names, like
This also applies to the Constructor.
Misc
At first glance, this makes no sense. These numbers are more like magic numbers: make them a constant:
and
Note that I ignored the row span because it was
Final Code:
Formatting
This does not make sense:
}
}I am not sure if this is the formatting problem in copy and pasting from IDE or it was like this in the IDE in the first place.
To correctly format your code here, you can copy and paste from your IDE, select all your code, and press
ctrl-k or click the {} button at the top of the editing pane:If it is the original way in the IDE, your IDE should have a format option. If you can't find it, search it up on google: you'll eventually find it.
Naming
Subtitle is one word, so you can easily change
subTitle to subtitle. This applies to may parts of your code.private void setSubtitle(String sub) {
subtitle.setText(sub);
}
private void setThumb(Image img) {
thumb.setImage(img);
}
private void setTitle(String titleString) {
title.setText(titleString);
}Here, you have some shortened names, like
sub, and extended names, like titleString. Names should he concise, but understandable. Try:sub->subtitleText
img->image
titleString->titleText
This also applies to the Constructor.
Misc
private void init() {
add(thumb, 0, 0, 1, 2);
add(title, 1, 0);
add(subTitle, 1, 1);
}At first glance, this makes no sense. These numbers are more like magic numbers: make them a constant:
private static final int THUMB_COORDINATES_X = 0;
private static final int THUMB_COORDINATES_Y = 0;
private static final int THUMB_SPAN_Y = 2;
private static final int TITLE_COORDINATES_X = 1;
private static final int TITLE_COORDINATES_Y = 0;
private static final int SUBTITLE_COORDINATES_X = 1;
private static final int SUBTITLE_COORDINATES_Y = 1;and
private void init() {
add(thumb, THUMB_COORDINATES_X, THUMB_COORDINATES_Y);
setColumnSpan(thumb, THUMB_SPAN_Y);
add(title, TITLE_COORDINATES_X, TITLE_COORDINATES_Y);
add(subTitle, SUBTITLE_COORDINATES_X, SUBTITLE_COORDINATES_Y);
}Note that I ignored the row span because it was
1; it's not necessary.Final Code:
package Components.CustomLabel;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
/**
*
* @author Aamir khan
*/
public class LabelWithSubtitle extends GridPane {
private final Label title = new Label();
private final Label subtitle = new Label();
private final ImageView thumb = new ImageView();
public LabelWithSubtitle(String titleText, String subtitleText, Image thumb) {
setTitle(titleText);
setSubtitle(subtitleText);
setThumb(thumb);
init();
}
private void setSubtitle(String sub) {
subtitle.setText(subtitleText);
}
private void setThumb(Image image) {
thumb.setImage(image);
}
private void setTitle(String titleText) {
title.setText(titleText);
}
private void init() {
add(thumb, THUMB_COORDINATES_X, THUMB_COORDINATES_Y);
setColumnSpan(thumb, THUMB_SPAN_Y);
add(title, TITLE_COORDINATES_X, TITLE_COORDINATES_Y);
add(subTitle, SUBTITLE_COORDINATES_X, SUBTITLE_COORDINATES_Y);
}
}Code Snippets
private void setSubtitle(String sub) {
subtitle.setText(sub);
}
private void setThumb(Image img) {
thumb.setImage(img);
}
private void setTitle(String titleString) {
title.setText(titleString);
}private void init() {
add(thumb, 0, 0, 1, 2);
add(title, 1, 0);
add(subTitle, 1, 1);
}private static final int THUMB_COORDINATES_X = 0;
private static final int THUMB_COORDINATES_Y = 0;
private static final int THUMB_SPAN_Y = 2;
private static final int TITLE_COORDINATES_X = 1;
private static final int TITLE_COORDINATES_Y = 0;
private static final int SUBTITLE_COORDINATES_X = 1;
private static final int SUBTITLE_COORDINATES_Y = 1;private void init() {
add(thumb, THUMB_COORDINATES_X, THUMB_COORDINATES_Y);
setColumnSpan(thumb, THUMB_SPAN_Y);
add(title, TITLE_COORDINATES_X, TITLE_COORDINATES_Y);
add(subTitle, SUBTITLE_COORDINATES_X, SUBTITLE_COORDINATES_Y);
}package Components.CustomLabel;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
/**
*
* @author Aamir khan
*/
public class LabelWithSubtitle extends GridPane {
private final Label title = new Label();
private final Label subtitle = new Label();
private final ImageView thumb = new ImageView();
public LabelWithSubtitle(String titleText, String subtitleText, Image thumb) {
setTitle(titleText);
setSubtitle(subtitleText);
setThumb(thumb);
init();
}
private void setSubtitle(String sub) {
subtitle.setText(subtitleText);
}
private void setThumb(Image image) {
thumb.setImage(image);
}
private void setTitle(String titleText) {
title.setText(titleText);
}
private void init() {
add(thumb, THUMB_COORDINATES_X, THUMB_COORDINATES_Y);
setColumnSpan(thumb, THUMB_SPAN_Y);
add(title, TITLE_COORDINATES_X, TITLE_COORDINATES_Y);
add(subTitle, SUBTITLE_COORDINATES_X, SUBTITLE_COORDINATES_Y);
}
}Context
StackExchange Code Review Q#108887, answer score: 3
Revisions (0)
No revisions yet.