my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Scrollbars 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 "Scrollbars" Watch "Scrollbars" New topic
Author

Scrollbars

Marie Jeanne Thibault
Greenhorn

Joined: Nov 15, 2003
Posts: 15
Hi!
Can anyone please help me understand why my panel p1 won't change color?
Thanks,
Lynn
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.Color.*;
public class Scroll extends JFrame implements AdjustmentListener
{
//Declare
private JScrollBar jscbHort1, jscbHort2, jscbHort3;

//Main
public static void main(String[] args)
{
Scroll frame = new Scroll();
frame.pack();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(400, 250);
frame.setVisible(true);
}
//Default constructor
public Scroll()
{
setTitle("Exercise 9.9: Use Scroll Bars");
//Create panel to hold the color display
JPanel p1 = new JPanel();
p1.add(new JLabel("Show Colors", SwingConstants.CENTER));
p1.setLayout(new GridLayout(1, 1));
//Create second panel that holds the scrollbars
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(3, 1));
p2.setBorder(new TitledBorder("Choose Colors"));
p2.add(new JLabel("Red"));
p2.add(jscbHort1 = new JScrollBar());
jscbHort1.setValues(0,50,0,255);
p2.add(new JLabel("Green"));
p2.add(jscbHort2 = new JScrollBar());
jscbHort2.setValues(0,50,0,255);
p2.add(new JLabel("Blue"));
p2.add(jscbHort3 = new JScrollBar());
jscbHort3.setValues(0,50,0,255);
jscbHort1.setOrientation(Adjustable.HORIZONTAL);
jscbHort2.setOrientation(Adjustable.HORIZONTAL);
jscbHort3.setOrientation(Adjustable.HORIZONTAL);
//Add panels to frame
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p1, BorderLayout.CENTER);
getContentPane().add(p2, BorderLayout.SOUTH);
//Register the listeners
jscbHort1.addAdjustmentListener(this);
jscbHort2.addAdjustmentListener(this);
jscbHort3.addAdjustmentListener(this);
}

//Handle the scroll bar adjustment actions
public void adjustmentValueChanged(AdjustmentEvent e)
{
JPanel p1 = new JPanel();
if(e.getSource() == jscbHort1)
{
Color c = new Color(jscbHort1.getValue(), jscbHort2.getValue(), jscbHort3.getValue());
p1.setForeground(Color.red);
p1.repaint();
}
else if(e.getSource() == jscbHort2)
{
Color c = new Color(jscbHort1.getValue(), jscbHort2.getValue(), jscbHort3.getValue());
p1.setForeground(Color.green);
p1.repaint();
}
else if(e.getSource() == jscbHort3)
{
Color c = new Color(jscbHort1.getValue(), jscbHort2.getValue(), jscbHort3.getValue());
p1.setForeground(Color.blue);
p1.repaint();
}
}
}
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch Marie.

Notice that using a GridLayout makes the label as big as the whole room panel p1 is given. If you placed a button instead of a label, or the label were not responsible for painting its background (label.setOpaque(true) the colors would not be visible!


SCJP2. Please Indent your code using UBB Code
Marie Jeanne Thibault
Greenhorn

Joined: Nov 15, 2003
Posts: 15
Hi Jose,
Wow, looks like I was making it a lot more complicated than it should've been. By the way, I got it (nudge, nudge, wink, wink)...
Thanks again!
 
Consider Paul's rocket mass heater.
 
subject: Scrollbars