Using variables(int/string) to define frame/object names - java

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.

Related

Java how do i save a random item returned from a string array as a variable?

Im relatively new to Java and cant seem to get a straight answer from my lecturer at college, so I apologise if this is a dumb question. I'm creating a level up screen for my assignment, and it needs to include an array.
I've created a string array called "randomTitle" containing a list of character titles, I need a button to pull a random title from my array list and display it on a JLabel on my level up screen, but am unsure how to save the random title returned as a variable in order to display it, can anyone help me?
Random ran = new Random();
String ary = randomTitle[ran.nextInt(randomTitle.length)];
titleJLabel.setText(String.valueOf(randomTitle));
Random rand = new Random();
int random = (int) (Math.random()*randomTitle.length);
titleJLabel.setText(randomTitle[random]);
So through a slow process of elimination, this ended up solving my issue, I tried setting the JLabel to ary to begin with, which displayed my entire array, useful to know but not what I was aiming for. I've tested the programme over and over now and it seems to be working perfectly fine, Thanks a lot guys!
From what I can tell, you're trying to set the JLabel to a random address in the string array randomTitle. You already have the answer, if that's your question. However, for some reason you seem to be setting the JLabel to String.valueOf(randomTitle). What you want to be doing is setting the JLabel to the random address, which you already have stored in ary. Try setting the JLabel to ary.
Good luck!
Here it is as a one-liner using nextInt
titleJLabel.setText(randomTitle[Math.random().nextInt(randomTitle.length)]);

Assign text to JLabels from ArrayList

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

How to display array length in Eclipse debugger?

When debugging a java program in Eclipse, I can see (e.g. in the Variables view) the content of an arbitrary array, see the picture bellow (with the ByteArrayInputStream.buf field).
But I cannot find the array length field anywhere. Is there a way to show the length of an array in Eclipse debugger? How can I do it?
You can use the "Expressions" view and evaluate the length member:
Keep in mind that the last index is one less than the length!
While this works for public array members, it seems that an explicit cast is required for protected members. Consider the following code:
...
ByteArrayInputStream is = new ByteArrayInputStream(new byte[1769]);
...
Now, when evaluating is.buf, the Expressions view shows a dump of the array as shown in the question, but evaluating is.buf.length fails with <error(s)_during_the_evaluation>. If we add an explicit cast to ByteArrayInputStream, the evaluation works:
Thank you #ThorbjørnRavnAndersen for your answer (comment). You are right, the latest array segment (in my case: [1700..1768]) holds the length.
The whole picture:

use setText() on multiple labels using for loop in Java (Netbeans)

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.

Define an array and pass each point 2 values?

I want to define a grid in which I specify an (x,y) coordinate for each point in the grid. So I want to do something like this:
int [][] pt;
for (x=0; x<numX; x=x+1) {
for (y=0; y<numY; y=y+1) {
pt[x][y] = {xval, yval};
}
}
The reason why is because I am mapping the values of an orderly grid to a disorderly grid. The above code of course causes an exception (unexpected token "{").
What is the best way to do what I'm trying to do? Thanks.
Two things:
You havent initialized your array (maybe you did just didnt put in code)
You are trying to put two values into a place where only one can be held.
Initialize your array like this (if you didnt)
int[][] pt = new int[numX][numY];
To store both values in the array you will need to use an object. The java Point class would be an example of something you could use
Point[][] pt = new Point[numX][numY];
for (x=0; x<numX; x=x+1) {
for (y=0; y<numY; y=y+1) {
pt[x][y] = new Point(xval, yval);;
}
}
You basically want to store a fixed number of values inside every array cell?
Then you are limited with 2 major cases:
Use an object
Java doesn't have user defined value types, so you are forced to use full-blown objects on the heap (with little hope that JVM will be very clever and optimize it, but chances are near zero), be it an array, or any other class.
If both of your values are less than 64 bits, you can pack them in built-in primitive type (such as long) using bitwise arithmetic. (You must be very careful here)
ints are 32 bit, so you can pack 2 ints in 1 long.
pt[x][y] = {xval, yval} is illegal, pt[][] is a double dimensional array. It only can store one value. Just like this pt[x][y] = value
You may try java map.

Categories

Resources