473,386 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.border.*;
  5.  
  6. public class SwingTemplate extends JFrame {
  7.     private JPanel mainPanel,secondPanel,sliderPanel;
  8.     private JLabel colorSquare,colorID;
  9.     private Color [] colorArray;
  10.     private JLabel [] gridCell;
  11.     private JLabel rLabel, gLabel, bLabel;
  12.     private JSlider RedS,BlueS,GreenS;
  13.     Box horizontalBox;
  14.     Box horizontalBox2;
  15.     Border thisborder;
  16.  
  17.  
  18.     public SwingTemplate() {    
  19.             colorArray = new Color[32]; 
  20.             gridCell = new JLabel[32];
  21.             for (int i=0; i<32; i++){
  22.             gridCell[i] =new JLabel();
  23.             gridCell[i].setOpaque(true); 
  24.             gridCell[i].addMouseListener(new MyGridCellHandler());
  25.             }       
  26.         setWindowAttributes();
  27.         setLookAndFeel();
  28.         addMainPanel();
  29.         addComponents();
  30.         setVisible(true);
  31.         }
  32.  
  33. //////////////////////////////////////////////////////////////////////////////        
  34. /// MAIN METHODS ///
  35. //////////////////////////////////////////////////////////////////////////////
  36.  
  37.     private void setWindowAttributes() {
  38.         setTitle("Simple Color Chooser");
  39.         setSize(700,800);
  40.         setLocation(50,50);
  41.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  42.         }
  43.  
  44.     private void setLookAndFeel() {
  45.         try {
  46.             UIManager.setLookAndFeel(
  47.             UIManager.getSystemLookAndFeelClassName());
  48.             //UIManager.getCrossPlatformLookAndFeelClassName());
  49.             }
  50.         catch(Exception e) {
  51.         System.out.println("Sorry, LookAndFeel not found.  Available LAFs are\n");
  52.         UIManager.LookAndFeelInfo [] x = UIManager.getInstalledLookAndFeels();
  53.         for(int i=0; i < x.length; i++)
  54.             System.out.println(x[i]);
  55.             }
  56.         }
  57.  
  58.     private void addMainPanel() {
  59.         mainPanel = new JPanel();
  60.         mainPanel.setLayout(new FlowLayout());        
  61.         mainPanel.setBackground(new Color(240,240,240));
  62.         add(mainPanel);
  63.         } 
  64.  
  65.  
  66.  
  67.     private void addComponents() {
  68.      ///////////////////////////////////////////////////////
  69.      //TITLE BAR
  70.  
  71.         JRootPane root = getRootPane();
  72.         mainPanel.add(Box.createRigidArea(new Dimension(5,5)));
  73.         JMenuBar bar = new JMenuBar();
  74.         JMenuItem mi,exitItem;
  75.         JMenu menu = new JMenu("File");
  76.         bar.add(menu);
  77.         menu.add("Clear Contents");
  78.         menu.add("Exit");
  79.         JMenu menu1 = new JMenu("Help");
  80.         bar.add(menu1);
  81.         menu1.add("About ColorChooser");
  82.  
  83.         root.setJMenuBar(bar);
  84.         mainPanel.add(Box.createRigidArea(new Dimension(5,5)));
  85.     ///////////////////////////////////////////////////////////
  86.  
  87.     //////////////////////////////////////////////////////////
  88.     //COLOR BUTTONS
  89.  
  90.      JPanel panel = new JPanel();
  91.  
  92.      JFrame frame = new JFrame();
  93.      Container contentPane = frame.getContentPane();
  94.      horizontalBox = Box.createHorizontalBox();
  95.      horizontalBox2 = Box.createHorizontalBox();
  96.      horizontalBox.add(Box.createGlue());
  97.      horizontalBox.add(new JButton("Yellow"));
  98.      horizontalBox.add(new JButton("Orange"));
  99.      //horizontalBox.add(new JButton("Red"));
  100.      horizontalBox.add(new JButton("Pink"));
  101.      horizontalBox.add(new JButton("Teal"));
  102.      horizontalBox.add(new JButton("Aqua"));
  103.      horizontalBox.add(new JButton("Lime"));
  104.      horizontalBox.add(new JButton("Green"));
  105.      panel = new JPanel(new BorderLayout());
  106.  
  107.      horizontalBox.add(Box.createGlue());
  108.      panel.add(horizontalBox); 
  109.      panel.setBorder(BorderFactory.createTitledBorder("Light Colors"));
  110.      mainPanel.add(panel); 
  111.  
  112.      horizontalBox.add(Box.createGlue());  
  113.      mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));
  114.      ///////////////////////////////////////////////////////////
  115.     JPanel p2 = new JPanel();
  116.     horizontalBox2=Box.createHorizontalBox();
  117.     horizontalBox2.add(new JButton("Black"));
  118.     horizontalBox2.add(new JButton("Grey"));
  119.     horizontalBox2.add(new JButton("Brown"));
  120.     horizontalBox2.add(new JButton("Blue"));
  121.     horizontalBox2.add(new JButton("Purple"));
  122.     //horizontalBox2.add(new JButton("Green"));
  123.     horizontalBox2.add(new JButton("Red"));
  124.     p2 = new JPanel(new BorderLayout());
  125.     p2.add(horizontalBox2);
  126.     p2.setBorder(BorderFactory.createTitledBorder("Dark Colors"));
  127.     mainPanel.add(p2);
  128.     mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));
  129.     ////////////////////////////////////////////////////////////////
  130.     //Color Swatch Panel
  131.     JLabel colorSquare = new JLabel();
  132.     colorSquare.setOpaque(true);
  133.     colorSquare.setPreferredSize(new Dimension(200,200));
  134.     colorSquare.setMaximumSize(new Dimension(200,200)); 
  135.     colorSquare.setBackground(Color.white);
  136.     mainPanel.add(colorSquare);
  137.  
  138.     colorSquare.setBorder(BorderFactory.createTitledBorder("ColorSwatch"));
  139.     mainPanel.add(Box.createRigidArea(new Dimension(5,0)));
  140.     ///////////////////////////////////////////////////////////////
  141.  
  142.  
  143.     ////////////////////////////////////////////////////////////
  144.     //color grid
  145.  
  146.     JPanel colorPanel= new JPanel();
  147.     colorPanel.setLayout(new GridLayout(4,8));
  148.     colorPanel.setPreferredSize(new Dimension (200,200));
  149.     //colorPanel.setSize(new Dimension(200,200));
  150.     colorPanel.setMaximumSize(new Dimension(200,200));
  151.     colorPanel.setBorder(new LineBorder(Color.black,2));
  152.         int idx=0;
  153.         for (int i =0; i<4; i++)
  154.             for (int j=0; j<8;j++)
  155.                 colorPanel.add(gridCell[idx++],i,j);
  156.                 fillColorArray();
  157.                 setGridColors();
  158.     //horizontalBox= Box.createHorizontalBox();
  159.     //horizontalBox.add(colorPanel);
  160.     mainPanel.add(colorPanel);
  161.  
  162.     colorPanel.setBorder(BorderFactory.createTitledBorder("Color Grid"));
  163.     mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));
  164.  
  165.     //////////////////////////////////////////////////////////                 
  166.  
  167.     /////////////////////////////////////////////////////////////
  168.     //Sliders
  169.  
  170.  
  171.     JPanel SliderControls = new JPanel();
  172.     SliderControls.setLayout(new GridLayout(0,3));
  173.     JSlider BlueS = new JSlider(JSlider.HORIZONTAL,0,255,0);
  174.     JSlider RedS = new JSlider(JSlider.HORIZONTAL,0,255,0);
  175.     JSlider GreenS = new JSlider(JSlider.HORIZONTAL,0,255,0);
  176.     BlueS.setBorder(BorderFactory.createTitledBorder("Red")); 
  177.     BlueS.setMajorTickSpacing(50);
  178.     BlueS.setMinorTickSpacing(10);
  179.     BlueS.setPaintTicks(true);
  180.     BlueS.setPaintLabels(true);
  181.  
  182.     RedS.setBorder(BorderFactory.createTitledBorder("Green")); 
  183.     RedS.setMajorTickSpacing(50);
  184.     RedS.setMinorTickSpacing(10);
  185.     RedS.setPaintTicks(true);
  186.     RedS.setPaintLabels(true);
  187.  
  188.     GreenS.setBorder(BorderFactory.createTitledBorder("Blue")); 
  189.     GreenS.setMajorTickSpacing(50);
  190.     GreenS.setMinorTickSpacing(10);
  191.     GreenS.setPaintTicks(true);
  192.     GreenS.setPaintLabels(true);
  193.     mainPanel.add(Box.createRigidArea(new Dimension(5,5)));
  194.     SliderControls.add(RedS);
  195.     SliderControls.add(GreenS);
  196.     SliderControls.add(BlueS);
  197.  
  198.     SliderListener listener = new SliderListener();
  199.         RedS.addChangeListener(listener);
  200.         GreenS.addChangeListener(listener);
  201.         BlueS.addChangeListener(listener);
  202.  
  203.  
  204.     mainPanel.add(Box.createRigidArea(new Dimension(5,5)));
  205.     mainPanel.add(SliderControls, BorderLayout.PAGE_END);
  206.       mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));
  207.  
  208.  
  209.     ////////////////////////////////////////////////////////
  210.     //color ID and Random button
  211.     ////////
  212.     mainPanel.add(Box.createRigidArea(new Dimension(40,40)));  
  213.         colorID = new JLabel("Color Selected: #000000");
  214.         colorID.setFont(new Font("Arial",Font.BOLD,24));
  215.         colorID.setAlignmentX(Component.CENTER_ALIGNMENT);
  216.         mainPanel.add(colorID); 
  217.  
  218.     mainPanel.add(Box.createRigidArea(new Dimension(40,40)));
  219.         JButton button = new JButton("Click me for new colors");
  220. button.setToolTipText("Clicking on this button generates 32 new random colors");
  221.         button.setAlignmentX(Component.CENTER_ALIGNMENT);
  222.         button.addActionListener(new ActionListener() {
  223.             public void actionPerformed(ActionEvent e) {
  224.                 fillColorArray();
  225.                 setGridColors();
  226.                 }
  227.             });                      
  228.         mainPanel.add(button);
  229.  
  230.  
  231.  
  232.     ////////////////////////////////////////////////////////////             
  233.  
  234.         }   
  235.  
  236. //////////////////////////////////////////////////////////////////////////////
  237. /// GUI BUILD AND PLACE
  238. //////////////////////////////////////////////////////////////////////////////                
  239.  
  240.  
  241.           private void fillColorArray(){
  242.             int red=0,green=0,blue=0;
  243.             for(int i=0; i<32; i++) {
  244.             red = (int) (256*Math.random());
  245.             green = (int) (256*Math.random());
  246.             blue = (int) (256*Math.random()); 
  247.  
  248.             colorArray[i] = new Color(red,green,blue);
  249.             //colorArray[i] = new Color(red,green);
  250.             }
  251.             }
  252.  
  253.  
  254.         private void setGridColors() {
  255.  
  256.         for (int i=0; i<32; i++) {
  257.             gridCell[i].setBackground(colorArray[i]);
  258.             gridCell[i].repaint();
  259.             }
  260.  
  261.  
  262.         }
  263.  
  264.  
  265.  
  266. //////////////////////////////////////////////////////////////////////////////
  267. /// HELPER METHODS ///
  268. //////////////////////////////////////////////////////////////////////////////
  269.  
  270. public class SliderListener implements ChangeListener {
  271.          int red, green, blue;
  272.  
  273.         public void stateChanged(ChangeEvent event) {
  274.             red = RedS.getValue();
  275.             green = GreenS.getValue();
  276.             blue = BlueS.getValue();
  277.  
  278.             rLabel.setText("Red: " + red + "  Hex: " + 
  279.                 Integer.toHexString(red).toUpperCase());
  280.             gLabel.setText("Green: " + green + "  Hex: " + 
  281.                 Integer.toHexString(green).toUpperCase());
  282.             bLabel.setText("Blue: " + blue + "  Hex: " + 
  283.                 Integer.toHexString(blue).toUpperCase());
  284.  
  285.             colorSquare.setBackground(new Color(red, green, blue));
  286.             }
  287.         }
  288.  
  289.  
  290. //////////////////////////////////////////////////////////////////////////////        
  291. /// MOUSE HANDLER CLASS ///   
  292.  class MyGridCellHandler extends MouseAdapter{
  293.     public void mouseClicked(MouseEvent e) {
  294.     JLabel tmp= (JLabel) e.getSource();
  295.     Color c =tmp.getBackground();
  296.     String red = Integer.toHexString(c.getRed()).toUpperCase();
  297.     String blue=Integer.toHexString(c.getBlue()).toUpperCase();
  298.     String green= Integer.toHexString(c.getGreen()).toUpperCase();
  299.         if(red.length() !=2) red = "0" + red;
  300.         if (green.length() !=2) green = "0" + green;
  301.         if (blue.length() !=2) blue = "0" + blue;
  302.         colorID.setText("Color Selected: #" +red+green+blue);
  303.         colorSquare.setBackground(c);
  304.  
  305.  
  306.   }
  307.   }
  308.  
  309.  
  310. //////////////////////////////////////////////////////////////////////////////
  311.  
  312.     public static void main(String [] args) {
  313.         new SwingTemplate();
  314.         }
  315.     }
  316.  
Jul 23 '09 #1
10 4606
JosAH
11,448 Expert 8TB
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 NullPointerException is thrown.

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

kind regards,

Jos
Jul 23 '09 #2
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..

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.border.*;
  5. import javax.swing.event.*;
  6.  
  7. public class SwingTemplate extends JFrame {
  8.     private JPanel mainPanel,secondPanel,colorPanel;
  9.     private JLabel colorSquare,colorID;
  10.     private Color [] colorArray;
  11.     private JLabel [] gridCell;
  12.     private JLabel rLabel, gLabel, bLabel;
  13.     private JSlider RedS,BlueS,GreenS;
  14.     Box horizontalBox;
  15.     Box horizontalBox2;
  16.     Border thisborder;
  17.  
  18.  
  19.     public SwingTemplate() {    
  20.             colorArray = new Color[32]; 
  21.             gridCell = new JLabel[32];
  22.             for (int i=0; i<32; i++){
  23.             gridCell[i] =new JLabel();
  24.             gridCell[i].setOpaque(true); 
  25.             gridCell[i].addMouseListener(new MyGridCellHandler());
  26.             }       
  27.         setWindowAttributes();
  28.         setLookAndFeel();
  29.         addMainPanel();
  30.         addComponents();
  31.         setVisible(true);
  32.         }
  33.  
  34. //////////////////////////////////////////////////////////////////////////////        
  35. /// MAIN METHODS ///
  36. //////////////////////////////////////////////////////////////////////////////
  37.  
  38.     private void setWindowAttributes() {
  39.         setTitle("Simple Color Chooser");
  40.         setSize(700,800);
  41.         setLocation(50,50);
  42.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  43.         }
  44.  
  45.     private void setLookAndFeel() {
  46.         try {
  47.             UIManager.setLookAndFeel(
  48.             UIManager.getSystemLookAndFeelClassName());
  49.             //UIManager.getCrossPlatformLookAndFeelClassName());
  50.             }
  51.         catch(Exception e) {
  52.         System.out.println("Sorry, LookAndFeel not found.  Available LAFs are\n");
  53.         UIManager.LookAndFeelInfo [] x = UIManager.getInstalledLookAndFeels();
  54.         for(int i=0; i < x.length; i++)
  55.             System.out.println(x[i]);
  56.             }
  57.         }
  58.  
  59.     private void addMainPanel() {
  60.         mainPanel = new JPanel();
  61.         mainPanel.setLayout(new FlowLayout());        
  62.         mainPanel.setBackground(new Color(240,240,240));
  63.         add(mainPanel);
  64.         } 
  65.  
  66.  
  67.  
  68.     private void addComponents() {
  69.      ///////////////////////////////////////////////////////
  70.      //TITLE BAR
  71.  
  72.         JRootPane root = getRootPane();
  73.         mainPanel.add(Box.createRigidArea(new Dimension(5,5)));
  74.         JMenuBar bar = new JMenuBar();
  75.         JMenuItem mi,exitItem;
  76.         JMenu menu = new JMenu("File");
  77.         bar.add(menu);
  78.         menu.add("Clear Contents");
  79.         menu.add("Exit");
  80.         JMenu menu1 = new JMenu("Help");
  81.         bar.add(menu1);
  82.         menu1.add("About ColorChooser");
  83.  
  84.         root.setJMenuBar(bar);
  85.         mainPanel.add(Box.createRigidArea(new Dimension(5,5)));
  86.     ///////////////////////////////////////////////////////////
  87.  
  88.     //////////////////////////////////////////////////////////
  89.     //COLOR BUTTONS
  90.  
  91.      JPanel panel = new JPanel();
  92.  
  93.      JFrame frame = new JFrame();
  94.      Container contentPane = frame.getContentPane();
  95.      horizontalBox = Box.createHorizontalBox();
  96.      horizontalBox2 = Box.createHorizontalBox();
  97.      horizontalBox.add(Box.createGlue());
  98.      horizontalBox.add(new JButton("Yellow"));
  99.      horizontalBox.add(new JButton("Orange"));
  100.      //horizontalBox.add(new JButton("Red"));
  101.      horizontalBox.add(new JButton("Pink"));
  102.      horizontalBox.add(new JButton("Teal"));
  103.      horizontalBox.add(new JButton("Aqua"));
  104.      horizontalBox.add(new JButton("Lime"));
  105.      horizontalBox.add(new JButton("Green"));
  106.      panel = new JPanel(new BorderLayout());
  107.  
  108.      horizontalBox.add(Box.createGlue());
  109.      panel.add(horizontalBox); 
  110.      panel.setBorder(BorderFactory.createTitledBorder("Light Colors"));
  111.      mainPanel.add(panel); 
  112.  
  113.      horizontalBox.add(Box.createGlue());  
  114.      mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));
  115.      ///////////////////////////////////////////////////////////
  116.     JPanel p2 = new JPanel();
  117.     horizontalBox2=Box.createHorizontalBox();
  118.     horizontalBox2.add(new JButton("Black"));
  119.     horizontalBox2.add(new JButton("Grey"));
  120.     horizontalBox2.add(new JButton("Brown"));
  121.     horizontalBox2.add(new JButton("Blue"));
  122.     horizontalBox2.add(new JButton("Purple"));
  123.     //horizontalBox2.add(new JButton("Green"));
  124.     horizontalBox2.add(new JButton("Red"));
  125.     p2 = new JPanel(new BorderLayout());
  126.     p2.add(horizontalBox2);
  127.     p2.setBorder(BorderFactory.createTitledBorder("Dark Colors"));
  128.     mainPanel.add(p2);
  129.     mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));
  130.     ////////////////////////////////////////////////////////////////
  131.     //Color Swatch Panel
  132.     colorSquare = new JLabel();
  133.     colorSquare.setOpaque(true);
  134.     colorSquare.setPreferredSize(new Dimension(200,200));
  135.     colorSquare.setMaximumSize(new Dimension(200,200)); 
  136.     colorSquare.setBackground(Color.white);
  137.     mainPanel.add(colorSquare);
  138.  
  139.     colorSquare.setBorder(BorderFactory.createTitledBorder("ColorSwatch"));
  140.     mainPanel.add(Box.createRigidArea(new Dimension(5,0)));
  141.     ///////////////////////////////////////////////////////////////
  142.  
  143.  
  144.     ////////////////////////////////////////////////////////////
  145.     //color grid
  146.  
  147.     colorPanel= new JPanel();
  148.     colorPanel.setLayout(new GridLayout(4,8));
  149.     colorPanel.setPreferredSize(new Dimension (200,200));
  150.  
  151.     colorPanel.setMaximumSize(new Dimension(200,200));
  152.     colorPanel.setBorder(new LineBorder(Color.black,2));
  153.         int idx=0;
  154.         for (int i =0; i<4; i++)
  155.             for (int j=0; j<4;j++)
  156.                 colorPanel.add(gridCell[idx++],i,j);
  157.                 fillColorArray();
  158.                 setGridColors();
  159.     mainPanel.add(colorPanel);
  160.     colorPanel.setBorder(BorderFactory.createTitledBorder("Color Grid"));
  161.     mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));
  162.  
  163.     //////////////////////////////////////////////////////////                 
  164.  
  165.     /////////////////////////////////////////////////////////////
  166.     //Sliders
  167.  
  168.  
  169.     JPanel SliderControls = new JPanel();
  170.     SliderControls.setLayout(new GridLayout(0,3));
  171.      BlueS = new JSlider(JSlider.HORIZONTAL,0,255,0);
  172.      RedS = new JSlider(JSlider.HORIZONTAL,0,255,0);
  173.      GreenS = new JSlider(JSlider.HORIZONTAL,0,255,0);
  174.     BlueS.setBorder(BorderFactory.createTitledBorder("Blue")); 
  175.     BlueS.setMajorTickSpacing(50);
  176.     BlueS.setMinorTickSpacing(10);
  177.     BlueS.setPaintTicks(true);
  178.     BlueS.setPaintLabels(true);
  179.  
  180.     RedS.setBorder(BorderFactory.createTitledBorder("Red")); 
  181.     RedS.setMajorTickSpacing(50);
  182.     RedS.setMinorTickSpacing(10);
  183.     RedS.setPaintTicks(true);
  184.     RedS.setPaintLabels(true);
  185.  
  186.     GreenS.setBorder(BorderFactory.createTitledBorder("Green")); 
  187.     GreenS.setMajorTickSpacing(50);
  188.     GreenS.setMinorTickSpacing(10);
  189.     GreenS.setPaintTicks(true);
  190.     GreenS.setPaintLabels(true);
  191.     mainPanel.add(Box.createRigidArea(new Dimension(5,5)));
  192.     SliderControls.add(RedS);
  193.     SliderControls.add(GreenS);
  194.     SliderControls.add(BlueS);
  195.  
  196.     SliderListener listener = new SliderListener();
  197.         RedS.addChangeListener(listener);
  198.         GreenS.addChangeListener(listener);
  199.         BlueS.addChangeListener(listener);
  200.  
  201.         rLabel = new JLabel("Red: 0");
  202.         rLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
  203.         gLabel = new JLabel("Green: 0");
  204.         gLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
  205.         bLabel = new JLabel("Blue: 0");
  206.         bLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
  207.  
  208.     mainPanel.add(Box.createRigidArea(new Dimension(5,5)));
  209.     mainPanel.add(SliderControls, BorderLayout.PAGE_END);
  210.       mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));
  211.  
  212.  
  213.     ////////////////////////////////////////////////////////
  214.     //color ID and Random button
  215.     ////////
  216.         mainPanel.add(Box.createRigidArea(new Dimension(40,40)));  
  217.         colorID = new JLabel("Color Selected: #000000");
  218.         colorID.setFont(new Font("Arial",Font.BOLD,24));
  219.         colorID.setAlignmentX(Component.CENTER_ALIGNMENT);
  220.         colorID.setBorder(BorderFactory.createTitledBorder("Hex Value"));
  221.         mainPanel.add(colorID);
  222.  
  223.     ////////////////////////////////////////////////////////////             
  224.  
  225.         }   
  226.  
  227. //////////////////////////////////////////////////////////////////////////////
  228. /// GUI BUILD AND PLACE
  229. //////////////////////////////////////////////////////////////////////////////                
  230.  
  231.  
  232.           private void fillColorArray(){
  233.             int red=0,green=0,blue=0,yellow=0;
  234.             for(int i=0; i<32; i++) {
  235.             red = (int) (256*Math.random());
  236.             green = (int) (256*Math.random());
  237.             blue = (int) (256*Math.random()); 
  238.  
  239.             colorArray[i] = new Color(red,green,blue);
  240.            // colorArray[i] = new Color(yellow);
  241.             }
  242.             }
  243.  
  244.  
  245.         private void setGridColors() {
  246.  
  247.         for (int i=0; i<16; i++) {
  248.             gridCell[i].setBackground(colorArray[i]);
  249.             gridCell[i].repaint();
  250.             }
  251.  
  252.  
  253.         }
  254.  
  255.  
  256.  
  257. //////////////////////////////////////////////////////////////////////////////
  258. /// HELPER METHODS ///
  259. //////////////////////////////////////////////////////////////////////////////
  260.  
  261. private class SliderListener implements ChangeListener {
  262.        private int red, green, blue;
  263.  
  264.         public void stateChanged(ChangeEvent event) {
  265.         //Object source = event.getSource();
  266.             red = RedS.getValue();
  267.             green = GreenS.getValue();
  268.             blue = BlueS.getValue();
  269.  
  270.             rLabel.setText("Red: " + red + "  Hex: " + 
  271.                 Integer.toHexString(red).toUpperCase());
  272.             gLabel.setText("Green: " + green + "  Hex: " + 
  273.                 Integer.toHexString(green).toUpperCase());
  274.             bLabel.setText("Blue: " + blue + "  Hex: " + 
  275.                 Integer.toHexString(blue).toUpperCase());
  276.  
  277.             colorSquare.setBackground(new Color(red, green, blue));
  278.             }
  279.         }
  280.  
  281.  
  282. //////////////////////////////////////////////////////////////////////////////        
  283. /// MOUSE HANDLER CLASS ///   
  284.  class MyGridCellHandler extends MouseAdapter{
  285.     public void mouseClicked(MouseEvent e) {
  286.     JLabel tmp= (JLabel) e.getSource();
  287.     Color c =tmp.getBackground();
  288.     String red = Integer.toHexString(c.getRed()).toUpperCase();
  289.     String blue=Integer.toHexString(c.getBlue()).toUpperCase();
  290.     String green= Integer.toHexString(c.getGreen()).toUpperCase();
  291.         if(red.length() !=2) red = "0" + red;
  292.         if (green.length() !=2) green = "0" + green;
  293.         if (blue.length() !=2) blue = "0" + blue;
  294.         colorID.setText("Color Selected: #" +red+green+blue);
  295.         colorSquare.setBackground(c);
  296.  
  297.  
  298.   }
  299.   }
  300.  
  301.  
  302. //////////////////////////////////////////////////////////////////////////////
  303.  
  304.     public static void main(String [] args) {
  305.  
  306.         new SwingTemplate();
  307.                 }
  308.     }
Jul 23 '09 #3
JosAH
11,448 Expert 8TB
@yeshello54
Have you read the API documentation for the JSlider component? You can programmatically set its position.

kind regards,

Jos
Jul 23 '09 #4
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..
Jul 23 '09 #5
Markus
6,050 Expert 4TB
Well, as Jos said, the documentation holds all the answers you need. JSlider::setValue().
Jul 23 '09 #6
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.
Jul 24 '09 #7
JosAH
11,448 Expert 8TB
@yeshello54
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
Jul 24 '09 #8
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.
Jul 24 '09 #9
JosAH
11,448 Expert 8TB
@yeshello54
Did you write the code yourself? Because in your GridCellHandler you already do have usable code.

kind regards,

Jos
Jul 24 '09 #10
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
Jul 25 '09 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Scott Khan | last post by:
We have a Java Swing position. Need consultants ASAP. Position : Java Swing XML / SOAP - FX / Brokerage Requirements : Swing - JTrees / Jtables. other swing classes. etc. Java/ J2EE. JDK...
5
by: haran | last post by:
Hi, I have developed a project by using java Swings. Now i want to deploy that project into web. If its applet means we can deploy that by using browser. so plz help me to deploy the java swing...
3
by: itsmichelle | last post by:
This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However,...
3
by: bytespaulraj | last post by:
We have RHEL5 server. We could able to run Java programs through telnet But When we run Java Swing (GUI) we are getting GTK error Please help us to eliminate this error.
4
by: Ha Nguyen | last post by:
Hi all, I showed the popup when login fail but after that it was displayed again if i refresh that login page. Pls help me to not show popup when refreshing. Incidentally, is there any idea about...
2
by: Selva123 | last post by:
Hi All, Greetings. I want to test JAVA SWING application with PERL, do we have any module to do so (like win32::GUI for windows)?. or some third party free tools integrated with PERL? I am...
3
by: Akino877 | last post by:
Hello, I have a Java Swing application that I would like to be able to forward to or to run it - sorry, I am not sure if I am using the right term - from a JSP page. And I would like for my Java...
1
by: JanineXD | last post by:
Hey, I seem to have a problem on a Java Swing Program with ActionListener. I've tried to use getSource in our class but seem don't have an idea why it won't work when I tried to program at...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.