I'm trying to put 10 JLabels in an array, but I always receive an
error on following code (defining the first element of the array):
RowTlxImp[0]= new JLabel(lblTlxImp1);
symbol : constructor JLabel (javax.swing.JLabel)
location: class javax.swing.JLabel
RowTlxImp[0]= new JLabel(lblTlxImp1);
^
1 error
Errors compiling GeneralInfo.
Beneath you can find parts of the code I've written
//"Defining the class"
public class GeneralInfo extends JFrame {
...
/**"Defining the button which is necesarry to bring the array of
* labels to another class in order to be able to put the data
* retrieved by an SQL query into these labels. I'm putting
* the labels in an array because the number of data is
* variable, it can be that I need the 10 labels, but it can also
* be that I only need 5 , depending on the result of the SQL
* query"
*/
JButton btnSearch = new JButton("Search");
...
//"Creating constructor"
public GeneralInfo () {
//"Placing the button on the form, I'm using a GridbagLayout manager"
c.gridx = 5;
c.gridy = 3;
theManager.setConstraints(btnSearch, c);
screenOne.add(btnSearch);
/**"Allocatin some functions to the button, as you see I've entered
* the array of the JLabels in this method:
* g.SearchTelex(UpCarrier, UpStation, RowTlxImp);"
*/
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String StgCarrier = (String)cbbCarrier.getSelectedItem();
String StgStation = (String)cbbStation.getSelectedItem();
String UpCarrier = StgCarrier.toUpperCase();
String UpStation = StgStation.toUpperCase();
GIHelpClass g = new GIHelpClass();
g.SearchHandlerData(UpCarrier, UpStation);
g.SearchTelex(UpCarrier, UpStation, RowTlxImp);
setVisible(false);
}
});
//"Defining of the Labels"
JLabel lblTlxImp1 = new JLabel("");
JLabel lblTlxImp2 = new JLabel("");
JLabel lblTlxImp3 = new JLabel("");
JLabel lblTlxImp4 = new JLabel("");
JLabel lblTlxImp5 = new JLabel("");
JLabel lblTlxImp6 = new JLabel("");
JLabel lblTlxImp7 = new JLabel("");
JLabel lblTlxImp8 = new JLabel("");
JLabel lblTlxImp9 = new JLabel("");
JLabel lblTlxImp10 = new JLabel("");
//"Defining the array to put the Labels into"
JLabel[] RowTlxImp = new JLabel[10];
//"Defining of the panel where the labels have to be placed on"
public JPanel createTelexData() {
//"Allocating memory for the array elements"
for (int i = 0; i < RowTlxImp.length; i++) {
RowTlxImp[i] = new JLabel();
}
/**"Allocating the first label as the first element of the array
* Here I receive the error. What's the method to do this?
* Only done for 1 label here, still nine to go"
*/
RowTlxImp[0]= new JLabel(lblTlxImp1);
//"Afterwards I place the Labels on the JPanel createTelexData"
c.gridx = 1;
c.gridy = 2;
theManager.setConstraints(lblTlxImp1, c);
paneZero.add(lblTlxImp1);
/** In another class, "GIHelpClass" I need this array to put the
* data into it:
*/
//"Defning of the class"
public class GIHelpClass {
//"Defining of the method:"
public void SearchTelex(String StgTheCarrier2, String StgTheStation2,
JLabel[] TlxImp) {
/** First I do a count with a SQL query
* to determine the number of elements
* and I've placed it in an int = max12
* Next I define an array as big as the number
* of max12.
* Next Putting the result in each one of the labels
* Will this populate my labels in the General Info class?
*/
String[] RowOfImpTelex = new String[max12];
for (int i = 0; i < max12; i++) {
rs13.next();
RowOfImpTelex[i] = new String(rs13.getString("Telex"));
TlxImp[i].setText(RowOfImpTelex[i]);
}
Can somebody provide me an answer? Tried different things but
I can't find the solution. Is this also a correct set up to do,
working with 2 different classes "GenerlInfo.java" and
"GIHelpClass.java" ???
Well, as a starter in java programming, I still have a lot of
questions...
Rgds, Draghkar
Chris Smith - 17 Sep 2004 17:02 GMT
> I'm trying to put 10 JLabels in an array, but I always receive an
> error on following code (defining the first element of the array):
[quoted text clipped - 4 lines]
> location: class javax.swing.JLabel
> RowTlxImp[0]= new JLabel(lblTlxImp1);
By the time you reach this line of code, you've already created your
JLabel objects (which is what "new JLabel" does). To put them in the
array, you need only say:
RowTlxImp[0]= lblTlxImp1;
Your original code was attempting to create 10 MORE JLabel objects, but
it was not using a valid constructor to do it.
Incidentally, the universally adopted naming conventions for Java
include not capitalizing the names of variables, and avoid the pseudo-
Hungarian notation "warts" that are sometimes used in Microsoft
programming environments. So if you intend to work in Java, you'll want
to rename "RowTlxImp" to "rowTlxImp" instead, and preferably rename
"lblTlxImp" to something like "tlxImpLabel". You might also consider
replacing "tlxImp" with something that's intelligible to the rest of the
world, even if it means typing a few more letters.

Signature
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Draghkar - 17 Sep 2004 18:55 GMT
Tnx mate...
>> I'm trying to put 10 JLabels in an array, but I always receive an
>> error on following code (defining the first element of the array):
[quoted text clipped - 22 lines]
>replacing "tlxImp" with something that's intelligible to the rest of the
>world, even if it means typing a few more letters.