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.
Related
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
I'm not actually sure how to ask this question because its very confusing. I have a java app that has a MVC structure, which gets data from a database. I retrieve String ArrayList of data from a JDBC query. It contains information about Competitors in a race (eg: Name, Race_no, Start_Time, Swim_Time, Bike_Time, Finish_Time etc). The list size will vary week to week depending on the number of the competitors who raced that week. I have no problem getting the data from the database, but when I pass the information to the controller to pass to the view, I am having trouble assigning the data to a JLabel. So far the data is sent as one large array so I need to somehow split the array up in blocks of 12 (which is how many JLabels are required to be set for each competitor). I then set each of those 12 JLabels into its own JPanel ArrayList - then dynmically add to one JPanel for printing. My question is, how do I split the ArrayList to get the first 12, then the second lot of 12, etc.. I have tried doing a nested loop and set the size to 12, but of course that only gets the first 12 everytime. Maybe I need to store the data from the JDBC result set as something else.. I really need some guidance on this as have been stuck for days.
nb: I had this working as one large method in the data handler class, where I would use the while(rs.next()) to do all the work, but because of the MVC structure, I need to break the code up: This is the desired outcome:
EDIT:
I have implement this code which give me the desired output, but now having trouble assigning the JLabel variables with the data in the [j] loop:
<pre>
public void getRaceLabels()
{
ArrayList<String[]> raceLabels = dh.getRaceTimeLabels();
//System.out.println(raceLabels);
for (int i = 0; i < raceLabels.size(); i++)
{
String[] element = (String[]) raceLabels.get(i);
//System.out.println(element);
for (int j = 0; j < element.length; j++)
{
System.out.print(element[j]+" ,");
}
System.out.println("break");
}
</pre>
Create yourself a POJO which represents the basic information you need for a single record.
When loading the data from the database, load this data into the POJO.
Now, for each panel, you just need to pass it a single object, meaning you can now get away with using a simple iterator instead
I have an array of frames and a button that when clicked, will add a new frame to the array.
I'm wondering if I can use the length of the array to determine the name of the new frame to add. For example, if the length of the array is 8, I store this in a variable (for example, int i = length, which is 8).
I then create the frame with i, so
InternalFrame intFrame(i) = new InternalFrame();
intFrameArray.add(intFrame(i));
is using intFrame(i) the correct way to do this? I'm currently not able to test myself as I'm at work but I have had a few problems attempting this last night.
is using intFrame(i) the correct way to do this?
No. You can't. Java won't let you do that. Variable name cannot be dynamic.
I'm trying to make a contact field where you can type in a first name and a last name in two separate text fields and click the "Add" button I created to send it to the list, but I'm unsure of how to do this exactly, being new to jFrame. I was using something in a tutorial that was similar to this using floats (which is shown below), only because I wasn't sure how to use the "String" variation, however this only seems to work when the "setText" command is set on another text field and won't work on a jList.
float num1, num2, result;
num1 = Float.parseFloat(textFieldFirstName.getText());
num2 = Float.parseFloat(textFieldLastName.getText());
result = num1+num2;
listFieldContact.setText(String.valueOf(result));
Are there any ideas or even good resources out there for jFrame? I've looked in a lot of places but they never quite seem to have exactly the information I need.
this only seems to work when the "setText" command is set on another text field and won't work on a jList.
A JList doesn't have a setText(...) method. You need to update the ListModel.
Read the section from the Swing tutorial on How to Use Lists for a working example that does almost exactly what you want.
The example uses a single text field by you should easily be able to get it to work with two text fields.
Try:
String fname = textFieldFirstName.getText();
String lname = textFieldLastName.getText();
listFieldContact = fname + " " + lname;
You don't need float conversion, as MadProgrammer pointed out. You do need a space between first and last name. Maybe you want lname + ", " + fname in other circumstances.
I think to make the values available in the JList it is not necessary to use Float for string operation. We can do it like this :
Vector<String> nameVector = new Vector<>();
JList<String> nameList = new JList<>();
public void addText() {
nameVector.add(firstNameTF.getText()+lastNameTF.getText());
nameList.setListData(nameVector);
}
I think this piece of code will help you to solve your query.
I've got a view that contains documents with various questions I want answered about Purchase Orders.
Using a repeat, I list all the questions. There are a few different kinds of questions, so I only render the answer field that I need based on the FieldType column value. I want to pull the choices for a combobox from the DialogChoices field on the question document.
I'm currently getting the choices showing as plain text on the next line after the empty combobox instead of as the selectItems. Where is my code going wrong?
<xp:comboBox id="comboBox1">
<xp:this.rendered><![CDATA[#{javascript:rowData.getColumnValue("FieldType") == "Dialog Box"; }]]></xp:this.rendered>
<xp:selectItems>
<xp:this.value><![CDATA[#{javascript:var doc:NotesDocument = rowData.getDocument();
var choicesVector:java.util.Vector= doc.getItemValue("DialogChoices");
var choices = [];
// loop through the vector, doing push into the array
for (i=0; i<choicesVector.size(); i++) {
choices.push (choicesVector.elementAt(i))
};
return choices;}]]>
</xp:this.value>
</xp:selectItems>
</xp:comboBox>
Strange, but a test database with the code above does not seem to give me strange results. Maybe it is because the data is in fact not an Vector but just a string?
Here are some tips :
The first thing you could change in your code is the loop to get all the data out of your field. Since the value property of a combobox already expects an array or vector you can change the code to something like:
<xp:this.value><![CDATA[#{javascript:var doc:NotesDocument = rowData.getDocument();
return doc.getItemValue("DialogChoices");
}]]>
</xp:this.value>
But it would be even better to remove the getDocument call at all. If possible you can add a column to the view are you are using for the repeat's datasource. In this column you get the data from the field directory. This way you can use the viewentry's getColumnValue() which is a performance optimization. Something like:
<xp:selectItems>
<xp:this.value><![CDATA[#{try{
return rowData.getColumnValue("DialogChoices");
}catch(e){// do something }]]>
</xp:this.value>
</xp:selectItems>