0

Well, actually I have a Layout problem in java Swing. I simply want to add a JPanel on the bottom of a Frame - a coding snipplet that might be done with every web based language in about 5 Minutes. Not so in Java. I tried to add a jPanel to a jFrame, that Contains a jContentPane, set the size of the jPanel to what I need and to repaint and revalidate the jFrame, as well as setting the LayOutManager to null.

Java shows me in this case a full-width jPanel, that fills my whole jFrame.

Therefore I tried another approach: I divided my jPanel in a fully transparent jPanel on top and a 20%opaque jPanel on the bottom. Still it didn't work out as expected.

Since then I tried to resize the child jPanels of my new Panel and the Panel as well and tried to repaint and revalidate the jFrame. Without any effect.

Despite of my efforts, java still shows me a full sized 20%opaque jPanel on the whole jFrame, that now contains another 20%opaque jPanel on Top.

I know that this whole problem is caused by the LayoutManager, Java useless per Default. However, it is not an option to set the LayoutManager to null or even change the LayoutManager of our jFrame, because that would lead us to refactor the whole functionality of our tiny app we worked on for several weeks.

    public void showUndoPanel() {

    System .out.println("Show Undo Panel");

    JPanel myPanel = new JPanel(null);
    JPanel glassPanel = new JPanel();
    JPanel ContentPanel = new JPanel();
    JLabel myJLabel = new JLabel("Great Job!");

    myPanel.setBackground(new Color(255,122,122,100));

    glassPanel.setSize(650, 550);
    glassPanel.setBackground(new Color(255,122,122,100));
    myPanel.add(glassPanel);

    ContentPanel.setSize(650, 30);
    ContentPanel.setBackground(new Color(255,122,122,20));
    ContentPanel.add(myJLabel);


    myPanel.revalidate();
    myPanel.repaint();

    undoPanel = myPanel;
    myJFrame.add(undoPanel);
    myJFrame.revalidate();
}

What I expected:

enter image description here

What it actually does:

enter image description here

mZed
  • 339
  • 2
  • 7
  • Have you tried this one? https://stackoverflow.com/questions/22194821/how-do-i-add-a-footerwith-text-to-the-bottom-of-this-java-application – M. Prokhorov Dec 17 '19 at 12:34
  • 2
    You need to apply the appropriate [layout managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) for the job. For more help please post [mre]. Side note : see [java naming conventions](https://www.geeksforgeeks.org/java-naming-conventions/) – c0der Dec 17 '19 at 12:34
  • 1
    Looking at what you expect and what you get, defnitly start using `LayoutManagers`. Also in this case you would only need the `ContentPanel` added. `undoPanel = ContentPanel; myJFrame.add(undoPanel);` – XtremeBaumer Dec 17 '19 at 12:37
  • Well thanks for the quick response to ya all . If I use a LayoutManager BoxLayout I receive a jFrame divided by 2 jPanels with 50% height - how can I change this to gain a divided JFrame with one Panel 80%height and another one with 20% height? :) – mZed Dec 17 '19 at 12:48
  • ah thanks . I solved it by using BoxLayout and a RigidArea :) I will provide that solution in a comment, in case if anyone else encounters the same issue once more :D – mZed Dec 17 '19 at 12:53
  • Tip: to address a comment to someone add a @username (for example: @c0der) to it. – c0der Dec 18 '19 at 06:48

2 Answers2

0

Well, I solved the problem by using a BoxLayoutManager and a RigidArea. In case if anyone else may encounter that problem again in the future, I decided to provide the code for this simple solution.

    public void showUndoPanel() {

    System .out.println("Show Undo Panel");

    JPanel myPanel = new JPanel(null);
    JPanel glassPanel = new JPanel();
    JPanel ContentPanel = new JPanel();
    JLabel myJLabel = new JLabel("Great Job!");

    myPanel.setBackground(new Color(255,255,255,0));
    myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.PAGE_AXIS));

    glassPanel.setSize(650, 650);
    glassPanel.setBounds(0,0,650,550);
    glassPanel.setBackground(new Color(255,122,122,0));
    myPanel.add(glassPanel);
    myPanel.add(Box.createRigidArea(new Dimension(0,450)));


    ContentPanel.setSize(650, 30);
    ContentPanel.setBounds(0,750,650,30);
    ContentPanel.setBackground(new Color(255,122,122,20));
    ContentPanel.add(myJLabel);

    myPanel.add(ContentPanel);
    myPanel.revalidate();
    myPanel.repaint();

    undoPanel = myPanel;
    myJFrame.add(undoPanel);
    myJFrame.revalidate();
}

Now it behaves as expected:

enter image description here

mZed
  • 339
  • 2
  • 7
0

BorderLyout would make it easier to implement.
Note the comments in the following mre:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main {

    private static JFrame myJFrame;

    public static void main(String[] args)  {

        myJFrame = new JFrame();
        myJFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        myJFrame.setLocationRelativeTo(null);
        showUndoPanel();
        myJFrame.pack();
        myJFrame.setVisible(true);
    }

    public static void showUndoPanel() {

        JPanel myPanel = new JPanel();
        myPanel.setBackground(new Color(255,255,255,0));
        myPanel.setLayout(new BorderLayout());

        JPanel glassPanel = new JPanel(); //uses FlowLayout by default
        //glassPanel.setSize(650, 650); //use preferred size
        glassPanel.setPreferredSize(new Dimension(650, 650));
        //glassPanel.setBounds(0,0,650,550);   //no need to set bounds. bounds are set by the layout manager

        glassPanel.setBackground(new Color(255,122,122,0));
        myPanel.add(glassPanel, BorderLayout.CENTER);

        JPanel contentPanel = new JPanel(); //uses FlowLayout by default
        //contentPanel.setSize(650, 30);//use preferred size
        contentPanel.setPreferredSize(new Dimension(650, 30));
        //contentPanel.setBounds(0,750,650,30);   //no need to set bounds. bounds are set by the layout manager
        contentPanel.setBackground(new Color(255,122,122,20));
        JLabel myJLabel = new JLabel("Great Job!");
        contentPanel.add(myJLabel);

        myPanel.add(contentPanel, BorderLayout.SOUTH);
        myJFrame.add(myPanel);
    }
}
c0der
  • 18,467
  • 6
  • 33
  • 65