define multiple labels in javafx at once - java

I would like to declare multiple labels in javafx all at once, is this possible?
at the moment this is what i type, and i have line 20 to 30 labels!!
fajr_Label_ar = new Label();
fajr_Label_eng = new Label();
zuhr_Label_ar = new Label();
zuhr_Label_eng = new Label();
asr_Label_ar = new Label();
asr_Label_eng = new Label();
maghrib_Label_ar = new Label();
maghrib_Label_eng = new Label();
isha_Label_ar = new Label();
isha_Label_eng = new Label();
hadith_Label = new Label();
fajr_hourLeft = new Label();
Can I declare something along the line of;
label1, label2, label3.... = new Label();

This is not about JavaFX. This question is about Java in general ..
I see some localizations there : en,ar ... so I would suggest this pattern:
Label lbs[];
lbs=new Label[7];
String strings_en[]=new String[]{
"fajr",
"zuhr",
"asr",
"maghrib",
"isha",
"hadith",
"fajr_lefthour"
};
String strings_ar[]=new String[]{
"فجر",
"ظهر",
"عصر",
"مغرب",
"عشاء",
"حديث",
"فجر ساعة لاحقة"
};
String strings[];
//assing lan list
strings=strings_ar; // or strings=strings_en;
for (int i = 0; i < lbs.length; i++) {
lbs[i]=new Label(strings[i]);//initializing labels
//doing what you want here with labels
//...
}
It would be better to isolate data from design as you can see we have labels as Label array and data as String array.. and it would be better and better to define the lang set before retrieving data as we have done when assign object array strings to object array Ar or en .. that would help you to extend/scale/debug your program easily and healthy by adding more labels or languages ..

Related

Eclipse SWT Text Boxes change size

I had an Eclipse plug-in app (ten-year-old code, no documentation, etc.) dropped in my lap and while adding new features to it, I noticed that when a panel is resized, the text boxes change size continuously while the separator is being dragged.
As you can see in the second picture, the text boxes are kind of randomly sized. Is there a setting in SWT that will prevent this from happening?
Here's how I'm creating one of the text boxes. The others are basically clones of this:
Font font = parent.getFont();
setLayout(new FillLayout());
SashForm sashForm = new SashForm(this, SWT.VERTICAL);
FormToolkit toolkit = new FormToolkit(getParent().getDisplay());
Section section = toolkit.createSection(sashForm,
Section.DESCRIPTION | ExpandableComposite.TITLE_BAR | ExpandableComposite.EXPANDED);
section.setLayoutData(new GridData(GridData.FILL_BOTH));
section.setLayout(new GridLayout());
section.setText("Section Title");
Composite controlComposite = toolkit.createComposite(section);
GridLayout controlLayout = new GridLayout();
controlLayout.numColumns = 2;
controlLayout.verticalSpacing = 20;
controlComposite.setLayout(controlLayout);
section.setClient(controlComposite);
Font bold = ResourceManager.getBoldFont(font);
Label textLabel = toolkit.createLabel(controlComposite, "Title:", SWT.BOLD);
GridData gd = new GridData();
gd.horizontalSpan = 1;
textLabel.setLayoutData(gd);
textLabel.setFont(bold);
textBox = new ExtendedText(controlComposite, SWT.BORDER | SWT.SINGLE, false);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 1;
gd.verticalSpan = 2;
textBox.setLayoutData(gd);
The ExtendedText class is an extension of StyledText. The important bits of it are this:
GridData gd_bg = new GridData(GridData.FILL_BOTH);
setLayoutData(gd_bg);
final GridLayout gridLayout = new GridLayout();
gridLayout.verticalSpacing = 0;
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.horizontalSpacing = 0;
gridLayout.numColumns = 1;
gridLayout.makeColumnsEqualWidth = true;
sashForm.setWeights(new int[] { 1, 1 });
Okay, after digging in a little deeper, I got it working as expected.
First, the controlComposite and controlLayout objects are now created using
Composite controlComposite = new Composite(section, SWT.NONE)
controlComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
controlComposite.setBackground(section.getBackground());
GridLayout controlLayout = new GridLayout(2, true);
controlLayout.marginHeight = 20;
controlLayout.marginWidth = 0;
controlLayout.verticalSpacing = 10;
controlLayout.horizontalSpacing = 0;
controlComposite.setLayout(controlLayout);
section.setClient(controlComposite);
Once I did that, things started to stabilize. I also ended up tweaking the weights to this:
sashForm.setWeights(new int[] { 2, 3 });
It's not perfect, but it'll do for now.
Thanks to #greg-449 and #Baz for taking a look

How to display a text file in vaadin

I'm new around here even though I am have been looking at these forums for ages and I finally need a bit of help;
I have tried this;
FileResource file = new FileResource(new File("/a/d/r/e/s/s/file"));
TextArea text = new TextArea();
text.setValue(file);
This;
FileResource file = new FileResource(new File("/a/d/r/e/s/s/file"));
TextArea text = new TextArea();
text.setValue(file.toString());
And;
FileResource file = new FileResource(new File("/a/d/r/e/s/s/file"));
TextArea text = new TextArea();
text.setValue(file.getAbosoluteFile().toString());
And others which are to big to show;
How do I show the file
The easiest way would be to use the TextFileProperty:
TextArea text = new TextArea(new TextFileProperty(new File("/a/d/r/e/s/s/file")));
or the longer form:
TextArea text = new TextArea();
text.setPropertyDataSource(new TextFileProperty(new File("/a/d/r/e/s/s/file")));
What this piece of code does is it binds your Field TextArea to a Property. This is Vaadin's data binding mechanism. Property and Field synchronize each other automatically.
If you just want to display the file without editing it, consider using a Vaadin Label instead of the TextArea.
final TextArea textField = new TextArea();
textField.setSizeFull();
this.addComponent(textField);
try {
final File file = new File("/path/to/file");
final String fileAsString = FileUtils.readFileToString(file);
textField.setValue(fileAsString);
} catch (IOException e) {
e.printStackTrace();
}
You will need to have the IO component from Apache Commons available to be able to import FileUtils
Using the java standard library + java 8 streams
TextArea text = new TextArea();
String value = Files.readAllLines(Paths.get(file)).stream().collect(Collectors.joining())
text.setValue(value);
In Java 7 you can use the readAllLines function iterating over the List generated, in case that the size of the file is really big,follow a different approach, as the one that is explained here http://www.baeldung.com/java-read-lines-large-file

Grid Layout does not display correctly

I'm trying to build simple calculator gui with display and 9 buttons
public void init()
{
setSize(60,80);
inf = new InfoButton(this);
zero = new CalcButton(this,"0");
one = new CalcButton(this,"1");
add = new CalcButton(this,"+");
sub = new CalcButton(this,"-");
div = new CalcButton(this,"/");
mlt = new CalcButton(this,"*");
modu = new CalcButton(this,"%");
blank = new JButton("");
wys = new Wyswietlacz(); // its JTextPane
wys.setSize(60,20);
przyciski = new JPanel();
przyciski.setSize(60,60);
przyciski.setLayout(new GridLayout(3,3));
przyciski.add(zero);
przyciski.add(one);
przyciski.add(add);
przyciski.add(sub);
przyciski.add(mlt);
przyciski.add(div);
przyciski.add(modu);
przyciski.add(inf);
przyciski.add(blank);
calosc = new JPanel();
calosc.setLayout(new BoxLayout(calosc,BoxLayout.Y_AXIS));
calosc.add(wys);
calosc.add(przyciski);
calosc.setSize(60,80);
add(calosc);
}
and in main i make frame with size (60,80) but when i make it visible all i can see is display and one row of buttons. What am i doing wrong?
Call setPreferredSize(..) instead of setSize() on wys and przyciski. Then use JFrame's pack() instead of specifying a size for it.

Text area doesn't seem to be working

I am try to create a text area that will display a list of scores. For some reason though, the text area only expands when someone types in it, and the user should not even be allowed to type in it. I thought i wrote in the code for it properly but for some reason it doesn't seem to work. The "hello" phrase I appended isn't even displaying in the text area. Can anyone provide some advice:
public HighScores() throws FileNotFoundException, IOException{
frame.setVisible(true);
frame.setSize(400,200);
frame.add(main);
GridBagConstraints g = new GridBagConstraints();
g.insets = new Insets(10,10,10,10);
g.gridx = 0;
g.gridy = 0;
main.add(highscorespanel, g);
highscorespanel.add(highscores);
g.gridx = 0;
g.gridy = 1;
main.add(textareapanel, g);
Color c = textareapanel.getBackground();
textareapanel.setBackground(c);
textareapanel.add(ta);
ta = new JTextArea ();
ta.setVisible(true);
ta.setEnabled(true);
ta.setEditable(false);
ta.append("hello");
JScrollPane sp = new JScrollPane(ta);
BufferedReader br = new BufferedReader(new FileReader("src/BattleShip/scores.txt"));
String namescore = br.readLine();
while(namescore!=null){
ta.append("\t"+namescore);
}
Im almost sure you don't need the answer anymore but you need to move your line "textareapanel.add(ta);" to some point after you initialize ta.
ta = new JTextArea();
textareapanel.add(ta);
ta.setVisible(true);
ta.setEnabled(true);
ta.setEditable(false);
EDIT:
At a second look you want a JScrollPane for your JTextArea so you code should be like this:
JTextArea ta = new JTextArea();
ta.setVisible(true);
ta.setEnabled(true);
ta.setEditable(false);
JScrollPane sp = new JScrollPane(ta);
textareapanel.add(sp);

how to use zk hbox array?

I am not sure how to use a zk Hbox Array. I am trying to create an array of ZK Hbox components and use it within a for block.
void createRow(Component container, final Node fieldNode, FieldCreator [] fieldDescription) {
final Vbox fieldsRows = new Vbox();
final Hbox fieldsRow = new Hbox();
final Hbox[] fieldBox;
int i=0;
for (FieldCreator fieldDesc : fieldDescription) {
fieldBox[i] = new Hbox();
fieldDesc.createField(fieldNode, fieldBox[i]);
i++;
}
fieldsRow.appendChild(fieldBox[]);
Button removeFieldButton = new Button(Messages.getString("MXFC_removeFieldButton")); //$NON-NLS-1$
fieldsRow.appendChild(removeFieldButton);
fieldsRows.appendChild(fieldsRow);
removeFieldButton.addEventListener(Events.ON_CLICK, new EventListener() {
public void onEvent(Event event) throws Exception {
fieldNode.getParentNode().removeChild(fieldNode);
fieldBox[].setParent(null);
}
});
container.appendChild(fieldsRows);
}
The code above is incorrect. The compiler throws the error: "Syntax error on token "[", Expression expected after this token." on lines :
fieldsRow.appendChild(fieldBox[]);
fieldBox[].setParent(null);
How do I fix this?
Thanks,
Sony
Sony,
There are a few syntax errors in your java code.
fieldBox[] does not mean anything here in Java.
You need to initialize fieldBox before you can assign value to its entries.
To fix these problems we have to understand what you want to achieve in this piece of code. Based on my guess, you should
initialize fieldBox.
Hbox[] fieldBox = new Hbox[fieldDescription.length];
iterate through columns as you append/detach children of the row.
for(int i=0; i<fieldBox.length; i++) fieldsRow.appendChild(fieldBox[i]);
for(int i=0; i<fieldBox.length; i++) fieldBox[i].setParent(null);

Categories

Resources