Results 1 to 5 of 5
  1. #1
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default 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.

    Java 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();
            }
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default 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?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default 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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Using scrollBars to change the color of a label

    Thanks

Similar Threads

  1. Change color of a region
    By sky in forum AWT / Swing
    Replies: 5
    Last Post: 11-24-2009, 03:47 PM
  2. change Panel Color
    By aadi_j in forum AWT / Swing
    Replies: 2
    Last Post: 11-16-2009, 10:12 AM
  3. can we change label from action class?
    By kishan in forum Advanced Java
    Replies: 2
    Last Post: 04-28-2009, 02:12 PM
  4. How to Change the color of MultiColumnListBox
    By Java.child in forum AWT / Swing
    Replies: 1
    Last Post: 01-22-2009, 12:07 AM
  5. How to change string Color
    By Java.child in forum AWT / Swing
    Replies: 3
    Last Post: 01-06-2009, 04:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •