overlapping JPanels in the GUI - java

I am using BorderLayout in my application. I have a main panel to which I add two JPanels at the center. I want one of them to be transparent.
My code is :
mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.add(getGraphPaneScrollPane(), BorderLayout.CENTER);
mainPanel.add(getSituationPanel(), BorderLayout.CENTER);
Code for these two functions is :
public JScrollPane getGraphPaneScrollPane() {
if (graphPaneScrollPane == null) {
graphPaneScrollPane = new JScrollPane();
graphPaneScrollPane.setViewportView(getGraphEditorPane());
}
return graphPaneScrollPane;
}
private JScrollPane getSituationPanel(){
if(situationPanel == null){
logs.debug("Initializing Situation Panel");
situationPanel = new JScrollPane();
situationLabel = new JLabel("");
situationLabel.setVerticalTextPosition(JLabel.BOTTOM);
situationLabel.setHorizontalTextPosition(JLabel.CENTER);
situationLabel.setVerticalAlignment(JLabel.TOP);
situationLabel.setHorizontalAlignment(JLabel.CENTER);
situationLabel.setBorder(BorderFactory.createTitledBorder(""));
situationLabel.setBackground(Color.WHITE);
situationLabel.setOpaque(true);
situationLabel.setVerticalAlignment(SwingConstants.TOP);
situationPanel.setViewportView(situationLabel);
}
return situationPanel;
}
Now I want situationPanel to be transparent and getGraphPaneScrollPane to be above that in the GUI, because getGraphPaneScrollPane is the canvas, which I use to draw nodes.

I want situationPanel to be transparent and getGraphPaneScrollPane to be above that in the GUI,
The panel that is on top is the panel that needs to be transparent. If the panel on top is opaque then you will never see the panel under the top panel.
So making changes to the layout is the last thing I want.
Well that is what you are going to need to do. You can't just add two panels to one panel and expect it to work the way you want it to. Most Swing layout managers are designed to lay out components in two dimensions, not on top of one another.
Your current code is:
mainPanel.setLayout(new BorderLayout());
mainPanel.add(getGraphPaneScrollPane(), BorderLayout.CENTER);
mainPanel.add(getSituationPanel(), BorderLayout.CENTER);
You could try using the OverlayLayout, it is designed to lay out panels on top of on another. The code should be something like:
JPanel overlay = new JPanel();
overlay.setLayout( new OverlayLayout(overlay) );
overlay.add(getSituationPanel(), BorderLayout.CENTER); // add transparent panel first
overlay.add(getGraphPaneScrollPane(), BorderLayout.CENTER);
mainPanel.setLayout(new BorderLayout());
mainPanel.add(overlay);

Related

JFrame turns blue when adding tabs

I currently have a Jframe that I want to add to a tab instead.
(I used a frame just for testing purposes, to make sure the look and feel is correct, but when trying to add it to a JTabbedPane, the frame starts to look blue (weird top aswell).
I tried copying my settings from my original frame to the new frame but that did not help.
JTabbedPane tabs = new JTabbedPane();
tabs.addTab("1", frame.getContentPane());
JFrame FinalFrame = new JFrame();
FinalFrame.setDefaultLookAndFeelDecorated(true);
FinalFrame.setSize(WIDTH, HEIGTH);
FinalFrame.setLocation(100, 150);
FinalFrame.setTitle("Primal-Pvm Notification center");
FinalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FinalFrame.add(tabs);
Side by side view of the problem and the frame before adding it to the tab:
Edit: Answer by George Z. helped me out a lot.
Like he said to solve the problem:
Don't add things to your main frame but add them to a Jpanel and add that to a JTabbedPane.
If you have a Jpanel that you are adding to a tab that contains an override in the paintComponent, you have to create that class as the Jpanel so:
JPanel panel = new LineDrawer([Enter parameters]);
panel.setLayout([Enter Layout]);
The way you are approaching this seems to be pretty complex hence this weird behavior. (Looks like a look and feel problem? - show the part of the code that sets it)
However, I suggest you to create only one JFrame (this question explains why you should do that), set the layout of its content pane to BorderLayout and keep it like this. Its a rare situation to mess up with content panes. After that create independent JPanels representing the tab(s) you would like to have. Finally create a JTabbedPane with these panels and add it to the content frame of the JFrame.
A small example would be:
public class TabbedPanelExample extends JFrame {
public TabbedPanelExample() {
super("test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JTabbedPane pane = new JTabbedPane();
pane.addTab("FirstTab", createFirstTab());
pane.addTab("SecondTab", createSecondTab());
add(pane);
setSize(400, 400);
setLocationRelativeTo(null);
}
private Component createFirstTab() {
JPanel panel = new JPanel(new FlowLayout());
panel.add(new JLabel("Some Component"));
panel.add(new JTextField("Some Other Component"));
return panel;
}
private Component createSecondTab() {
JPanel panel = new JPanel(new FlowLayout());
panel.add(new JLabel("Some Component"));
panel.add(new JButton("Some Other Component"));
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new TabbedPanelExample().setVisible(true);
});
}
}
Post edit based on this comment:
Well I do have a Jframe with a lot of elements added to it so it kinda
is a hassle to switch it all to panels;
A JFrame cannot have a lot of elements. You take a look on how to use root panes. The container that "has a lot of elements" of a JFrame is its rootpane which is mostly completed by its contentpane. When you frame.add(component), you add the component to its content pane. Guess what? A JFrame's content pane is a JPanel. So are a already to panels.
Now in order to make this work, try to do as i said and frame.setLayout(new BorderLayout(); //changes layout to contentpane. Assuming you have a bunch of components (lets say comp1,comp2) and you are adding them like:
frame.add(comp1);
frame.add(comp2);
You must do the following in order to make it clear. Create a JPanel and instead of frame.add(comp1), do panel.add(comp1). So this JPanel has all the components you added in JFrame. After that create your JTabbedPane:
JTabbedPane pane = new JTabbedPane();
pane.addTab("tab", panel);
and finally add this pane to the content pane of your JFrame:
frame.add(pane);
In conclusion, you will move all the components you have added to your frame into a JPanel, add this JPanel to a JTabbedPane, and finally add this JTabbedPane to the frame (contentpane). Frame has only one component.

How to fix GUI components "leaving" their panels or taking up more space they they are supposed to?

I am trying to make a GUI for a game. I am very new to Java, especially GUI. The code below is a snippet which is supposed to make a JFrame with nested panels for organization. It works until I add buttons to the button panel. They end up on the boardBckg panel. If I manage to place them on the correct panel the JTextField disappears or it takes up the entire screen. I have been working on this part of the code for the past two days and I could really use GUI tips.
private void makeWindow()
{
boardPanel = new JPanel();
boardBckg = new JPanel();
menuPanel = new JPanel();
save = new JButton("Save");
save.setSize(Buttons);
load = new JButton("Load");
load.setSize(Buttons);
replay = new JButton ("Replay");
replay.setSize(Buttons);
words = new JTextField();
frame = new JFrame(title);
boardPanel.setSize(PANEL);
boardPanel.setMaximumSize(MAX);
boardPanel.setMinimumSize(MIN);
boardPanel.setLayout(new GridLayout(m,n));
boardBckg.setSize(1000, 1000);
boardBckg.setBackground(Color.cyan);
boardBckg.add(boardPanel, BorderLayout.CENTER);
frame.setSize(1500, 1000);
frame.setResizable(false);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BoxLayout vertical = new BoxLayout(menuPanel, BoxLayout.Y_AXIS);
menuPanel.setSize(500, 1000);
menuPanel.setBackground(Color.blue);
menuPanel.setLayout(vertical);
frame.add(boardBckg);
frame.add(menuPanel);
JPanel iGiveUp = new JPanel();
iGiveUp.setBackground(Color.black);
JPanel buttons = new JPanel();
buttons.setBackground(Color.darkGray);
buttons.add(save);
buttons.add(load);
buttons.add(replay);
menuPanel.add(iGiveUp);
menuPanel.add(buttons);
iGiveUp.add(words);
boardBckg.add(boardPanel, BorderLayout.CENTER);
The default layout of a JPanel is the FlowLayout. You can't just specify a BorderLayout constraint when you add the component to the panel.
frame.add(boardBckg);
frame.add(menuPanel);
The default layout for (the content pane of) the frame is a BorderLayout. If you don't specify a constraint, then the component is added to the BorderLayout.CENTER. Problem is only one component can be added to the CENTER so you only see the last comoponent added.
frame.setVisible(true);
Component should be added to the frame BEFORE the frame is packed and made visible. So the above statement should be the last statement in your constructor.
I have no ideas what your desired layout is but you need to start with something simple and take advantage of the default BorderLayout of the frame.
So your basic logic might be something like:
JPanel menuPanel = new JPanel()
menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.Y_AXIS));
menuPanel.add(...);
menuPanel.add(...);
JPanel center = new JPanel();
center.setLayout(...);
center.setBackground( Color.BLUE );
center.add(...);
frame.add(menuPanel, BorderLayout.LINE_START);
frame.add(center, BorderLayout.CENTER);
frame.pack();
frame.setVisible( true );
The main point is to break the panels down logically and add them to the frame one at a time. So first get the menu and its child components added to the frame is the correct position. Then you can add the CENTER panel and its child components.

Centering a fixed size canvas in a JFrame

I'm in the process of creating a circuit editor (similar to any regular paint software with a basic menu and a canvas with specifiable dimensions). I am currently trying to transform the previously unscrollable canvas (JPanel) to a scrollable one.
The obvious design error at the moment is that while the scrollbars seem to correctly reflect the internal size of the canvas (which can of course be way bigger than the JFrame), due to the canvas JPanel being added in the CENTER of the BorderLayout of the master panel, it always resizes along with the JFrame.
public final class MainFrame extends JFrame
{
public MainFrame()
{
JPanel menuPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
// Populate Menu Panel
// ...
JPanel canvasPanel = new JPanel();
canvasPanel.setBackground(Color.white);
Dimension canvasDims = new Dimension(800,600);
canvasPanel.setPreferredSize(canvasDims);
canvasPanel.setMinimumSize(canvasDims);
canvasPanel.setMaximumSize(canvasDims);
JScrollPane canvasScrollPane = new JScrollPane(
canvasPanel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JPanel masterPanel = new JPanel(new BorderLayout());
masterPanel.add(menuPanel, BorderLayout.NORTH);
masterPanel.add(canvasScrollPane, BorderLayout.CENTER);
setContentPane(masterPanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1200, 700);
setMinimumSize(new Dimension(500, 500));
setLocationRelativeTo(null);
setVisible(true);
}
I've read quite a few posts regarding centering JPanels and scrollbars but with both complexities added together, things might be a bit more complicated as I haven't yet found a solution.
What I'm really trying to achieve is to have the canvas' JPanel fixed in whatever size the user might have specified and centered in the middle as well as the scrollbars behaving as one would expect like in the beloved windows' paint:
How would you go about doing this/fixing my design? Any help would be greatly appreciated!
P.S. Happy new Year :)
JPanel fixed in whatever size the user might have specified and centered in the middle as well as the scrollbars behaving as one would expect
So you need to nest panels so the canvas panel can be displayed at its preferred size, while the parent panel resizes with the size of the frame.
An easy way to do this is with a JPanel that uses a GridBagLayout. Then you add the canvas panel to this panel using the default GridBagConstraints.
So the basic structure of the panels would be:
JPanel canvas = new JPanel();
canvas.setPreferredSize( new Dimension(300, 300) );
canvas.setBackground(Color.RED);
JPanel wrapper = new JPanel( new GridBagLayout() );
wrapper.add(canvas, new GridBagConstraints() );
frame.add(new JScrollPane(wrapper));
Note: there is no need for your "masterPanel". The default layout manager for the content pane of a JFrame is a BorderLayout, so you just add the "menuPanel" and "scrollPane" directly to the frame with the proper BorderLayout constraints.

Swing GUI design

I am newbie in Swing GUI components. I have to design a component like the image below.
I didn't know exactly what kind of gui component I have to use for this.
I am looking for suitable component like "Master & Details" panels in the image.
Panel should get the border and get the header section. In the header section I want to be place the panel name in the left as it is in the image & I want to place some labels in right side corner on this header. Please suggest me the suitable component.
Something like this:
JPanel demoPanel = new JPanel(new BorderLayout(10,10));
demoPanel.add(new JLabel("Demo"), BorderLayout.PAGE_START);
JPanel master = new JPanel(new BorderLayout());
master.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
master.add(new JLabel("Master"), BorderLayout.PAGE_START);
JPanel detail = new JPanel();
detail.add(new JLabel("Detail"), BorderLayout.PAGE_START);
detail.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
JPanel alpha = new JPanel(new BorderLayout());
alpha.add(new JLabel("Alpha (translucency)"), BorderLayout.PAGE_START);
JSlider slider = new JSlider(JSlider.HORIZONTAL);
slider.setMaximum(100);
slider.setMinimum(0);
slider.setMajorTickSpacing(10);
alpha.add(slider, BorderLayout.CENTER);
demoPanel.add(master, BorderLayout.LINE_START);
demoPanel.add(detail, BorderLayout.CENTER);
demoPanel.add(alpha, BorderLayout.PAGE_END);
It doesn't have the exact UI design (with the colors and spacing between the component) as your screenshot, but it gives you basic layout and should get you started

Including more than two Panels in a JFrame?

We are working on a project where we encountered a problem with including more than two Panels on the same JFrame .What we want is one Panel above the other.
Can the community help give an example of ho to implement this or refer me to a good tutorial or guide related to our Java Swing needs?
Assuming you want two panels added to a single frame:
Set a layout for your parent JFrame and add the two panels. Something like the following
JFrame frame = new JFrame();
//frame.setLayout(); - Set any layout here, default will be the form layout
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
frame.add(panel1);
frame.add(panel2);
Assuming you want to add one panel over the other
JFrame frame = new JFrame();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
frame.add(panel1);
panel1.add(panel2);
There is no limit on the number of panels to be added on the JFrame. You should understand that they all are containers when seen on a higher level.
if you want each of the frames/panels the same size, use the GridLayout, with a grid of 1(column) and 2(rows)
Frame myFrame;
GridLayout myLayout = new GridLayout(2,1);
myFrame.setLayout(myLayout);
Panel p1;
Panel p2;
myFrame.add(p1);
myFrame.add(p2);
if the panels are different size use the BorderLayout.... set the upper frame to "North" and the lower one to "South" or "Center"
Frame myFrame;
myFrame.setLayout(new BorderLayout() );
Panel p1;
Panel p2;
myFrame.add(p1, BorderLayout.NORTH);
myFrame.add(p2, BorderLayout.CENTER);
//you can also use card Layout, that enables you to add multiple card-panels on Main panel.
CardLayout cl;
JPanel main,one,two,three;
JButton button1,button2;
cl = new CardLayout();
main.setLayout(cl);
main.add(one,"1");
main.add(two,"2");
main.add(three,"3");
cl.show(main,"1");
public void actionPerformed(ActionEvent e){
if(e.getSource() == button1)
cl.show(main,"2");
else if(e.getSource() == button2)
cl.show(main,"3");
}

Categories

Resources