I am using JLabels and I want to write something like this.
"A" after writing "A" goes to the next line and writes "B".After that writes a variable coming from a method
I can accomplish writing "A" and "B" like that with the following code
JLabel label105 = new JLabel();
label105.setText("<html>Gas Company</html>");
But when I try to insert an integer value from system to set text I fail.Either it writes to the same line or it doesnt work at all.Can anyone write how can I use setText to accomplish this?
Basically what I want is the following in labels.
System.out.print("A");
System.out.println("B");
System.out.println(getValue());
Try this
String string="<html>A<br />B<br />"+Integer.toString(intValue)+"</html>";
label105.setText(string);
The variable intValue is your integer value
Related
this is my click event. The code is supposed to extract entries from text controls and use it to make calculations. At this stage I m trying to make sure that the user enters all of the values that are required, that is: marks for assignments 1, 2, 3 and the exam mark. I realize that I had not entered the "!" to indicate that the controls should not be empty
I believe you wanted to do something like this:
Double assign1;
String stringAssign1 = mark1.getText().toString();
if (!stringAssign1.isEmpty()){
assign1 = Double.parseDouble(stringAssign1);
}
Firstly, you should check if string is empty (also as mentioned before isEmpty() is string function), then if not, parse it to double.
Next time please use codeblock instead of screenshot of code :)
I want to fill 4 arrays with specific data from block string
I got blocks like this
00:0035:0063:1705211023:00:
11::7027661000300976376:
99:59:07027661000300976376:::::
05:11:10000:::00 09:11:8510 07:::::1490:::
99:65:00:00:00:00:00:01000000000002140331410062269000000126300000000
99:64:00:00:00:00:00:00000355600200000000022700000000000000000001
99:01:227:1490:30:0:0:0:0:0:1:0:324
****Segundo Ticket PANGUI**** 99:00:35:63:1705211023:0:1:19353:63895896:1490:0:::::
99:150:0|1|H014|35|63|210517102100|
and I want to check if 00:.. , 05:11:.. , 99:65.. , 99:64... and 99:01...headers exists and stores data for specific field from each row, for example in line or row 99:65.. I will store the last field. If no exists one or more, I must be store zero, something like this
if exist 99:11 then Arr11 =specificfieldfrom9911, else arr11 = "0"
So that for each block have a structure or set of arrays that identifies the fields of each block
Arr00
Arr0511
Arr9965
Arr9964
Arr9901
how can I achieve this? any help would be great.
after you get the individual string you can use startsWith("") method on the strings. Eg. Assuming the string that came is on a variable called inputString you can use
if(inputString.startsWith("99:65")){
//do what you want to do
}
i hope this helps
Hi i'm using a GUI and at the moment printing a list to console. I understand you can print strings with text boxes using textBox.setText("") but instead of printing my list to console I want to use a text box or any other alternative.
At the moment ive got this as my printlist method:
private void printList(){
System.out.println(myList);
}
After attempting to do:
System.out.println(resultsBox.setText(myList));
I realize, this will not work as it only works with strings not lists. So yeah, what else could I use?
Thanks in advance
Do this to convert your list to a String,
if(myList!=null) {
resultsBox.setText(myList.toString());
}
and if there's a valid getText(), you can test the value that you set by printing it,
System.out.println(resultsBox.getText());
You just need to call
resultsBox.setText(myList)
separately.
Notice that this is a method call by itself that sets the values in the box. It does not return a value so you cannot pass it as an argument to println().
I have created multiple labels in design mode and named them as lab_1, lab_2, lab_3 and so on.
Now I want to use setText() on them using a for loop.
for(int i=0; i<16; i++){
String var= "lab_"+i;
var.setText(i);
}
This obviously didn't work. But I'm unable to think of something else.
Is it possible to change the labels into an array of labels now(I haven't created them dynamically instead I created them from the design window.)
Any help?
You want something like this??.
String EMPTY_SPACE="";
JLabel [] jLabels ={lab_1, lab_2, lab_3};
for (int i = 0; i < jLabels.length; i++) {
jLabels[i].setText(i+EMPTY_SPACE);
}
Ignore the loop and focus on these two lines
String var= "lab_"+i;
var.setText(i);
you are trying to call setText on var which is a string. Since your title talk about label and your example about setText, I believe you want to set the text of JLabel using it setText method.
To solve your issue, simply change your variable names.
Note that even if it will probably solve the compiler error (that you did not tell us you had) that you had, your program will probably not work as expected.
If you expect a concatenation of each string in your label, then at each setText call you must retrieve the actual text and concatenate.
the reason im asking this is because for example I have 3 textbox name(variable), text_1,text_2 and text_3. and i want to automatically write something in the textbox depending on which textbox name(variable) i have to write on. Using a loop I need to check if current textbox name is text_1, text_2 or text_3.
if i were to write it in pseudo code it will look like this:
loop:
if(component name == text_1)
text_1.setText = text;
else if(component name == text_2)
.... and so on..
This is what arrays are for. Then you can access them by index with
textFields[0].setText(text);
and doing something to all of them is just a matter of using a loop and an index.