aspose file tools*
The moose likes Beginning Java and the fly likes Adding JLabels to a vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding JLabels to a vector" Watch "Adding JLabels to a vector" New topic
Author

Adding JLabels to a vector

Ross McManus
Greenhorn

Joined: Jul 28, 2010
Posts: 28
I am trying to add JLabels to a vector.

I have an HotelGUI class



Say if I try and add



Netbeans give me this error on the rooms.add(first); part


How do I add JLabels to a vector correctly?

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
This may be less of a Swing issue and more of a basic Java issue. Are you trying to make method calls outside of a constructor, method or static block? It would help if you posted more of the offending code along with the complete error message.
Ross McManus
Greenhorn

Joined: Jul 28, 2010
Posts: 28
Here is my Code for the Room, Bedroom and MeetingRoom classes.







The compiler error in Netbeans is

/Users/Ross/Documents/Java Programming/Assessments/Re-sit Assignment/src/HotelGUI.java:137: <identifier> expected
rooms.add(first);
/Users/Ross/Documents/Java Programming/Assessments/Re-sit Assignment/src/HotelGUI.java:137: <identifier> expected
rooms.add(first);
2 errors


I am creating a Hotel Booking system and on the sheet I have got it said on the GUI part

"The rooms that are created should be added to Vector of Rooms, to be used when processing events."

So I guessed that I would have to add the JLabels to a vector as in the GUI part it also said "Each JLabel represents a room"




pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
You need to post the class with the Vector code in it, the class that is giving you the error. Perhaps it's my mistake, but I don't see it. Also, you have a JPanel named rooms, and is this perchance clashing with the Vector variable?
Ross McManus
Greenhorn

Joined: Jul 28, 2010
Posts: 28
pete stein wrote:You need to post the class with the Vector code in it, the class that is giving you the error. Perhaps it's my mistake, but I don't see it. Also, you have a JPanel named rooms, and is this perchance clashing with the Vector variable?


Sorry about that the code that has the Vector in it is the GUI class I just took it out to post this and posted the vector code below it.

Here is the GUI class with the vector in.

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Ross McManus wrote:
Sorry about that the code that has the Vector in it is the GUI class I just took it out to post this and posted the vector code below it.


Your problem is as I suspected in my first post: You're placing code in the middle of a class, not in any method, constructor or similar construct, and this simply won't work. Again, this is not a Swing issue but a basic Java issue. The solution: don't do this; instead place code that needs to be in a method or constructor within a method or constructor.
Ross McManus
Greenhorn

Joined: Jul 28, 2010
Posts: 28
Thanks for the reply I have done what you said and got it compiling now.

I am just not sure if it is adding the JLabels to a vector or am I just adding random numbers and text?

I am not 100% sure how to do this.

Also I have MeetingRooms so how would I add them instead of it being Bedroom would I need to create a new Vector for MeetingRooms?

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Ross McManus wrote:Thanks for the reply I have done what you said and got it compiling now.

Great.


I am just not sure if it is adding the JLabels to a vector or am I just adding random numbers and text?

I am not 100% sure how to do this.

If there's any doubt, after adding components to the Vector you could do a System.out.println on the Vector to see what it contains.


Also I have MeetingRooms so how would I add them instead of it being Bedroom would I need to create a new Vector for MeetingRooms?

OK, now that we've got the compile issue out of the way, let's consider logic. Can you tell us why you want to add JLabels to a Vector? Usually when I do something of this sort, I add a data object, such as in this example a Room object, to the Vector (or an ArrayList which I prefer to use), not a GUI component. Then the GUI uses this Vector or List to help build the GUI components. Can you tell us more of your design?
Ross McManus
Greenhorn

Joined: Jul 28, 2010
Posts: 28
pete stein wrote:
Ross McManus wrote:Thanks for the reply I have done what you said and got it compiling now.


OK, now that we've got the compile issue out of the way, let's consider logic. Can you tell us why you want to add JLabels to a Vector? Usually when I do something of this sort, I add a data object, such as in this example a Room object, to the Vector (or an ArrayList which I prefer to use), not a GUI component. Then the GUI uses this Vector or List to help build the GUI components. Can you tell us more of your design?


I want to add the JLabels to a Vector because in the Assignment sheet I have on the GUI section it states

"The rooms that are created should be added to Vector of Rooms, to be used when processing events."

I thought that it would be the JLabels that I had to add to the vector because before that part it tells me that

"Hotel GUI is to have internal panels holding 5 JButtons and JLabels. Each JLabel represents a room and is to display text giving the room number and type of room(single, double or meeting room)

So that is where I thought I would have to add a Vector of JLabels.

Later on for the Mouse Events section it tells me

"If the source of a button event is 'BOOK SINGLE ROOM' or 'BOOK DOUBLE ROOM' the Vector of Rooms should be searched over the range for bedrooms, to ensure that there is a vacant room of the requested type: otherwise a suitable message is displayed stating there are no rooms available"

Or do I have to create Room objects for each room and thats what needs to be in the vector?



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

As Pete already mentioned, this is a general Java problem, not a Swing problem. I'm going to move this to Beginning Java.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Ross McManus wrote:
I want to add the JLabels to a Vector because in the Assignment sheet I have on the GUI section it states

"The rooms that are created should be added to Vector of Rooms, to be used when processing events."

I thought that it would be the JLabels that I had to add to the vector because before that part it tells me that

"Hotel GUI is to have internal panels holding 5 JButtons and JLabels. Each JLabel represents a room and is to display text giving the room number and type of room(single, double or meeting room)

So that is where I thought I would have to add a Vector of JLabels.

Later on for the Mouse Events section it tells me

"If the source of a button event is 'BOOK SINGLE ROOM' or 'BOOK DOUBLE ROOM' the Vector of Rooms should be searched over the range for bedrooms, to ensure that there is a vacant room of the requested type: otherwise a suitable message is displayed stating there are no rooms available"

Or do I have to create Room objects for each room and thats what needs to be in the vector?

Does it specifically tell you to call one class "Room"? If so, then your instructions are clear: you need a Vector of Room objects, or Vector<Room>, and this makes sense to me since all your different types of Rooms inherit from Room and thus can easily be placed into this Vector. To me the JLabel is simply the visual representation of the Room and shouldn't be entered into the Vector.
Ross McManus
Greenhorn

Joined: Jul 28, 2010
Posts: 28
Thanks for the reply.

Yeah I started to think about the JLabels vector and started to think maybe it was wrong.

I'm trying to understand where I would create the Room objects.

Would it be in either the

Room constructor



Or the Bedroom constructor




Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, the first thing to know about a constructor for class X is that its purpose is to construct an object of class X.

So knowing that, what is the purpose of a constructor of the Room class?

And having answered that, do you still think that inside a Room constructor is a good place to create a list of Room objects?
Ross McManus
Greenhorn

Joined: Jul 28, 2010
Posts: 28
Therefore the purpose of the constructor in the Room class is to create an object of Room. So yeah a good place to create Room objects

But what I don't get is

I have bedroom's with a certain rate & type (eg. single or double)

Then I have MeetingRooms with a certain rate & capacity

So I would create the Bedroom objects in the Bedroom constructor and then the MeetingRoom's in the MeetingRoom constructor.

But that leaves the Room constructor still empty.
Ross McManus
Greenhorn

Joined: Jul 28, 2010
Posts: 28
Actually looking at my classes again.

The MeetingRoom class does not have another constructor to create a Room object in.



I am confused. Do I just create all the Room objects in the Room() constructor the Bedroom's & MeetingRooms.

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Ross McManus wrote:Actually looking at my classes again.

The MeetingRoom class does not have another constructor to create a Room object in.

It doesn't need one as it is a Room type.


I am confused. Do I just create all the Room objects in the Room() constructor the Bedroom's & MeetingRooms.


No, the Room constructor does nothing but create a Room object that's it. The Room, MeetingRoom and WhatNotRoom classes are all Room types and are used by other classes that need to use Rooms or display Room information such as your GUI.
Ross McManus
Greenhorn

Joined: Jul 28, 2010
Posts: 28
Thanks for the reply.

I still don't get where I create the Room objects though that are going to be added to the vector?

Because I have Rooms numbered 201 to 203 which are single bedrooms

Then rooms numbered 204 to 212 which are double bedrooms

Then three MeetingRooms numbered 101 to 103
Ross McManus
Greenhorn

Joined: Jul 28, 2010
Posts: 28
Okay I think I get it now.

I create all the Room objects and the Bedroom's & Meetingroom's are just types of Room.

So I think this is right what I have done. It compiles.


pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Ross McManus wrote:Okay I think I get it now.

I create all the Room objects and the Bedroom's & Meetingroom's are just types of Room.

Yes, that is correct.

So I think this is right what I have done. It compiles.

"It compiles" is not a test of correct logic, just a test of compilability.




Don't do this. Again the Room class should just concern itself with the behavior and state of a single Room object, nothing more and nothing less, and the Room constructor should just concern itself with creating a single Room object, nothing more and nothing less. Again, it will be another class entirely, perhaps the GUI, perhaps the GUI's model that will create and store multiple Room objects.
Ross McManus
Greenhorn

Joined: Jul 28, 2010
Posts: 28
Oh I get what you mean about the Room constructor.

So I guess they will have to be created in the GUI class as I can't create another class as it does not tell me on the Assignment sheet I need another class.

I guess the code would go before I create the JPanel right?




pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
The Vector should be created and filled before you make your JLabels (I think -- you might want to post the entire assignment), and then you could use a for loop to loop through the Vector, from i = 0 to i < myVector.size() and inside the loop create your JLabels and add them to the GUI.
Ross McManus
Greenhorn

Joined: Jul 28, 2010
Posts: 28
So would I need to create the Room objects etc Room = room201 new Room();

At the beginning of the GUI class? Then the vector underneath then obviously the rest.

I have uploaded the assignment pdf to a filehosting site here http://www.mediafire.com/?g6iihien506hccx as I did not know how else to post the assingment



pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Ross McManus wrote:So would I need to create the Room objects etc Room = room201 new Room();

At the beginning of the GUI class? Then the vector underneath then obviously the rest.

I have uploaded the assignment pdf to a filehosting site here http://www.mediafire.com/?g6iihien506hccx as I did not know how else to post the assingment


Create the Vector<Room> first, and this variable will have to be declared in the class, not in the Constructor (although it may be initialized in the constructor). Then create rooms and add to the Vector as you create. You will need to be able to access this Vector later when the user pushes buttons or if the user wishes to load another list of Rooms.
Ross McManus
Greenhorn

Joined: Jul 28, 2010
Posts: 28
Thanks for the reply.

So it would be something like






pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Ross McManus wrote:
Thanks for the reply.

So it would be something like






No, as you're declaring your Vector inside of an initializer block and it will only be visible inside of the initializer block. Again, declare the Vector in the class itself. You really shouldn't be using initializer blocks for this program anyway.

Better:




Also, please read up on Java naming conventions. Your hotel variable should start with a lower case letter. If you want others to understand your code more easily (including us and especially our teacher), you'll want to follow these conventions.

Good luck.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding JLabels to a vector