2

There are two things that I am trying to figure out. First thing, I want to figure out how to make a Jcomponent background transparent. When placing a JPanel into another JPanel that has a color set as the background, the JPanel that is set within the other JPanel has a white background that I can't seem to get rid of. I tried using the firstpanel.setOpache function and repaint but it doesn't do anything.

And second, I noticed that putting a JPanel within another JPanel thats within another JPanel compresses it size. The images below will show what I am trying to describe. I want to know what to do to avoid compressing the JPanel size and still able to put it within two levels of other JPanels . The code below is what I am practicing with.

import javax.swing.*;
import java.awt.*;


public class LearningFrame extends JFrame {

private JLabel userLabel;

private LearningFrame(){
    JPanel backGroundPanel = new JPanel();
    JPanel mainPanel = new JPanel();
    JPanel firstPanel = new JPanel();
    JPanel secondPanel = new JPanel();

    userLabel = new JLabel("User");
    userLabel.setFont(new Font("Arial",1 ,24));

    backGroundPanel.setBackground(new Color(247,211,53));
   // backGroundPanel.setLayout(new BoxLayout(backGroundPanel,BoxLayout.Y_AXIS));

    setContentPane(backGroundPanel);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(1200,800);
    setLocationRelativeTo(null);

    //backGroundPanel.setLayout(null);
    mainPanel.setLayout(new GridLayout(1,2));

    firstPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    secondPanel.setLayout(new BoxLayout(secondPanel,BoxLayout.Y_AXIS));

   // firstPanel.setBackground(new Color(211,43,185));
    secondPanel.setBackground(new Color(34,233,44));


    JButton button = new JButton("Click");
    JButton button2 = new JButton("Click");
    JButton button3 = new JButton("Click");
    JButton button4 = new JButton("Click");


    firstPanel.add(button);
    firstPanel.add(button2);
    firstPanel.add(userLabel);
    secondPanel.add(button3);
    secondPanel.add(button4);

    mainPanel.add(firstPanel);
    mainPanel.add(secondPanel);

    backGroundPanel.add(mainPanel);


    setVisible(true);

}

public JPanel logPanel() {

    JPanel logInPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

    JTextField userTextField = new JTextField(14);
    JTextField passTextField = new JTextField(14);


    userLabel = new JLabel("Username: ");
    JLabel passLabel = new JLabel("Password: ");
    passLabel.setFont(new Font("Arial", Font.BOLD, 24));
    userLabel.setFont(new Font("Arial", Font.BOLD, 24));
    logInPanel.add(userLabel);
    logInPanel.add(userTextField);
    logInPanel.add(passLabel);
    logInPanel.add(passTextField);

    logInPanel.setOpaque(true);
    logInPanel.repaint();

    return logInPanel;
}

public static void main(String[] args){

    LearningFrame x = new LearningFrame();
}

}

This image is of two JPanels within the main JPanel, the JPanels expands and are not compressed

This image is of two JPanels within a JPanel that is within another JPanel, the two JPanels are compressed.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Chuck
  • 75
  • 1
  • 2
  • 10
  • 1
    Try panel.setBackground( new Color(r, g, b, a) ); A being an alpha property ....http://stackoverflow.com/questions/3562225/java-set-opacity-in-jpanel repeat question? – DarkV1 Jul 08 '16 at 00:32
  • For full transparency you just use `setOpaque( false )` on the child panels that you add to a parent panel. Don't play with the background or use a Color with an alpha value of 0. `I tried using the firstpanel.setOpache function` - well you add the "firstPanel" to "mainPanel" so you also need to make "mainPanel" transparent if you want to see the background panel color. – camickr Jul 08 '16 at 01:13
  • @camickr Thank you, it finally worked. And I forgot about the mainPanel transparency. Do you know why the firstPanel, and secondPanel size compresses within the mainPanel while its in backgroundPanel? – Chuck Jul 08 '16 at 03:55
  • @DarkV1 Thank you! It had work – Chuck Jul 08 '16 at 03:55
  • @Chuck, `Do you know why.. ` - By default a JPanel uses a FlowLayout which respects the preferred size of all components added to it. So you main panel is using a FlowLayout.I guess you would need to get rid of the "mainPanel", then just add your two compoennts to the background panel and each panel will take up half of the frame. – camickr Jul 08 '16 at 04:25
  • *"And second.."* Nope. You're mistaking SO for a help desk. Each Q&A on this site should be devoted to a *single question* and include a title descriptive of that problem. So decide which question you want answered on this thread, make the title specific, and start a new question for the other matter. – Andrew Thompson Jul 08 '16 at 05:06
  • BTW `userLabel.setFont(new Font("Arial",1 ,24));` that will only work on PCs with the Arial font installed. Better to make it `userLabel.setFont(new Font(Font.SANS_SERIF,1 ,24));` - that will produce an Arial font for Windows, Helvetica for OS X, not sure (but the default undecorated font) for Linux.. And just noticed, don't use 'magic numbers'. Constants were designed to be descriptive, and provide compile time checking, so it should be `userLabel.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,24));` – Andrew Thompson Jul 08 '16 at 05:09
  • Are you using the swing class? If yes, then go to the design view. Then you can edit the properties of all JComponents from the bottom left menu. – Lakshya Goyal Jul 08 '16 at 09:59

1 Answers1

0

Just use

firstPanel.setOpaque(false);

This is will make the background of the component invisible, but you will still be able to see any components that are positioned inside it.

nick zoum
  • 7,216
  • 7
  • 36
  • 80