Two Different Panels in One Frame - Java - java

I have a question.
I have one main frame, and to the left i have a sidebar with menus.
My question is, is it possible to make another panel within the main frame,
so that if menu1 is clicked, the related contents should be displayed to the second half of the main frame, and when other menus are pressed then obviously the relevant stuff according to what is selected. its a bit hard to explain, sorry. Has anyone got an idea, whether that is possible in Java with Eclipse?

yes this's pretty possible you have look at CardLayout, this LayoutManager could be provide the simple way how to implement switching betweens JPanel in the JFrame

Yes, you can add 2 JPanels to 1 frame.
JFrame frame = new JFrame();
JPanel pane1 = new JPanel();
JPanel pane2 = new JPanel();
frame.add(pane1, BorderLayout.WEST);
frame.add(pane2, BorderLayout.EAST);

Related

How to instance a JPanel into panel in Jframe

Sorry if I might mix terms up. So I have a basic application where I would press the button and Jpanel1 with a label in it, would then switch/replace to JPanel2 that'll have a picture in it, all within the panel.
The JPanel inside the box would change from Jpanel1 to JPanel 2 after pressing the button. So is their a way to instance a JPanel in a panel or JFrame? I can't find the method on how to fill the panel with the JPanel.
frame.getContentPane().add(panel, /*layout.location*/);
You mean add
The exact way to do this depends on the LayoutManager you use. I suggest checking out the Visual Guide to LayoutManagers. For example, if you use a BorderLayout, you can add a panel to the center and then replace it with a different panel when the user clicks a button.
I ended using layeredPane with a card layout. Then I placed a panel using the center Border Layout, then instanced the JPanel as the panel from a different class.
Picture of Windowbuilder layout
panelPPBrowser = new JPanel();
layeredPane.add(panelPPBrowser, "name_216881785358769");
panelPPBrowser.setLayout(new BorderLayout(0, 0));
panelBrowser panelBrowserCon = new panelBrowser();
panelPPBrowser.add(panelBrowserCon, BorderLayout.CENTER);

JPanel button is not at the correct place

i made a custom JFrame for the desktop application and i added a JPanel on the very top of the app to serve as a subtitute of the title box. the problem is when i added a button it located right in the middle of the JPanel instead of the usual left top. AND it would not move even if i set it at a different location.
here is the code:
JFrame f = new JFrame("Hello");
f.setResizable(true);
JPanel pa = new JPanel();
JButton btn = new JButton("Exit");
btn.setBackground(Color.white);
btn.setText("Button");
btn.setSize(300, 80);
btn.setLocation(50, 0);
pa.setBackground(Color.red);
pa.setPreferredSize(new Dimension(width,60));
pa.add(btn);
f.setBackground(Color.white);
f.setUndecorated(true);
f.getContentPane().add(pa, BorderLayout.NORTH);
f.setSize(new Dimension(width,height));
f.setLocation(200, 200);
f.setVisible(true);
You use a BorderLayout in the frame. You can do the same thing in the panel.
pa.setLayout(new BorderLayout());
pa.add(btn, BorderLayout.WEST);
In general, setLocation tends to fight against the layout manager, so you usually don't want to use it unless you're going to position everything by hand.
that is one way to do it, but BorderLayout way is not very good way because i also want to add another button next to it.
Then what this might need is a FlowLayout using FlowLayout.LEADING as the alignment.
But as general tips:
Provide ASCII art or a simple drawing of the intended layout of the GUI (showing all components) at minimum size, and if resizable, with more width and height - to show how the extra space should be used.
For better help sooner, post a
Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example of your attempt.

Add a shared button across JTabbedPane

Hey everyone I've been using this as my guide and the base for my code that I've been working on:
Java Source
What I want to do, is add a shared button across all of the panes. I dont want to declare a unique button for each one, but one that is shared. My first thought was to change the frame to boxlayout and just toss a button in after it adds the pane to the frame:
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BoxLayout(frame, BoxLayout.PAGE_AXIS));
//Add content to the window.
//frame.add(new GUI(), BorderLayout.CENTER);
frame.add(new GUI());
//setup Find button
//findButton.setSize(110,55);
findButton.setText("Find");
findButton.setVisible(true);
//add button to frame
frame.add(findButton);
However, I get a runtime error: BoxLayout can't be shared. So now I wind up here. While I look into why I'm getting this error, can someone let me know if this is the right approach?
Suggestions:
Consider placing the JButton in a JPanel that is below or above the JTabbedPane so that it is always visible, and you will only need one button.
Or if it must be in a component held in the tabs, then each will need its own unique JButton, but they can share the same Action, which is what I recommend you do: Create an inner private class that extends AbstractAction, create an instance of this inner class, pass it into each JButton via either the JButton's constructor or its setAction(...) method.
Your BoxLayout problem is completely unrelated to your original question and should not even be part of this discussion. Yes, a BoxLayout must be used in one container, and that same container should be passed into the BoxLayout. Likely you're adding it to the JFrame, but in reality this adds it to the JFrame's contentPane, and so for this to work, you must pass frame.getContentPane() into the BoxLayout's first constructor parameter:
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));
This is one reason I don't like adding components or setting layouts directly on the top level window since it is nothing but misleading syntactic sugar.
I much prefer:
JPanel contentPane = (JPanel) frame.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));

How to visualize missing Inner Panels?

I am trying to form the following Frame:
The main Frame uses a BorderLayout.
Into at this Frame and I added a Panel which uses BoxLayout - I'll call it P1.
For some reason the Panels I add into P1 are not seen when I run the program.
What's even more confusing is that if P1 uses a GridLayout instead of a BoxLayout, all the Panels I added into P1 are shown.
The EventPanel Class used in the code extends Panel and uses a SpringLayout.
Any ideas how to make P1 work?
Here's the relevant code:
public static void main(String[] args)
{
//Setting Frame
JFrame m_CalendarFrame = new JFrame();
m_CalendarFrame.getContentPane().setLayout(new BorderLayout());
//Setting inner Panel
JPanel P1 = new JPanel();
P1.setLayout(new BoxLayout(P1, BoxLayout.Y_AXIS));
EventPanel Ev = new EventPanel("8:00", "16:00", "Bday", "Go party!");
EventPanel Ev2 = new EventPanel("8:00", "16:00", "Java", "Handing Java Project");
//Adding Two pannels into previous inner Panel
P1.add(Ev);
P1.add(Ev2);
m_CalendarFrame.getContentPane().add(P1, BorderLayout.NORTH);
m_CalendarFrame.setVisible(true);
m_CalendarFrame.setSize(800,800);
}
I'm guess the problem is in your panel that uses the SpringLayout. Start by reading the Swing tutorial on How to Use SpringLayout for working examples and explanations.
If you still need help then post your SSCCE that demonstrates your problem. And start simple. Try using the SpringLayout with one comopnent, then two, then three until you feel more comforatable with it.
I believe it has to do with free space management, but I haven't tested it myself. The BoxLayout uses the preferred size for the components it draws, and the GridLayout distributes the available space evenly among the objects drawn.
What happens if you specify a preferred size for your EventPanel? Based on your code example, you can quickly test this by forcing a preferred size of (800,400) for your EventPanel instances, and see if it does change something when using the BoxLayout.

How do I add space to a JPanel, so that JScrollPane doesn't sit on top of my components?

I have a JScrollPane and when I load my application the bar is sitting on top of one of my buttons. What I would like to do is add some space to the side of my button so that the scroll bar draws over the space and not my button.
Example code that I tried:
JPanel eButton = new JPanel(new BorderLayout());
JPanel spaceFiller = new JPanel();
spaceFiller.setSize(30, 10);
eButton.add(editButton, BorderLayout.EAST);
eButton.add(spaceFiller, BorderLayout.WEST);
The problem with this code is that it still overwrites my button and no space is added. What is the best way to make sure that JScrollPane doesn't overlap the components in my JFrame?
Thanks
To ensure that the size of the JPanel is respected you should use setPreferredSize() instead of setSize().
In your sample code, didn't you reverse EAST and WEST? Shouldn't it be like:
eButton.add(editButton, BorderLayout.WEST);
eButton.add(spaceFiller, BorderLayout.EAST);
That would make more sense, sicne the scrollbar will appear on the right side (EAST).
Please note that the solution you suggest, even though it may work (after exchanging EAST and WEST) looks more like a hack than a real solution.

Categories