Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2004

Tip: Looking for answers? Try searching our database.

Help needed displaying info in labels using arrays

Thread view: 
Draghkar - 25 Sep 2004 14:36 GMT
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!
Paul Lutus - 25 Sep 2004 16:45 GMT
> 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.

Maybe you could say what that "bit of trouble" is.

> I work with 2 seperate classes: GeneralInfo.java and
> GIHelpClass.java
[quoted text clipped - 3 lines]
> are used, depending on the search results of the SQL query. So I had
> to put the JLabels in an array.

Can you please say what you wanted, what you got, and how they differ?

1. I wanted this ________________________________________________
2. But, darn it, I got this instead _____________________________
3. Here is how (1) and (2) differ _______________________________

> My problem is displaying the info in the GeneralInfo class.

No, that is not your problem. You don't say what your problem is, or your
goal.

Signature

Paul Lutus
http://www.arachnoid.com

Draghkar - 27 Sep 2004 19:07 GMT
I'l 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 the
class GIHelpClass.java (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.

So:

>>1. I want data to be displayed in JLabels
>>2. But, darn it, I got this instead: nothing :-)
>>3. Here is how (1) and (2) differ: see above...

Rgds,

Draghkar

>> Hi, I'm stuck again in the little program I'm developing...
>>
[quoted text clipped - 21 lines]
>No, that is not your problem. You don't say what your problem is, or your
>goal.
Paul Lutus - 27 Sep 2004 19:43 GMT
> I'l 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
[quoted text clipped - 8 lines]
>
> JLabel[] RowTlxImp = new JLabel[10];

Bad idea. Use a container, a structure able to contain any number of
elements, but one that only contains as many as you need.

To receive help, post a complete, short, compilable, working code exmaple
that shows the problem.

Signature

Paul Lutus
http://www.arachnoid.com

Jacob - 25 Sep 2004 17:45 GMT
>        RowTlxImp[0]= lblTlxImp1;
>        RowTlxImp[0]= lblTlxImp2;
>        ...

This piece of code is wrong. You don't mean to allocate them all to
index 0?

> JLabel lblTlxImp1 = new JLabel("");
> JLabel lblTlxImp2 = new JLabel("");
[quoted text clipped - 6 lines]
> JLabel lblTlxImp9 = new JLabel("");
> JLabel lblTlxImp10 = new JLabel("");

You don't need this piece of code.

Replace it by:

   int n = 10;
   JLabel[] RowTlxImp = new JLabel[n];
   for (int i = 0; i < n; i++)
      RowTlxImp[i] = new JLabel("");

In general: Work with your naming and code structure.
In particular, variable names should start with lower case letters
and have _meningful_ names and should never be abbreviated.
For more hints see http://geosoft.no/javastyle.html


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.