• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble with subpanels

 
Roonie Collins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The color is not changing and it is driving me nuts. Please show why this isn't working properly.

Thanks! ( :
 
Paul Clapham
Sheriff
Posts: 21865
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roonie, welcome to the Ranch!

There's a lot of code there and after scanning it a couple of times I don't see anything which is supposed to change any colours. All I see is code which sets up a GUI. But maybe I didn't look carefully enough (there's a lot of code there) -- could you point out the part of the code which you think should be changing the colour of something?
 
Roonie Collins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Hi Roonie, welcome to the Ranch!

There's a lot of code there and after scanning it a couple of times I don't see anything which is supposed to change any colours. All I see is code which sets up a GUI. But maybe I didn't look carefully enough (there's a lot of code there) -- could you point out the part of the code which you think should be changing the colour of something?


Hi Paul, thanks for taking a look!

I have a button subpanel and when the button is pushed it creates an action. The ActionEvent takes the arguments from the JTextFields and uses the values to setBackground to the color.

Here is the button subpanel and ActionEvent...



I think the GridLayout is preventing the background from being changed. Thanks again Paul!
 
Paul Clapham
Sheriff
Posts: 21865
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roonie Collins wrote:I think the GridLayout is preventing the background from being changed. Thanks again Paul!


That's unlikely, since you aren't using a GridLayout anywhere in the code you posted. (The word "GridLayout" does appear in a comment, but that won't affect anything.) Have you debugged your code to see if the ActionListener code is actually being executed and that it gets to the line of code which is supposed to change the background colour? What colour is it trying to use?
 
Roonie Collins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Roonie Collins wrote:I think the GridLayout is preventing the background from being changed. Thanks again Paul!


That's unlikely, since you aren't using a GridLayout anywhere in the code you posted. (The word "GridLayout" does appear in a comment, but that won't affect anything.) Have you debugged your code to see if the ActionListener code is actually being executed and that it gets to the line of code which is supposed to change the background colour? What colour is it trying to use?


I am sorry, I was referring to the BorderLayout not GridLayout... I think if I could access the labelPanel from the ActionListener I could change the color. The action listener is try to change the background to the RGB integer values that are in the JTextFields. I haven't used the debugger because I am not very familiar with NetBeans but I will give it a try.
 
Paul Clapham
Sheriff
Posts: 21865
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I see, the way your code is written, it isn't actually changing the background of the panel you want to change.

So yeah, your diagnosis is correct, the way you've designed your code you've hidden things away in methods which shouldn't be hidden away. One way to fix the problem would be to flatten the code so that all creation of GUI components takes place in a single method. Or you could pass a reference to the panel you want changed to the Color2PlayPanel, probably in a constructor parameter.

As for debugging, if you don't want to get into Netbeans debugging right now then you can just insert "System.out.println" statements which print out information about what's happening in your code.
 
Roonie Collins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Oh I see, the way your code is written, it isn't actually changing the background of the panel you want to change.

So yeah, your diagnosis is correct, the way you've designed your code you've hidden things away in methods which shouldn't be hidden away. One way to fix the problem would be to flatten the code so that all creation of GUI components takes place in a single method. Or you could pass a reference to the panel you want changed to the Color2PlayPanel, probably in a constructor parameter.

As for debugging, if you don't want to get into Netbeans debugging right now then you can just insert "System.out.println" statements which print out information about what's happening in your code.


I am not sure of how to reference the labelPanel, which is the CENTER of the BorderLayout. Would I assign it in the ActionEvent constructor? I think my instructor wants us to make separate methods to create subpanels versus one method for all of the panels. Thanks again, Paul.
 
Paul Clapham
Sheriff
Posts: 21865
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of a ColorPlay2Panel object as a tool for displaying some colour selection buttons and also for changing the text of an unrelated button to reflect that selection. That really isn't the best of object-oriented designs, but then it's traditional when learning Swing to stumble through without understanding OOD. So we have to go with that design, apparently.

Normally what you'd do is to allow a ColorPlay2Panel to have listeners, so that when its colour choice is changed it would notify its listeners. Since it's a JPanel it already supports that, i.e. you can call addPropertyChangeListener on it to become a listener, and it can call firePropertyChange to notify its listeners of changes to properties such as "Color Choice".

Or alternatively for this project you could just design it to contain a reference to another component, which it will modify when the colour choice is changed. That isn't as good from the design point of view, because now you've hard-coded what happens to that other component when a new colour is chosen. With the listener method, the listener gets to decide what to do to its component when a new colour is chosen. (You aren't learning object-oriented design yet but being exposed to arguments like this will help when you do start doing that.) Anyway for the ColorPlay2Panel to have a reference to another component, in this case the best way to implement that way would be to pass a reference when constructing the ColorPlay2Panel object. Which would mean your constructor would need a parameter for that.
 
Roonie Collins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Think of a ColorPlay2Panel object as a tool for displaying some colour selection buttons and also for changing the text of an unrelated button to reflect that selection. That really isn't the best of object-oriented designs, but then it's traditional when learning Swing to stumble through without understanding OOD. So we have to go with that design, apparently.

Normally what you'd do is to allow a ColorPlay2Panel to have listeners, so that when its colour choice is changed it would notify its listeners. Since it's a JPanel it already supports that, i.e. you can call addPropertyChangeListener on it to become a listener, and it can call firePropertyChange to notify its listeners of changes to properties such as "Color Choice".

Or alternatively for this project you could just design it to contain a reference to another component, which it will modify when the colour choice is changed. That isn't as good from the design point of view, because now you've hard-coded what happens to that other component when a new colour is chosen. With the listener method, the listener gets to decide what to do to its component when a new colour is chosen. (You aren't learning object-oriented design yet but being exposed to arguments like this will help when you do start doing that.) Anyway for the ColorPlay2Panel to have a reference to another component, in this case the best way to implement that way would be to pass a reference when constructing the ColorPlay2Panel object. Which would mean your constructor would need a parameter for that.


I am still very new to Java so I don't completely understand what you are trying to explain to me without an example, which is frustrating. I feel like I am almost there....

For example I got the GUI button to change colors like this....





I would really like to get namePanel, buttonPanel and messgePanel to change colors but they wont. I don't know how to rename those panels in a manner that I can use...

Another example of what I am trying to do is this...

JButton clickedButton = (JButton)event.getSource();

namePanel.setBackground(
new Color(
enteredColorValues[0],
enteredColorValues[1],
enteredColorValues[2]));
 
Paul Clapham
Sheriff
Posts: 21865
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roonie Collins wrote:I am still very new to Java...


That's unfortunate, I have to say. People who are new to Java shouldn't be made to write Swing applications, because non-trivial Swing applications require a certain level of skill. As you're finding now. I'm sorry that I can't do anything about the way you're being taught, because now you have to try to get those skills by asking questions on forums. Which is far from being the best way to learn skills.
 
Roonie Collins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Roonie Collins wrote:I am still very new to Java...


That's unfortunate, I have to say. People who are new to Java shouldn't be made to write Swing applications, because non-trivial Swing applications require a certain level of skill. As you're finding now. I'm sorry that I can't do anything about the way you're being taught, because now you have to try to get those skills by asking questions on forums. Which is far from being the best way to learn skills.


Yeah, it's beyond stressful and depressing. I think it's time to give up. I have taken out massive loans to scour the internet for a solution that is not there.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!