Results 1 to 11 of 11
  1. #1
    puan12 is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default Changing the Foreground Color of ArrowButton of JComboBox in nimbus LaF

    Hello Everybody,

    I want to change the foreground color of ArrowButton of JComboBox for below states. I have tried to override the default key values using the painter. but it is not working.
    Here is my Code

    Java Code:
    public class MyCombo extends JFrame {
    	public MyCombo(){
    		 String labels[] = { "A", "B", "C", "D" };
    		    JFrame frame = new JFrame("Popup JComboBox");
    		    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		    JComboBox comboBox = new JComboBox(labels);
    		    frame.add(comboBox, BorderLayout.NORTH);
    		    frame.setSize(300, 100);
    		    frame.setVisible(true);
    	}
    	
     public static void main(String[] args)
     {
    	 javax.swing.SwingUtilities.invokeLater(new Runnable() {
    		 
               public void run() {
               	
               	 UIManager.put("control", new Color(65,65,65));
               try {for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
      		            if ("Nimbus".equals(info.getName())) {
      		                UIManager.setLookAndFeel(info.getClassName());
      		                break;
      		            }
      		        }
      		    	 
      			  }	
      			catch (ClassNotFoundException e) {}
      		        catch (InstantiationException e) {}
      		        catch (IllegalAccessException e) {}
      		        catch (UnsupportedLookAndFeelException e) {}
               UIManager.getLookAndFeelDefaults().put("ComboBox:\"ComboBox.arrowButton\"[Enabled].foregroundPainter", new ArrowPainter(new Color(255,255,255)));
               UIManager.getLookAndFeelDefaults().put("ComboBox:\"ComboBox.arrowButton\"[MouseOver].foregroundPainter", new ArrowPainter(new Color(255,255,255)));
      			
      		new MyCombo();
               }
    	 });
     }
    }
    Here is ArrowPainter class

    Java Code:
    public class ArrowPainter implements Painter {
    	private float leftWidth;
        
        private float topHeight;
       
        private float centerWidth;
        
        private float centerHeight;
        
        private float rightWidth;
        
        private float bottomHeight;
        
        private float leftScale;
       
        private float topScale;
       
        private float centerHScale;
       
        private float centerVScale;
        
        private float rightScale;
        
        private float bottomScale;
    
    	private Color new_color;
    	 private Path2D path = new Path2D.Float();
    	 private Object[] componentColors;
    	 public ArrowPainter(Color new_color)
    	 {
    		 this.new_color = new_color;
    	 }
    	 @Override
    		public void paint(Graphics2D g, Object c, int width, int height) {
    			// TODO Auto-generated method stub
    			paintForegroundEnabled(g);
    		}
    	 public void paintForegroundEnabled(Graphics2D g) {
    	        path = decodePath5();
    	        g.setPaint(decodeGradient9(path));
    	        g.fill(path);
    
    	    }
    	private Paint decodeGradient9(Shape s) {
    		// TODO Auto-generated method stub
    		
    		 Rectangle2D bounds = s.getBounds2D();
    	        float x = (float)bounds.getX();
    	        float y = (float)bounds.getY();
    	        float w = (float)bounds.getWidth();
    	        float h = (float)bounds.getHeight();
    	        return decodeGradient((1.0f * w) + x, (0.5f * h) + y, (0.0f * w) + x, (0.5f * h) + y,
    	                new float[] { 0.0f,0.5f,1.0f },
    	                new Color[] { new_color,
    	                            decodeColor(new_color,new_color,0.5f),
    	                            new_color});
    	}
    	private Color decodeColor(Color color1, Color color2,
                float midPoint) {
    		// TODO Auto-generated method stub
    		 return new Color(deriveARGB(color1, color2, midPoint));
    	}
    	private int deriveARGB(Color color1, Color color2, float midPoint) {
    		// TODO Auto-generated method stub
    		 int r = color1.getRed() +
    	                Math.round((color2.getRed() - color1.getRed()) * midPoint);
    	        int g = color1.getGreen() +
    	                Math.round((color2.getGreen() - color1.getGreen()) * midPoint);
    	        int b = color1.getBlue() +
    	                Math.round((color2.getBlue() - color1.getBlue()) * midPoint);
    	        int a = color1.getAlpha() +
    	                Math.round((color2.getAlpha() - color1.getAlpha()) * midPoint);
    	        return ((a & 0xFF) << 24) |
    	                ((r & 0xFF) << 16) |
    	                ((g & 0xFF) << 8) |
    	                (b & 0xFF);
    	}
    	private Paint decodeGradient(float x1, float y1, float x2, float y2, float[] midpoints, Color[] colors) {
    		// TODO Auto-generated method stub
    		if (x1 == x2 && y1 == y2) {
                y2 += .00001f;
            }
            return new LinearGradientPaint(x1, y1, x2, y2, midpoints, colors);
    	}
    	private double decodeY(float y) {
    		// TODO Auto-generated method stub
    		 if (y >= 0 && y <= 1) {
    	            return y * topHeight;
    	        } else if (y > 1 && y < 2) {
    	            return ((y-1) * centerHeight) + topHeight;
    	        } else if (y >= 2 && y <= 3) {
    	            return ((y-2) * bottomHeight) + topHeight + centerHeight;
    	        } else {
    	            throw new IllegalArgumentException("Invalid y");
    	        }
    	}
    	private double decodeX(float x) {
    		// TODO Auto-generated method stub
    		 if (x >= 0 && x <= 1) {
    	            return x * leftWidth;
    	        } else if (x > 1 && x < 2) {
    	            return ((x-1) * centerWidth) + leftWidth;
    	        } else if (x >= 2 && x <= 3) {
    	            return ((x-2) * rightWidth) + leftWidth + centerWidth;
    	        } else {
    	            throw new IllegalArgumentException("Invalid x");
    	        }
    	}
    	private Path2D decodePath5() {
    		// TODO Auto-generated method stub
    		path.reset();
            path.moveTo(decodeX(0.9995915f), decodeY(1.3616071f));
            path.lineTo(decodeX(2.0f), decodeY(0.8333333f));
            path.lineTo(decodeX(2.0f), decodeY(1.8571429f));
            path.lineTo(decodeX(0.9995915f), decodeY(1.3616071f));
            path.closePath();
            return path;
    	}
    	
    }
    Could someone tell me how to change the Foregroundcolor of the ArrowButton?

    Thanks in advance
    Last edited by puan12; 08-12-2014 at 04:03 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Changing the Foreground Color of ArrowButton of JComboBox in nimbus LaF

    Please format your code by placing between [code][/code] tags. Also, please submit a Short, Self Contained, Correct Example
    demonstrating the problem. It should only focus on the specific issue and should compile and run.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    puan12 is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default Re: Changing the Foreground Color of ArrowButton of JComboBox in nimbus LaF

    Hello Jim,

    Thankyou for your suggestion.
    I have formatted the code. I have explained where exactly the problem occurs.

    Greetings
    Anusha

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Changing the Foreground Color of ArrowButton of JComboBox in nimbus LaF

    Quote Originally Posted by puan12 View Post
    I have formatted the code. I have explained where exactly the problem occurs.
    We can't run that code, as it's only two lines. Can you post an SSCCE (or an MCVE) that demonstrates the problem?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  5. #5
    puan12 is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default Re: Changing the Foreground Color of ArrowButton of JComboBox in nimbus LaF

    I am sorry for that.
    Now i have attached the executable code. That is the minimal code i can make it.
    Please find it.

    Greetings
    Anusha

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Changing the Foreground Color of ArrowButton of JComboBox in nimbus LaF

    Crossposted: java - Changing the Foreground Color of ArrowButton of JComboBox in nimbus LaF - Stack Overflow

    The UIManager methods you're trying to use only work with the Nimbus look and feel. More info here: Nimbus Defaults (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)

    The problem is that you aren't setting your look and feel to Nimbus anywhere.

    This code works for me, also notice that this is an MCVE instead of an entire project with extraneous methods:


    Java Code:
        import java.awt.Color;
        import java.awt.Component;
        import java.awt.Graphics2D;
        import java.lang.reflect.InvocationTargetException;
        
        import javax.swing.JComboBox;
        import javax.swing.JFrame;
        import javax.swing.Painter;
        import javax.swing.SwingUtilities;
        import javax.swing.UIManager;
        import javax.swing.UIManager.LookAndFeelInfo;
        
        
        public class Test {
        	public static void main (String [] args) throws InvocationTargetException, InterruptedException{
        		
        		SwingUtilities.invokeAndWait(new Runnable(){
        			public void run(){
        				
        				
        				try {
        				    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        				        if ("Nimbus".equals(info.getName())) {
        				            UIManager.setLookAndFeel(info.getClassName());
        				            break;
        				        }
        				    }
        				} catch (Exception e) {
        				    // If Nimbus is not available, you can set the GUI to another look and feel.
        				}
        				
        				 UIManager.getLookAndFeelDefaults().put("ComboBox:\"ComboBox.arrowButton\"[Enabled].foregroundPainter", new MyPainter());
        		         UIManager.getLookAndFeelDefaults().put("ComboBox:\"ComboBox.arrowButton\"[MouseOver].foregroundPainter", new MyPainter());
        		          
        
        		         JComboBox comboBox = new JComboBox();
        		         comboBox.addItem("one");
        		         comboBox.addItem("two");
        		         comboBox.addItem("three");
        		         
        				
        		         JFrame frame = new JFrame();
        		         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        		         frame.add(comboBox);
        		         frame.setSize(300, 300);
        		         frame.setVisible(true);
        			}
        		});
        	}
        	
        	public static class MyPainter implements Painter<Component>{
        
        		@Override
        		public void paint(Graphics2D g, Component object, int width, int height){
        			g.setColor(Color.RED);
        			g.fillRect(0, 0, width, height);
        		}
        	}
        }
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  7. #7
    puan12 is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default Re: Changing the Foreground Color of ArrowButton of JComboBox in nimbus LaF

    Thankyou for your reply.

    But i have also set the lookandfeel to Nimbus in my program.
    But this is not the solution i have expected. I have tried the above solution before.

    The above solution paints the rectangle over the arrow.I want to change only the color of arrow(by default it is black and i want to change it to white).
    Is this possible?

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Changing the Foreground Color of ArrowButton of JComboBox in nimbus LaF

    You have to draw the triangle yourself. Use my little example and add logic to it, and if it doesn't work, post the updated code.

    Note that there are other approaches to changing that icon instead of using a Painter. Google is your friend.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Changing the Foreground Color of ArrowButton of JComboBox in nimbus LaF

    You may want to look for the source of the look and feel for a ComboBox and
    simply "borrow" the arrowButton code. But always document that you did so.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    puan12 is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default Re: Changing the Foreground Color of ArrowButton of JComboBox in nimbus LaF

    I will try the approaches and i will update here. The thing is i have to change the color using Nimbus default keys, so i have to use the painter. Thanks again..!!! @KevinWorkman

    I did the same above in my ArrowPainter class, i have taken the code form ComboBoxArrowButtonPainter class. But it did not work for me. I don't know the exact problem @Jim

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Changing the Foreground Color of ArrowButton of JComboBox in nimbus LaF

    Start smaller. I gave you code that draws a big red rectangle. Can you draw a circle where you want the arrow to be instead? When you get that working, then think about drawing a triangle.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

  1. Replies: 4
    Last Post: 11-25-2010, 02:22 AM
  2. changing the border of a JComboBox
    By LittleRave in forum AWT / Swing
    Replies: 4
    Last Post: 01-18-2010, 02:59 PM
  3. Color-changing model
    By higuchi in forum New To Java
    Replies: 1
    Last Post: 03-19-2009, 07:29 AM
  4. Replies: 6
    Last Post: 01-02-2009, 09:36 PM
  5. How to change the foreground color of a disabled control
    By arunkumarsimhadri in forum New To Java
    Replies: 0
    Last Post: 07-09-2008, 04:30 PM

Posting Permissions

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