Using scrollBars to change the color of a label
Hello all I have a question for you , you have been so helpful in the past. I am creating a program that uses three scroll bars to change the foreground color of a label. I have it changing thcolor but my question is when I move the sroll bar forward the color changes but when I move the scroll bar back the labe stays the changed color.How do I get the original color to return after moving the scroll bar back to its original postion.I was thinkng of something like the mouse press release listener but the adjustment listener donesnt have that option .Any help would be great thanks alot.
Code:
import javax.swing.*;
/**
* Created with IntelliJ IDEA.
* User: root
* Date: 6/6/12
* Time: 1:20 PM
* To change this template use File | Settings | File Templates.
*/
public class Exercise17_15 {
public static void main(String[] args)
{
Exercise17_15_GUI gui = new Exercise17_15_GUI();
gui.setVisible(true);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setLocationRelativeTo(null);
}
}
import java.awt.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.*;
/*
* Created by JFormDesigner on Wed Jun 06 14:47:30 CDT 2012
*/
/**
* @author Adam Ortell
*/
public class Exercise17_15_GUI extends JFrame {
public Exercise17_15_GUI() {
initComponents();
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
// Generated using JFormDesigner Evaluation license - Adam Ortell
scrollBar3 = new JScrollBar();
scrollBar3.addAdjustmentListener(new adjustmentListener3());
scrollBar4 = new JScrollBar();
scrollBar4.addAdjustmentListener(new adjustmentListener2());
scrollBar5 = new JScrollBar();
scrollBar5.addAdjustmentListener(new adjustmentListener1());
label5 = new JLabel();
label6 = new JLabel();
label7 = new JLabel();
label8 = new JLabel();
label9 = new JLabel();
//======== this ========
Container contentPane = getContentPane();
//---- scrollBar3 ----
scrollBar3.setOrientation(Adjustable.HORIZONTAL);
//---- scrollBar4 ----
scrollBar4.setOrientation(Adjustable.HORIZONTAL);
//---- scrollBar5 ----
scrollBar5.setOrientation(Adjustable.HORIZONTAL);
//---- label5 ----
label5.setText(" Blue");
//---- label6 ----
label6.setText(" Green");
//---- label7 ----
label7.setText(" Red");
//---- label8 ----
label8.setText(" Choose Colors");
//---- label9 ----
label9.setText("Show Colors");
GroupLayout contentPaneLayout = new GroupLayout(contentPane);
contentPane.setLayout(contentPaneLayout);
contentPaneLayout.setHorizontalGroup(
contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addGroup(contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addGroup(contentPaneLayout.createParallelGroup()
.addComponent(label5, GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
.addComponent(label6, GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
.addComponent(label7, GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(contentPaneLayout.createParallelGroup()
.addComponent(scrollBar5, GroupLayout.PREFERRED_SIZE, 272, GroupLayout.PREFERRED_SIZE)
.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
.addComponent(scrollBar3, GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
.addComponent(scrollBar4, GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE))))
.addGroup(contentPaneLayout.createSequentialGroup()
.addContainerGap()
.addComponent(label8, GroupLayout.PREFERRED_SIZE, 167, GroupLayout.PREFERRED_SIZE)
.addGap(0, 197, Short.MAX_VALUE)))
.addContainerGap())
.addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
.addGap(0, 154, Short.MAX_VALUE)
.addComponent(label9, GroupLayout.PREFERRED_SIZE, 152, GroupLayout.PREFERRED_SIZE)
.addGap(82, 82, 82))
);
contentPaneLayout.setVerticalGroup(
contentPaneLayout.createParallelGroup()
.addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
.addGap(0, 67, Short.MAX_VALUE)
.addComponent(label9)
.addGap(60, 60, 60)
.addComponent(label8)
.addGap(18, 18, 18)
.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addGroup(contentPaneLayout.createSequentialGroup()
.addComponent(scrollBar5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(scrollBar4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(contentPaneLayout.createSequentialGroup()
.addComponent(label7)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(label6)))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(contentPaneLayout.createParallelGroup()
.addComponent(scrollBar3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label5)))
);
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
// Generated using JFormDesigner Evaluation license - Adam Ortell
private JScrollBar scrollBar3;
private JScrollBar scrollBar4;
private JScrollBar scrollBar5;
private JLabel label5;
private JLabel label6;
private JLabel label7;
private JLabel label8;
private JLabel label9;
// JFormDesigner - End of variables declaration //GEN-END:variables
class adjustmentListener1 implements AdjustmentListener
{
public void adjustmentValueChanged(AdjustmentEvent adjustmentEvent)
{
label9.setForeground(Color.RED);
repaint();
}
}
class adjustmentListener2 implements AdjustmentListener
{
public void adjustmentValueChanged(AdjustmentEvent adjustmentEvent)
{
label9.setForeground(Color.GREEN);
repaint();
}
}
class adjustmentListener3 implements AdjustmentListener
{
public void adjustmentValueChanged(AdjustmentEvent adjustmentEvent)
{
label9.setForeground(Color.BLUE);
repaint();
}
}
}
Re: Using scrollBars to change the color of a label
The colors set by the adjustment listeners are a fixed color and have no relationship with where the slider is positioned.
Should there be a relationship?
Re: Using scrollBars to change the color of a label
Yes the blue slider should tint the text bluer and same with the other colors.
Re: Using scrollBars to change the color of a label
You should look at using the Color class's constructor and the values from the scrollbar.
Re: Using scrollBars to change the color of a label