Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Trying To Change The Color Of JScrollBar

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Trying To Change The Color Of JScrollBar

    News = new JTextArea();
    		News.setText("News");
    		News.setForeground(two);
    		News.setBackground(one);
    		News.setEditable(false);
    		News.setLineWrap(true);
    		scrollPane = new JScrollPane(News);
    		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    		scrollPane.setBorder(BorderFactory.createEmptyBorder());
    		scrollPane.setBounds(30, 235, 340, 130);
    		Login.add(scrollPane);

    Googling the solution doesnt work this time. I tried UIManager, but that doesnt work
    Ex: UIManager.put("ScrollBar.thumb", two);
    I also tried this: scrollPane.setBackground(two);
    I'm also trying to figure out how to remove the scroll up and down buttons.
    Help would be much appreciated.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Trying To Change The Color Of JScrollBar

    I'm not 100% sure if it matters, but did you set the UIManager before or after you created the scroll pane?
    Also, have you tried setting the background and foreground colors on the scroll bars themselves, instead of the scroll pane? JScrollPane.getHorizontalScrollBar() and JScrollPane.getVerticalScrollBar() should get you the individual scroll bars.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Trying To Change The Color Of JScrollBar

    Thank you for the reply, I tried placing the UIManger before and after the JScrollPane.
    scrollPane.getVerticalScrollBar().setBackground(one);
    scrollPane.getVerticalScrollBar().setBorder(BorderFactory.createEmptyBorder());
    scrollPane.setBorder(BorderFactory.createEmptyBorder());

    This works: scrollPane.getVerticalScrollBar().setBackground(on e);
    I'm trying to remove the white border and the following two lines of code does not work:
    scrollPane.getVerticalScrollBar().setBorder(Border Factory.createEmptyBorder());
    scrollPane.setBorder(BorderFactory.createEmptyBord er());

    I still cant find a way to remove the buttons and make the thumb change color.

    --- Update ---

    Nevermind, I just realized that this works, but only in removing the border around the JTextArea I have created.
    scrollPane.getVerticalScrollBar().setBorder(Border Factory.createEmptyBorder());
    scrollPane.setBorder(BorderFactory.createEmptyBord er());
    I want to remove the white border around the scrollbar.

    --- Update ---

    I'm really close now, I figured out how to completely change the scrollbar by creating my own UI.
    Everything works in the MyScrollBarUI class, except that the thumb stays in place, where it is drawn, which makes sense, anybody know a easy clean fix to this?

    How I set Up The UI in my jpanel:
    scrollPane.getVerticalScrollBar().setUI(new MyScrollBarUI());

    CustomUI:
    class MyScrollBarUI extends BasicScrollBarUI{
    	Color one, two;
    	public MyScrollBarUI(){
    		Load settings = new Load();	
    		Color[] colors;
    		colors = settings.getColors();
    		one = colors[0];
    		two = colors[1];
    	}
     
    	@Override
    	protected JButton createDecreaseButton(int orientation) {
    		JButton button = new JButton("zero button");
    	    Dimension zeroDim = new Dimension(0,0);
    	    button.setPreferredSize(zeroDim);
    	    button.setMinimumSize(zeroDim);
    	    button.setMaximumSize(zeroDim);
    	    return button;
    	}
     
    	@Override
    	protected JButton createIncreaseButton(int orientation) {
    		JButton button = new JButton("zero button");
    	    Dimension zeroDim = new Dimension(0,0);
    	    button.setPreferredSize(zeroDim);
    	    button.setMinimumSize(zeroDim);
    	    button.setMaximumSize(zeroDim);
    	    return button;
    	}
     
    	@Override
        protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
            g.setColor(one);
            g.fillRect(0, 0, 20, 130);
        }
     
        @Override
        protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
            g.setColor(two);
            g.fillRect(0, 0, 20, 40);
        }
    }


    --- Update ---

    Guys, thanks for the help, I fixed it with this:
    g.setColor(two);
    g.translate(thumbBounds.x, thumbBounds.y);
    g.fillRect(0, 0, thumbBounds.width, thumbBounds.height);
    g.translate(-thumbBounds.x, -thumbBounds.y);

  4. The Following User Says Thank You to Damian3395 For This Useful Post:

    pbrockway2 (July 4th, 2013)

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Trying To Change The Color Of JScrollBar

    Thanks for the updates.

Similar Threads

  1. How to change color of JMenuItem when mouse is over it
    By Bagzli in forum AWT / Java Swing
    Replies: 5
    Last Post: April 2nd, 2012, 02:09 PM
  2. Bouncing Ball Program Random Color Change
    By coderEvolution in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 3rd, 2011, 04:01 PM
  3. Change font color and size
    By javanovice in forum AWT / Java Swing
    Replies: 2
    Last Post: April 20th, 2010, 09:57 AM
  4. Change of color for selected text in AWT
    By venkyInd in forum AWT / Java Swing
    Replies: 2
    Last Post: April 9th, 2010, 03:51 AM
  5. Checkbox - Change Font Color
    By wakebrdr77 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 10:57 AM