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 6 of 6

Thread: JLabel as part of JTabbedPane tab component overwrites background color

  1. #1
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default JLabel as part of JTabbedPane tab component overwrites background color

    Hi there,

    I am using my own custom component for a JTabbedPanes tab component. My custom component consists of a JLabel and a JButton (to close the tab).
    Code of the custom component:
    	protected static class Tab extends JPanel {
    		private static final long serialVersionUID = 1L;
     
    		private static Icon cachedIcon = null;
     
    		private final JButton btnClose;
     
    		public Tab(Ref ref) {
    			super();
    			if (cachedIcon == null) {
    				String iconPath = "/javax/swing/plaf/metal/icons/ocean/close.gif";
    				cachedIcon = new ImageIcon(Tab.class.getResource(iconPath));
    			}
    			setLayout(new GridLayout(0, 2, 0, 0));
     
    			JLabel lblName = new JLabel(ref.getName());
    			add(lblName);
     
    			btnClose = new JButton(cachedIcon);
    			add(btnClose);
    		}
     
    		public void addActionListener(ActionListener l) {
    			btnClose.addActionListener(l);
    		}
     
    		public void removeActionListener(ActionListener l) {
    			btnClose.removeActionListener(l);
    		}
     
    	}
    This is how I setup my tabbed pane:
    addTab(ref.getName(), panel);
    Tab tab = new Tab(ref);
    setTabComponentAt(indexOfComponent(panel), tab);

    When the tabbed pane is drawn it looks like this:
    image1.jpg

    The tab in the center is selected. You can clearly see, that the area of the JLabel uses a grey background although the rest of the tab component is light blue.

    What should I do to make the JLabels backdrop transparent?

    Thank you all for the help.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JLabel as part of JTabbedPane tab component overwrites background color

    Set the opacity of the component to false.

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: JLabel as part of JTabbedPane tab component overwrites background color

    Do you mean "setOpaque(boolean)"?
    I tried that and it didnt make a difference. If you meant something else please elaborate.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JLabel as part of JTabbedPane tab component overwrites background color

    Can you make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JLabel as part of JTabbedPane tab component overwrites background color

    That's what I meant. Try doing so for the Tab object as well as the JLabel it contains. If what you try doesn't achieve the desired result, post the updated code.

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: JLabel as part of JTabbedPane tab component overwrites background color

    Quote Originally Posted by copeg View Post
    That's what I meant. Try doing so for the Tab object as well as the JLabel it contains. If what you try doesn't achieve the desired result, post the updated code.
    Thanks copeq, I did not make the Tab opaque, only the label, and that was the trick.
    You really helped me out a lot.

    In retrospect, I am pretty stupid for making such a mistake.

Similar Threads

  1. [SOLVED] background color
    By keepStriving in forum AWT / Java Swing
    Replies: 4
    Last Post: December 3rd, 2013, 05:33 PM
  2. Setting background color to translucent?!?!
    By syregnar86 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 23rd, 2013, 07:58 AM
  3. Replies: 2
    Last Post: November 9th, 2011, 12:35 AM
  4. JTabbedPane with two Panels in the same tab?
    By cadarn in forum AWT / Java Swing
    Replies: 1
    Last Post: August 3rd, 2011, 10:48 AM
  5. Replies: 3
    Last Post: April 14th, 2010, 07:33 PM