Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using a bean to put slider on panel

 
Candy Bortniker
Ranch Hand
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a bean that creates a JSlider, a textfield, and a label. I want to use the the bean to put these things on a window. I have panel that is a content pane to hold the colored circle. I want another panel to hold the slider. I am able to put the circle on the content pane without a problem but the sliderpanel doesn't seem to appear. Do I need to do something different to the driver program or the bean?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Multiple problems... you can't add your XSliderBeans to the inside of your panel because they extend from JFrame... instead they should just extend JPanel or JComponent. Another problem is that you are creating new XSliderBeans every time the DrawPanel is painted. You should be creating the XSliderBeans once and just attach listeners to know when to set values in the DrawPanel. You should probably also add the XSliderBeans to the panel that holds the DrawPanel and not the DrawPanel itself, and have listeners at this level that listen to changes of the sliders and call methods on the DrawPanel to set the color.
 
Candy Bortniker
Ranch Hand
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to make the changes suggested but I'm having a problem with blue.addAdjustmentListener(this); I get a complie error: The method addAdjustmentListener(DrawFrame) is undefined for the type BlueSliderBean. I have added

I have also added to the driver program:
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're creating the sliders inside the DrawFrame class, then you just need to pass a reference to the DrawPanel to that method. It would show up somewhere after this code:

 
Candy Bortniker
Ranch Hand
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I have and I still get the same error message.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoops... I went back and looked and the slider bean doesn't have that method defined for it. You'll need to add the addAdjustmentListener() and removeAdjustmentListener() methods to the bean yourself. All they'll do is pass their parameter on to the JSlider inside the slider bean.
 
Candy Bortniker
Ranch Hand
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I've add addAdjustmentListener and removeAdjustmentListener but I still don't get a slider to appear on window. Here is the updated bean:
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that in the constructor all the components are created and added to panels, but nothing is added to the BlueSliderBean itself. (this.add(...)). You could fix it either by adding "panel2" to "this", or by doing away with "panel2" and adding everything to "this" instead.
 
Candy Bortniker
Ranch Hand
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have taken out panel2 and add:
this.add(sliderPanel, BorderLayout.CENTER);
this.add(textField);
this.add(integerlbl, BorderLayout.SOUTH);
but still nothing appears on the window.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure where else the problem could be, so I've made a similiar working example... maybe this will help you figure out what went wrong?

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic