Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes using a bean to put slider on panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "using a bean to put slider on panel" Watch "using a bean to put slider on panel" New topic
Author

using a bean to put slider on panel

Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
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

Joined: Oct 18, 2000
Posts: 4121

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.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
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

Joined: Oct 18, 2000
Posts: 4121

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

Joined: Mar 17, 2003
Posts: 123
That's what I have and I still get the same error message.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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

Joined: Mar 17, 2003
Posts: 123
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

Joined: Oct 18, 2000
Posts: 4121

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

Joined: Mar 17, 2003
Posts: 123
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

Joined: Oct 18, 2000
Posts: 4121

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
 
subject: using a bean to put slider on panel