Results 1 to 10 of 10
  1. #1
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default a setOpaque(false) JPanel is not transparent

    These classes should create a 400X400 window showing a blue circle on a red background. Problem: they show a blue circle on a grey (empty?) background.

    Java Code:
    public class test {
    	public static void main(String[] args)
    	{
    		new testWindow();
    	}
    }
    Java Code:
    import javax.swing.JFrame;
    
    public class testWindow extends JFrame{
    	private JFrame f=new JFrame();
    	public testWindow()
    	{
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.add(new background());
    		forground p=new forground();
    		p.setOpaque(false);
    		f.add(p);
    		f.pack();
    		f.setVisible(true);
    	}
    }
    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    
    import javax.swing.JPanel;
    
    
    public class background extends JPanel
    {
    	public background()
    	{
    		
    	}
    	public Dimension getPreferredSize()
    	{
    		return new Dimension (400,400);
    	}
    	public void paint(Graphics g)
    	{
    		super.paintComponents(g);
    		g.setColor(Color.red);
    		g.fillRect(0,0,400,400);
    	}
    }
    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    
    import javax.swing.JPanel;
    
    
    public class forground extends JPanel{
    	public forground()
    	{
    		
    	}
    	public void paint(Graphics g)
    	{
    		super.paintComponents(g);
    		g.setColor(Color.blue);
    		g.fillOval(50,50,100,100);
    	}
    	public Dimension getPreferredSize()
    	{
    		return new Dimension (400,400);
    	}
    }
    Can anyone show me how to make the "forground" JPanel transparent? As this is a simple compilable version of my bigger program, painting both the background and the forground in one JPanel doesn't help me.

  2. #2
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    your main problem is here

    f.add(new background());
    then
    f.add(p);

    look up how borderLayout works, to see what needs to be added to what

    Java Code:
    public void paint(Graphics g)
    	{
    		super.paintComponents(g);
    yuk!

    try
    Java Code:
    //public void paint(Graphics g)
    public void paintComponent(Graphics g)
    	{
    	      //super.paintComponents(g);
                    super.paintComponent(g);//singular

  3. #3
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Ok, So I changed the code to
    Java Code:
    import java.awt.BorderLayout;
    import javax.swing.JFrame;
    
    public class testWindow extends JFrame{
    	private JFrame f=new JFrame();
    	public testWindow()
    	{
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setLayout(new BorderLayout());
    		f.add(new background(), BorderLayout.CENTER);
    		forground p=new forground();
    		p.setOpaque(true);
    		f.add(p, BorderLayout.CENTER);
    		
    		f.pack();
    		f.setVisible(true);
    	}
    }
    And I've changed the "super.paintComponents(g);" to "super.paintComponent(g);". However the output is still the same. Note that I want the 2 JPanels to be displayed on top of each other and that any part of the forground JPanel that isn't painted, the background shows through. So that the result is a blue circle on a red background.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I think that when adding two components BorderLayout.CENTER, the second one completely obscures the first, regardless of the second's opaque setting. I believe that a JLayeredPane may suit your purposes better.

  5. #5
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    > Note that I want the 2 JPanels to be displayed on top of each other

    OverlayLayout panel to contain the 2 panels,
    then add the overlayLayoutPanel to BorderLayout.CENTER

    can't see why adding foregroundPanel to backgroundPanel,
    then adding backgroundPanel to CENTER wouldn't also work
    the way you want

  6. #6
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Quote Originally Posted by Michael Dunn View Post
    > Note that I want the 2 JPanels to be displayed on top of each other

    OverlayLayout panel to contain the 2 panels,
    then add the overlayLayoutPanel to BorderLayout.CENTER

    can't see why adding foregroundPanel to backgroundPanel,
    then adding backgroundPanel to CENTER wouldn't also work
    the way you want
    I can't see that either, all I can see that it doesn't.

    Using JLayeredPane, I think it should be something like this:

    Java Code:
    import java.awt.BorderLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JLayeredPane;
    
    public class testWindow extends JFrame{
    	private JFrame f=new JFrame();
    	private JLayeredPane p=new JLayeredPane();
    	public testWindow()
    	{
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setLayout(new BorderLayout());
    		p.addImpl(new background(), BorderLayout.CENTER, 0);
    		forground d=new forground();
    		d.setOpaque(false);
    		p.addImpl(d, BorderLayout.CENTER, -1);
    
    		p.add(new background(), BorderLayout.CENTER);
    		f.pack();
    		f.setVisible(true);
    	}
    }
    However these lines gives problems: "p.addImpl(d, BorderLayout.CENTER, -1);" and "p.addImpl(d, BorderLayout.CENTER, -1);".
    I looked at the API, send in the right parameters (I think), added it to the JFrame, but it says it isn't visible. When I run it anyway, this error occurs:

    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	The method addImpl(Component, Object, int) from the type JLayeredPane is not visible
    	The method addImpl(Component, Object, int) from the type JLayeredPane is not visible
    
    	at testWindow.<init>(testWindow.java:13)
    	at test.main(test.java:5)
    Last edited by imorio; 08-30-2010 at 05:15 PM.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you're going to use JLayeredPanes, you'd best read the tutorials as you don't appear to be using it correctly.

  8. #8
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    If you're going to use JLayeredPanes, you'd best read the tutorials as you don't appear to be using it correctly.
    I read this tutorial: Java Platform SE 6. However my problems remain. Based on the source code found in that tutorial I removed my forground and background classes and rewrote my testWindow class to:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JLayeredPane;
    
    public class testWindow extends JFrame{
    	private JFrame f=new JFrame();
    	private JLayeredPane p=new JLayeredPane();
    	public testWindow()
    	{
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setLayout(new BorderLayout());
    		p.setPreferredSize(new Dimension(400, 400));
    		p.add(createBackground(), new Integer(0));
    		p.add(createForground(), new Integer(-1));
    
    		p.add(new background(), BorderLayout.CENTER);
    		f.setPreferredSize(new Dimension(400,400));
    		f.setVisible(true);
    	}
    	public JLabel createBackground()
    	{
    		JLabel label=new JLabel();
    		label.setVerticalAlignment(JLabel.TOP);
    		label.setHorizontalAlignment(JLabel.CENTER);
    		label.setBackground(Color.red);
    		label.setBounds(0,0,400,400);
    		label.setOpaque(true);
    		return label;
    	}
    	public JLabel createForground()
    	{
    		JLabel label=new JLabel();
    		label.setVerticalAlignment(JLabel.TOP);
    		label.setHorizontalAlignment(JLabel.CENTER);
    		label.setBackground(Color.blue);
    		label.setBounds(50,50,100,100);
    		label.setOpaque(true);
    		return label;
    	}
    }
    However this just creates a window with minimal size and when I enlarge the window, it is empty. This is odd because I setPreferredSize twice and add everything to the JFrame as I think I should. This is very frustrating, because debugging only leads me to conclude that the things I want to appear don't appear.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    1) Where do you add your JLayeredPane to the JFrame?
    2) What is new background() supposed to do?
    3) If your JLabels are opaque, how does one see through them?
    4) What are you trying to do when you add something to a JLayeredPane BorderLayout.CENTER?

    Luck

  10. #10
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Thanks, it works now!! You were right, I didn't add the JLayeredPane... But now everything works better then expected. Repainting the JPanel automaticly update's the GUI. Now I can finally continue with my program. Thanks again for answering my question, when in retrospect I looked really stupid.

Similar Threads

  1. transparent jframe?
    By majkel in forum AWT / Swing
    Replies: 12
    Last Post: 10-09-2010, 05:06 PM
  2. Transparent layer
    By disabled in forum SWT / JFace
    Replies: 0
    Last Post: 03-20-2010, 04:04 PM
  3. swirling becomes false,why?
    By arefeh in forum New To Java
    Replies: 2
    Last Post: 01-18-2010, 06:12 PM
  4. SWT Transparent button
    By ynnorj in forum SWT / JFace
    Replies: 2
    Last Post: 03-04-2009, 06:33 AM
  5. Transparent JTextPane
    By Ada in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2007, 09:50 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
  •