java swing slider help!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yeshello54
    New Member
    • Mar 2009
    • 54

    java swing slider help!!

    Hey guys i am pretty new to java swing and need some help. I am developing a simple color chooser program in swing. I have a color panel that is connected to three sliders. red green and blue. but for some reason i cant get it to work..i will post my code and if anyone has any insight to why my changelistener wont work correctly I would really appreciate it. Thanks.

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    public class SwingTemplate extends JFrame {
        private JPanel mainPanel,secondPanel,sliderPanel;
        private JLabel colorSquare,colorID;
        private Color [] colorArray;
        private JLabel [] gridCell;
        private JLabel rLabel, gLabel, bLabel;
        private JSlider RedS,BlueS,GreenS;
        Box horizontalBox;
        Box horizontalBox2;
        Border thisborder;
    
       
        public SwingTemplate() {    
                colorArray = new Color[32]; 
                gridCell = new JLabel[32];
                for (int i=0; i<32; i++){
                gridCell[i] =new JLabel();
                gridCell[i].setOpaque(true); 
                gridCell[i].addMouseListener(new MyGridCellHandler());
                }       
            setWindowAttributes();
            setLookAndFeel();
            addMainPanel();
            addComponents();
            setVisible(true);
            }
            
    //////////////////////////////////////////////////////////////////////////////        
    /// MAIN METHODS ///
    //////////////////////////////////////////////////////////////////////////////
    
        private void setWindowAttributes() {
            setTitle("Simple Color Chooser");
            setSize(700,800);
            setLocation(50,50);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            }
                    
        private void setLookAndFeel() {
            try {
                UIManager.setLookAndFeel(
                UIManager.getSystemLookAndFeelClassName());
                //UIManager.getCrossPlatformLookAndFeelClassName());
                }
            catch(Exception e) {
            System.out.println("Sorry, LookAndFeel not found.  Available LAFs are\n");
            UIManager.LookAndFeelInfo [] x = UIManager.getInstalledLookAndFeels();
            for(int i=0; i < x.length; i++)
                System.out.println(x[i]);
                }
            }
            
        private void addMainPanel() {
            mainPanel = new JPanel();
            mainPanel.setLayout(new FlowLayout());        
            mainPanel.setBackground(new Color(240,240,240));
            add(mainPanel);
            } 
            
        
            
        private void addComponents() {
         ///////////////////////////////////////////////////////
         //TITLE BAR
        
            JRootPane root = getRootPane();
            mainPanel.add(Box.createRigidArea(new Dimension(5,5)));
            JMenuBar bar = new JMenuBar();
            JMenuItem mi,exitItem;
            JMenu menu = new JMenu("File");
            bar.add(menu);
            menu.add("Clear Contents");
            menu.add("Exit");
            JMenu menu1 = new JMenu("Help");
            bar.add(menu1);
            menu1.add("About ColorChooser");
            
            root.setJMenuBar(bar);
            mainPanel.add(Box.createRigidArea(new Dimension(5,5)));
        ///////////////////////////////////////////////////////////
        
        //////////////////////////////////////////////////////////
        //COLOR BUTTONS
        
         JPanel panel = new JPanel();
         
         JFrame frame = new JFrame();
         Container contentPane = frame.getContentPane();
         horizontalBox = Box.createHorizontalBox();
         horizontalBox2 = Box.createHorizontalBox();
         horizontalBox.add(Box.createGlue());
         horizontalBox.add(new JButton("Yellow"));
         horizontalBox.add(new JButton("Orange"));
         //horizontalBox.add(new JButton("Red"));
         horizontalBox.add(new JButton("Pink"));
         horizontalBox.add(new JButton("Teal"));
         horizontalBox.add(new JButton("Aqua"));
         horizontalBox.add(new JButton("Lime"));
         horizontalBox.add(new JButton("Green"));
         panel = new JPanel(new BorderLayout());
         
         horizontalBox.add(Box.createGlue());
         panel.add(horizontalBox); 
         panel.setBorder(BorderFactory.createTitledBorder("Light Colors"));
         mainPanel.add(panel); 
         
         horizontalBox.add(Box.createGlue());  
         mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));
         ///////////////////////////////////////////////////////////
        JPanel p2 = new JPanel();
        horizontalBox2=Box.createHorizontalBox();
        horizontalBox2.add(new JButton("Black"));
        horizontalBox2.add(new JButton("Grey"));
        horizontalBox2.add(new JButton("Brown"));
        horizontalBox2.add(new JButton("Blue"));
        horizontalBox2.add(new JButton("Purple"));
        //horizontalBox2.add(new JButton("Green"));
        horizontalBox2.add(new JButton("Red"));
        p2 = new JPanel(new BorderLayout());
        p2.add(horizontalBox2);
        p2.setBorder(BorderFactory.createTitledBorder("Dark Colors"));
        mainPanel.add(p2);
        mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));
        ////////////////////////////////////////////////////////////////
        //Color Swatch Panel
        JLabel colorSquare = new JLabel();
        colorSquare.setOpaque(true);
        colorSquare.setPreferredSize(new Dimension(200,200));
        colorSquare.setMaximumSize(new Dimension(200,200)); 
        colorSquare.setBackground(Color.white);
        mainPanel.add(colorSquare);
        
        colorSquare.setBorder(BorderFactory.createTitledBorder("ColorSwatch"));
        mainPanel.add(Box.createRigidArea(new Dimension(5,0)));
        ///////////////////////////////////////////////////////////////
     
         
        ////////////////////////////////////////////////////////////
        //color grid
        
        JPanel colorPanel= new JPanel();
        colorPanel.setLayout(new GridLayout(4,8));
        colorPanel.setPreferredSize(new Dimension (200,200));
        //colorPanel.setSize(new Dimension(200,200));
        colorPanel.setMaximumSize(new Dimension(200,200));
        colorPanel.setBorder(new LineBorder(Color.black,2));
            int idx=0;
            for (int i =0; i<4; i++)
                for (int j=0; j<8;j++)
                    colorPanel.add(gridCell[idx++],i,j);
                    fillColorArray();
                    setGridColors();
        //horizontalBox= Box.createHorizontalBox();
        //horizontalBox.add(colorPanel);
        mainPanel.add(colorPanel);
        
        colorPanel.setBorder(BorderFactory.createTitledBorder("Color Grid"));
        mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));
        
        //////////////////////////////////////////////////////////                 
                    
        /////////////////////////////////////////////////////////////
        //Sliders
    
       
        JPanel SliderControls = new JPanel();
        SliderControls.setLayout(new GridLayout(0,3));
        JSlider BlueS = new JSlider(JSlider.HORIZONTAL,0,255,0);
        JSlider RedS = new JSlider(JSlider.HORIZONTAL,0,255,0);
        JSlider GreenS = new JSlider(JSlider.HORIZONTAL,0,255,0);
        BlueS.setBorder(BorderFactory.createTitledBorder("Red")); 
        BlueS.setMajorTickSpacing(50);
        BlueS.setMinorTickSpacing(10);
        BlueS.setPaintTicks(true);
        BlueS.setPaintLabels(true);
        
        RedS.setBorder(BorderFactory.createTitledBorder("Green")); 
        RedS.setMajorTickSpacing(50);
        RedS.setMinorTickSpacing(10);
        RedS.setPaintTicks(true);
        RedS.setPaintLabels(true);
        
        GreenS.setBorder(BorderFactory.createTitledBorder("Blue")); 
        GreenS.setMajorTickSpacing(50);
        GreenS.setMinorTickSpacing(10);
        GreenS.setPaintTicks(true);
        GreenS.setPaintLabels(true);
        mainPanel.add(Box.createRigidArea(new Dimension(5,5)));
        SliderControls.add(RedS);
        SliderControls.add(GreenS);
        SliderControls.add(BlueS);
        
        SliderListener listener = new SliderListener();
            RedS.addChangeListener(listener);
            GreenS.addChangeListener(listener);
            BlueS.addChangeListener(listener);
            
            
        mainPanel.add(Box.createRigidArea(new Dimension(5,5)));
        mainPanel.add(SliderControls, BorderLayout.PAGE_END);
          mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));
    
        
        ////////////////////////////////////////////////////////
        //color ID and Random button
        ////////
        mainPanel.add(Box.createRigidArea(new Dimension(40,40)));  
            colorID = new JLabel("Color Selected: #000000");
            colorID.setFont(new Font("Arial",Font.BOLD,24));
            colorID.setAlignmentX(Component.CENTER_ALIGNMENT);
            mainPanel.add(colorID); 
    
        mainPanel.add(Box.createRigidArea(new Dimension(40,40)));
            JButton button = new JButton("Click me for new colors");
    button.setToolTipText("Clicking on this button generates 32 new random colors");
            button.setAlignmentX(Component.CENTER_ALIGNMENT);
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    fillColorArray();
                    setGridColors();
                    }
                });                      
            mainPanel.add(button);
                  
    
        
        ////////////////////////////////////////////////////////////             
                     
            }   
            
    //////////////////////////////////////////////////////////////////////////////
    /// GUI BUILD AND PLACE
    //////////////////////////////////////////////////////////////////////////////                
          
    
              private void fillColorArray(){
                int red=0,green=0,blue=0;
                for(int i=0; i<32; i++) {
                red = (int) (256*Math.random());
                green = (int) (256*Math.random());
                blue = (int) (256*Math.random()); 
                 
                colorArray[i] = new Color(red,green,blue);
                //colorArray[i] = new Color(red,green);
                }
                }
                
                
            private void setGridColors() {
            
            for (int i=0; i<32; i++) {
                gridCell[i].setBackground(colorArray[i]);
                gridCell[i].repaint();
                }
            
            
            }
            
            
    
    //////////////////////////////////////////////////////////////////////////////
    /// HELPER METHODS ///
    //////////////////////////////////////////////////////////////////////////////
      
    public class SliderListener implements ChangeListener {
             int red, green, blue;
            
            public void stateChanged(ChangeEvent event) {
                red = RedS.getValue();
                green = GreenS.getValue();
                blue = BlueS.getValue();
                
                rLabel.setText("Red: " + red + "  Hex: " + 
                    Integer.toHexString(red).toUpperCase());
                gLabel.setText("Green: " + green + "  Hex: " + 
                    Integer.toHexString(green).toUpperCase());
                bLabel.setText("Blue: " + blue + "  Hex: " + 
                    Integer.toHexString(blue).toUpperCase());
                
                colorSquare.setBackground(new Color(red, green, blue));
                }
            }
    
    
    //////////////////////////////////////////////////////////////////////////////        
    /// MOUSE HANDLER CLASS ///   
     class MyGridCellHandler extends MouseAdapter{
        public void mouseClicked(MouseEvent e) {
        JLabel tmp= (JLabel) e.getSource();
        Color c =tmp.getBackground();
        String red = Integer.toHexString(c.getRed()).toUpperCase();
        String blue=Integer.toHexString(c.getBlue()).toUpperCase();
        String green= Integer.toHexString(c.getGreen()).toUpperCase();
            if(red.length() !=2) red = "0" + red;
            if (green.length() !=2) green = "0" + green;
            if (blue.length() !=2) blue = "0" + blue;
            colorID.setText("Color Selected: #" +red+green+blue);
            colorSquare.setBackground(c);
            
      
      }
      }
    
             
    //////////////////////////////////////////////////////////////////////////////
            
        public static void main(String [] args) {
            new SwingTemplate();
            }
        }
    Last edited by JosAH; Jul 23 '09, 06:00 AM. Reason: added [code] ... [/code] tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    When you create your sliders you assign them to local variables with the same name as your member variables; leave out those local variables because they hide your member variables.

    You don't create rLabel, gLabel and bLabel but you do want to assign a text to them in your change listener. They are still null so a NullPointerExce ption is thrown.

    There may be more errors but I suggest you sprinkle in some System.out.prin tln() statements here and there so you can see what is actually happening.

    kind regards,

    Jos

    Comment

    • yeshello54
      New Member
      • Mar 2009
      • 54

      #3
      ya thank you i fixed it..but Ihave another question..when i select a random color from my grid it shows the color in the panel how i want..but I want the sliders to automatically get set to that color..Im not really sure how to do that. but if anyone could help that would be great..

      Code:
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.swing.border.*;
      import javax.swing.event.*;
      
      public class SwingTemplate extends JFrame {
          private JPanel mainPanel,secondPanel,colorPanel;
          private JLabel colorSquare,colorID;
          private Color [] colorArray;
          private JLabel [] gridCell;
          private JLabel rLabel, gLabel, bLabel;
          private JSlider RedS,BlueS,GreenS;
          Box horizontalBox;
          Box horizontalBox2;
          Border thisborder;
      
         
          public SwingTemplate() {    
                  colorArray = new Color[32]; 
                  gridCell = new JLabel[32];
                  for (int i=0; i<32; i++){
                  gridCell[i] =new JLabel();
                  gridCell[i].setOpaque(true); 
                  gridCell[i].addMouseListener(new MyGridCellHandler());
                  }       
              setWindowAttributes();
              setLookAndFeel();
              addMainPanel();
              addComponents();
              setVisible(true);
              }
              
      //////////////////////////////////////////////////////////////////////////////        
      /// MAIN METHODS ///
      //////////////////////////////////////////////////////////////////////////////
      
          private void setWindowAttributes() {
              setTitle("Simple Color Chooser");
              setSize(700,800);
              setLocation(50,50);
              setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
              }
                      
          private void setLookAndFeel() {
              try {
                  UIManager.setLookAndFeel(
                  UIManager.getSystemLookAndFeelClassName());
                  //UIManager.getCrossPlatformLookAndFeelClassName());
                  }
              catch(Exception e) {
              System.out.println("Sorry, LookAndFeel not found.  Available LAFs are\n");
              UIManager.LookAndFeelInfo [] x = UIManager.getInstalledLookAndFeels();
              for(int i=0; i < x.length; i++)
                  System.out.println(x[i]);
                  }
              }
              
          private void addMainPanel() {
              mainPanel = new JPanel();
              mainPanel.setLayout(new FlowLayout());        
              mainPanel.setBackground(new Color(240,240,240));
              add(mainPanel);
              } 
              
          
              
          private void addComponents() {
           ///////////////////////////////////////////////////////
           //TITLE BAR
          
              JRootPane root = getRootPane();
              mainPanel.add(Box.createRigidArea(new Dimension(5,5)));
              JMenuBar bar = new JMenuBar();
              JMenuItem mi,exitItem;
              JMenu menu = new JMenu("File");
              bar.add(menu);
              menu.add("Clear Contents");
              menu.add("Exit");
              JMenu menu1 = new JMenu("Help");
              bar.add(menu1);
              menu1.add("About ColorChooser");
              
              root.setJMenuBar(bar);
              mainPanel.add(Box.createRigidArea(new Dimension(5,5)));
          ///////////////////////////////////////////////////////////
          
          //////////////////////////////////////////////////////////
          //COLOR BUTTONS
          
           JPanel panel = new JPanel();
           
           JFrame frame = new JFrame();
           Container contentPane = frame.getContentPane();
           horizontalBox = Box.createHorizontalBox();
           horizontalBox2 = Box.createHorizontalBox();
           horizontalBox.add(Box.createGlue());
           horizontalBox.add(new JButton("Yellow"));
           horizontalBox.add(new JButton("Orange"));
           //horizontalBox.add(new JButton("Red"));
           horizontalBox.add(new JButton("Pink"));
           horizontalBox.add(new JButton("Teal"));
           horizontalBox.add(new JButton("Aqua"));
           horizontalBox.add(new JButton("Lime"));
           horizontalBox.add(new JButton("Green"));
           panel = new JPanel(new BorderLayout());
           
           horizontalBox.add(Box.createGlue());
           panel.add(horizontalBox); 
           panel.setBorder(BorderFactory.createTitledBorder("Light Colors"));
           mainPanel.add(panel); 
           
           horizontalBox.add(Box.createGlue());  
           mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));
           ///////////////////////////////////////////////////////////
          JPanel p2 = new JPanel();
          horizontalBox2=Box.createHorizontalBox();
          horizontalBox2.add(new JButton("Black"));
          horizontalBox2.add(new JButton("Grey"));
          horizontalBox2.add(new JButton("Brown"));
          horizontalBox2.add(new JButton("Blue"));
          horizontalBox2.add(new JButton("Purple"));
          //horizontalBox2.add(new JButton("Green"));
          horizontalBox2.add(new JButton("Red"));
          p2 = new JPanel(new BorderLayout());
          p2.add(horizontalBox2);
          p2.setBorder(BorderFactory.createTitledBorder("Dark Colors"));
          mainPanel.add(p2);
          mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));
          ////////////////////////////////////////////////////////////////
          //Color Swatch Panel
          colorSquare = new JLabel();
          colorSquare.setOpaque(true);
          colorSquare.setPreferredSize(new Dimension(200,200));
          colorSquare.setMaximumSize(new Dimension(200,200)); 
          colorSquare.setBackground(Color.white);
          mainPanel.add(colorSquare);
          
          colorSquare.setBorder(BorderFactory.createTitledBorder("ColorSwatch"));
          mainPanel.add(Box.createRigidArea(new Dimension(5,0)));
          ///////////////////////////////////////////////////////////////
       
           
          ////////////////////////////////////////////////////////////
          //color grid
          
          colorPanel= new JPanel();
          colorPanel.setLayout(new GridLayout(4,8));
          colorPanel.setPreferredSize(new Dimension (200,200));
          
          colorPanel.setMaximumSize(new Dimension(200,200));
          colorPanel.setBorder(new LineBorder(Color.black,2));
              int idx=0;
              for (int i =0; i<4; i++)
                  for (int j=0; j<4;j++)
                      colorPanel.add(gridCell[idx++],i,j);
                      fillColorArray();
                      setGridColors();
          mainPanel.add(colorPanel);
          colorPanel.setBorder(BorderFactory.createTitledBorder("Color Grid"));
          mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));
          
          //////////////////////////////////////////////////////////                 
                      
          /////////////////////////////////////////////////////////////
          //Sliders
      
         
          JPanel SliderControls = new JPanel();
          SliderControls.setLayout(new GridLayout(0,3));
           BlueS = new JSlider(JSlider.HORIZONTAL,0,255,0);
           RedS = new JSlider(JSlider.HORIZONTAL,0,255,0);
           GreenS = new JSlider(JSlider.HORIZONTAL,0,255,0);
          BlueS.setBorder(BorderFactory.createTitledBorder("Blue")); 
          BlueS.setMajorTickSpacing(50);
          BlueS.setMinorTickSpacing(10);
          BlueS.setPaintTicks(true);
          BlueS.setPaintLabels(true);
          
          RedS.setBorder(BorderFactory.createTitledBorder("Red")); 
          RedS.setMajorTickSpacing(50);
          RedS.setMinorTickSpacing(10);
          RedS.setPaintTicks(true);
          RedS.setPaintLabels(true);
          
          GreenS.setBorder(BorderFactory.createTitledBorder("Green")); 
          GreenS.setMajorTickSpacing(50);
          GreenS.setMinorTickSpacing(10);
          GreenS.setPaintTicks(true);
          GreenS.setPaintLabels(true);
          mainPanel.add(Box.createRigidArea(new Dimension(5,5)));
          SliderControls.add(RedS);
          SliderControls.add(GreenS);
          SliderControls.add(BlueS);
          
          SliderListener listener = new SliderListener();
              RedS.addChangeListener(listener);
              GreenS.addChangeListener(listener);
              BlueS.addChangeListener(listener);
              
              rLabel = new JLabel("Red: 0");
              rLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
              gLabel = new JLabel("Green: 0");
              gLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
              bLabel = new JLabel("Blue: 0");
              bLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
              
          mainPanel.add(Box.createRigidArea(new Dimension(5,5)));
          mainPanel.add(SliderControls, BorderLayout.PAGE_END);
            mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));
      
          
          ////////////////////////////////////////////////////////
          //color ID and Random button
          ////////
              mainPanel.add(Box.createRigidArea(new Dimension(40,40)));  
              colorID = new JLabel("Color Selected: #000000");
              colorID.setFont(new Font("Arial",Font.BOLD,24));
              colorID.setAlignmentX(Component.CENTER_ALIGNMENT);
              colorID.setBorder(BorderFactory.createTitledBorder("Hex Value"));
              mainPanel.add(colorID);
          
          ////////////////////////////////////////////////////////////             
                       
              }   
              
      //////////////////////////////////////////////////////////////////////////////
      /// GUI BUILD AND PLACE
      //////////////////////////////////////////////////////////////////////////////                
            
      
                private void fillColorArray(){
                  int red=0,green=0,blue=0,yellow=0;
                  for(int i=0; i<32; i++) {
                  red = (int) (256*Math.random());
                  green = (int) (256*Math.random());
                  blue = (int) (256*Math.random()); 
                   
                  colorArray[i] = new Color(red,green,blue);
                 // colorArray[i] = new Color(yellow);
                  }
                  }
                  
                  
              private void setGridColors() {
              
              for (int i=0; i<16; i++) {
                  gridCell[i].setBackground(colorArray[i]);
                  gridCell[i].repaint();
                  }
              
              
              }
              
              
      
      //////////////////////////////////////////////////////////////////////////////
      /// HELPER METHODS ///
      //////////////////////////////////////////////////////////////////////////////
       
      private class SliderListener implements ChangeListener {
             private int red, green, blue;
              
              public void stateChanged(ChangeEvent event) {
              //Object source = event.getSource();
                  red = RedS.getValue();
                  green = GreenS.getValue();
                  blue = BlueS.getValue();
                  
                  rLabel.setText("Red: " + red + "  Hex: " + 
                      Integer.toHexString(red).toUpperCase());
                  gLabel.setText("Green: " + green + "  Hex: " + 
                      Integer.toHexString(green).toUpperCase());
                  bLabel.setText("Blue: " + blue + "  Hex: " + 
                      Integer.toHexString(blue).toUpperCase());
                  
                  colorSquare.setBackground(new Color(red, green, blue));
                  }
              }
      
      
      //////////////////////////////////////////////////////////////////////////////        
      /// MOUSE HANDLER CLASS ///   
       class MyGridCellHandler extends MouseAdapter{
          public void mouseClicked(MouseEvent e) {
          JLabel tmp= (JLabel) e.getSource();
          Color c =tmp.getBackground();
          String red = Integer.toHexString(c.getRed()).toUpperCase();
          String blue=Integer.toHexString(c.getBlue()).toUpperCase();
          String green= Integer.toHexString(c.getGreen()).toUpperCase();
              if(red.length() !=2) red = "0" + red;
              if (green.length() !=2) green = "0" + green;
              if (blue.length() !=2) blue = "0" + blue;
              colorID.setText("Color Selected: #" +red+green+blue);
              colorSquare.setBackground(c);
              
        
        }
        }
      
               
      //////////////////////////////////////////////////////////////////////////////
              
          public static void main(String [] args) {
            
              new SwingTemplate();
                      }
          }
      Last edited by JosAH; Jul 23 '09, 07:17 PM. Reason: added [code] ... [/code] tags

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by yeshello54
        ya thank you i fixed it..but Ihave another question..when i select a random color from my grid it shows the color in the panel how i want..but I want the sliders to automatically get set to that color..Im not really sure how to do that. but if anyone could help that would be great..
        Have you read the API documentation for the JSlider component? You can programmaticall y set its position.

        kind regards,

        Jos

        Comment

        • yeshello54
          New Member
          • Mar 2009
          • 54

          #5
          ya I have read it..I am just a litle lost in where to go from here. I want to be able to click say my red square and then have the 3 sliders at the bottom jump to the position they need to be in to make red. so when red has been clicked the red slider will be at 255 the green at 0 and the blue at 0..and so on..

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Well, as Jos said, the documentation holds all the answers you need. JSlider::setVal ue().

            Comment

            • yeshello54
              New Member
              • Mar 2009
              • 54

              #7
              ok I understand the set value. here is my problem though.. if i add the following code "RedS.setValue( 255);" down in my mouse listener class well then if i click any of the 16 colors in my grid then the red slider changes to the value of 255. I want to be able to click just the red button and have the value change to 255 and if i click the blue button then the red goes to 0 and the blue goes to 255 and so on. So my question is how do i set it up to change based on my 16 different color squares. each color square in the grid is created as an array like so..

              "colorArray[1]=RED;"

              hopefully this makes sense..some guidence on how to fix this problem would be great. thanks.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by yeshello54
                ok I understand the set value. here is my problem though.. if i add the following code "RedS.setValue( 255);" down in my mouse listener class well then if i click any of the 16 colors in my grid then the red slider changes to the value of 255. I want to be able to click just the red button and have the value change to 255 and if i click the blue button then the red goes to 0 and the blue goes to 255 and so on. So my question is how do i set it up to change based on my 16 different color squares. each color square in the grid is created as an array like so..

                "colorArray[1]=RED;"

                hopefully this makes sense..some guidence on how to fix this problem would be great. thanks.
                If you have a Color object you can get its color components with the getRed(), getGreen() and getBlue() methods. Again, first read the API documentation and then post a question if you can't find what you're looking for.

                kind regards,

                Jos

                Comment

                • yeshello54
                  New Member
                  • Mar 2009
                  • 54

                  #9
                  i have read the api on it. But I think what i want to know how to do or where to look at how to do is inside my mouse listenter be able to say
                  "if mouse event e = colorArray[1]
                  then update slider to correct position"

                  im just confused on the proper syntax on how to code something like that.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by yeshello54
                    i have read the api on it. But I think what i want to know how to do or where to look at how to do is inside my mouse listenter be able to say
                    "if mouse event e = colorArray[1]
                    then update slider to correct position"

                    im just confused on the proper syntax on how to code something like that.
                    Did you write the code yourself? Because in your GridCellHandler you already do have usable code.

                    kind regards,

                    Jos

                    Comment

                    • yeshello54
                      New Member
                      • Mar 2009
                      • 54

                      #11
                      ahhh i figured it out now. Thanks for the direction. yes actually we wrote that method in class so that everyone could use it. I think i was thinking to hard about how to get the background color..thanks for the help

                      Comment

                      Working...