Hi, I'm stuck again in the little program I'm developing...
Having a bit of trouble in displaying information in a JLabel by using
array's. I work with 2 seperate classes: GeneralInfo.java and
GIHelpClass.java
What I'm trying to do is the following: I defined 10 JLabels in class
GenerlInfo, here information needs to be displayed, and the SQL query
is defined in GIHelpClass. Problem is that it is not sure all Jlabels
are used, depending on the search results of the SQL query. So I had
to put the JLabels in an array.
My problem is displaying the info in the GeneralInfo class.Here is the
coding I'm using:
In GeneralJava.java:
//"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 =new JLabel();
}
/**"Allocating the first label as the first element of the array
* Only displayed for 2 labels here, all 10 done"
*/
RowTlxImp[0]= lblTlxImp1;
RowTlxImp[0]= lblTlxImp2;
...
//"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];
JLabel[] HelpRowTlxImp = new JLabel[max12];
for (int i = 0; i < max12; i++) {
rs13.next();
RowOfImpTelex = new String(rs13.getString("Telex"));
HelpRowTlxImp.setText(RowOfImpTelex);
}
/** After closing my connection, statement and sqlquerry
* from the ODBC driver, I get the info from the first array
* and place it the other array. But nothing is displayed
* in the labels !! What am I doing wrong?
*/
for (int j = 0; j < max12; j++) {
TlxImp[j].setText(HelpRowTlxImp[j].getText());
}
Can somebody help me in order to display this info? The above code is
not the correct code, but if you don't work with arays, this coding
works!!
Rgds!
Andrew Thompson - 25 Sep 2004 14:54 GMT
> Hi, I'm stuck again in the little program I'm developing...
I tried to look over our code, but the example is a mess.
If you need to post that much code in future, please
check this page on tips for writing code examples..
<http://www.physci.org/codes/sscce.jsp>
The first tips I might point out is that if you are
having problems with arrays, you are having as much
problems with arrays of 2 or 3, as arrays of 10, so
trim the (7-8) unecessary Labels out and create the
shortest self-contained example that displays problem.

Signature
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
Draghkar - 27 Sep 2004 19:02 GMT
Start again, and I'll keep the code in the post limited. Not used to
post in newsgroups, so have to learn a bit on how things are done
here. Thought that giving so much code would help resolve the problem.
Ok, here we again:
What I'm trying to do is displaying data from a database in JLabels.
Problem is that I have 10 labels (created in class GeneralInfo.java),
but the data I need to display can be 1, 5 or 10 elements. That's why
I put the labels in an array:
JLabel[] RowTlxImp = new JLabel[10];
...
To do all this I'm using 2 classes: GeneralInfo.java and
GIHelpClass.java. I have a button in GeneralInfo that calls a method
from GIHelpClass.
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
g.SearchTelex(UpCarrier, UpStation, RowTlxImp);
...
I'm trying to display the data by defining a method SearchTelex in
GIHelpClass (max12 is the total of data elements I have):
public void SearchTelex(String StgTheCarrier2, String StgTheStation2,
JLabel[] TlxImp) {
...
for (int i = 0; i < max12; i++) {
rs13.next();
RowOfImpTelex = new String(rs13.getString("Telex"));
HelpRowTlxImp.setText(RowOfImpTelex);
}
rs13.close;
...
for (int j = 0; j < max12; j++) {
TlxImp[j].setText(HelpRowTlxImp[j].getText());
}
These last lines of coding are not displaying any data in the Labels.
So at the moment I'm not able to display anything in these Labels.
What am I doing wrong? Should I define a constructor?
Hope this post is more structured, and really focussing on the problem
I have.
Rgds,
Draghkar
>> Hi, I'm stuck again in the little program I'm developing...
>
[quoted text clipped - 9 lines]
>trim the (7-8) unecessary Labels out and create the
>shortest self-contained example that displays problem.
Andrew Thompson - 28 Sep 2004 03:10 GMT
Please do not top-post Draghkar, I find it most confusing.
<http://www.physci.org/codes/javafaq.jsp#netiquette>
See further comments below.
>>> Hi, I'm stuck again in the little program I'm developing...
>>
>>I tried to look over our code, but the example is a mess.
..
>><http://www.physci.org/codes/sscce.jsp>
> Start again, and I'll keep the code in the post limited.
Limited to what? Code snippets?
You either did not read the link to which I pointed you, or
completely failed to understand it. As the author of the
document, I am curious to know which is the case.
Did you read the link?

Signature
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane