Results 1 to 3 of 3
Like Tree2Likes
  • 2 Post By DarrylBurke

Thread: Can't change background of a JFrame with JScrollBar?

  1. #1
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default Can't change background of a JFrame with JScrollBar?

    Hello everyone. I have a 'Grapher' class that extends JComponent that draws some rectangles. I wanted it to have a scrollbar, so I added an instance of the Grapher class into a JScrollBar, added the JScrollBar into a JFrame, and set the frame visible to see my rectangles. I didn't like the gray background so I figured I would be easily able to change it to another color, say red, but however I do this nothing works. Here is a part of my code:
    Java Code:
    			Grapher component = new Grapher();
    			component.setPreferredSize(new Dimension(1250, 500));
    			component.setBackground(Color.red);
    			
    			
    			JScrollPane scrollBar=new JScrollPane(component,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);  
    			scrollBar.setPreferredSize(component.getPreferredSize());
    			
    			scrollBar.setBackground(Color.red);
    			
    			JFrame viewFrame = new JFrame("Viewing Window");
    			viewFrame.setSize(1250, 500); 
    			viewFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    			viewFrame.add(scrollBar);
    			viewFrame.setLocationRelativeTo(null); //makes frame appear in center of screen
    			
    			viewFrame.getContentPane().setBackground(Color.red); 
    			
    			viewFrame.setVisible(true);

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Can't change background of a JFrame with JScrollBar?

    Moved from New to Java.

    Please use meaningful variable names. That means, don't have a variable named scrollBar that refers to a JScrollPane.

    Anything you see in a JScrollPane is the scroll pane's viewport view: in your code snippet, component. If the viewport view is transparent -- doesn't paint its background -- the background of the JScrollPane's JViewport will show. Not the background of the scroll pane nor that of the frame's content pane.

    db
    Fubarable and davetheant like this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default Re: Can't change background of a JFrame with JScrollBar?

    Thank you very much db, I was able to remove all of the other attempts at coloring the background and replaced it with a single line! I just called the scrollBar.getViewport() method and set the Viewport's color to Red. Thanks again

Similar Threads

  1. How to change background color
    By Jeffrey4u in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 10-22-2011, 08:32 AM
  2. Change background image
    By Jeffrod in forum AWT / Swing
    Replies: 2
    Last Post: 08-12-2011, 02:14 AM
  3. Change JPanel background after its been set once
    By mevets in forum AWT / Swing
    Replies: 4
    Last Post: 04-14-2010, 01:07 AM
  4. JFrame window doesnt change background color
    By Addez in forum New To Java
    Replies: 7
    Last Post: 11-07-2009, 09:38 PM
  5. when muse pressed the background change
    By pcman in forum Java Applets
    Replies: 1
    Last Post: 03-17-2008, 11:51 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
  •