Reputation:
I'm writing a large Java desktop program in swing and I'm using one JFrame and around 30 JPpanels in CardLayout
There are several panels that share the same side menu panel.
In the prototype of the program, I wrote this menuPanel inside each of these JPanels. However, I realized that the code was very redundant, so trying to optimize it, I created this MenuPanel class as a JPanel and I added it to all of those other JPanels. And then I added these JPanels to the CardLayout.
When I run the program, the side panel doesn't show because in Eclipse Design Parser it says MenuPanel is added to more than one parent component.
First of all, do you think this is a good design i.e. to use CardLayout? Or do you think I should use one JFrame and one JPanel where I keep updating the JPanel (removing Components and then adding others) each time it's needed?
Second of all, is there a workaround to solve the issue that I presented above?
The code for the MenuPanel is here:
import java.awt.Font;
import javax.swing.*;
public class MenuPanel extends JPanel {
private static final long serialVersionUID = 1L;
public static JLabel lblPicture = new JLabel("New label");
public static JLabel lblName = new JLabel("Fname Lname");
public static JButton btnCourses = new JButton("Courses");
public static JButton btnChat = new JButton("Chat");
public static JButton btnSchedule = new JButton("Schedule");
public static JButton btnProfile = new JButton("Profile");
public static JButton btnAdvancedTools = new JButton("Advanced Tools");
public static JButton btnSignOut = new JButton("Sign Out");
public static JLabel lblWelcomeBack = new JLabel("Welcome Back");
public MenuPanel() {
initializeComponents();
}
private void initializeComponents() {
this.setLayout(null);
this.setVisible(true);
this.setBounds(0, 0, 129, 562);
lblPicture
.setIcon(new ImageIcon(getClass().getResource("Profile.png")));
lblPicture.setBounds(0, 11, 124, 128);
this.add(lblPicture);
lblName.setHorizontalAlignment(SwingConstants.CENTER);
lblName.setFont(new Font("Tahoma", Font.BOLD, 12));
lblName.setBounds(10, 150, 115, 20);
this.add(lblName);
btnCourses.setBounds(10, 195, 110, 25);
this.add(btnCourses);
btnChat.setBounds(10, 240, 110, 25);
this.add(btnChat);
btnSchedule.setBounds(10, 285, 110, 25);
this.add(btnSchedule);
btnProfile.setBounds(10, 325, 110, 25);
this.add(btnProfile);
btnAdvancedTools.setBounds(10, 370, 110, 25);
this.add(btnAdvancedTools);
btnSignOut.setBounds(10, 515, 110, 25);
this.add(btnSignOut);
}
}
And here is the code for one of the many panels that use this MenuPanel:
import java.awt.*;
import javax.swing.*;
public class MainPagePanel extends JPanel {
private static final long serialVersionUID = 1L;
protected static JPanel AppPanel = new JPanel();
protected static MenuPanel menuPanel = new MenuPanel();
protected static JLabel lblWelcomeBack = new JLabel("Welcome Back");
public MainPagePanel() {
initializeComponents();
}
private void initializeComponents() {
this.setLayout(null);
AppPanel.setLayout(null);
AppPanel.add(menuPanel, 0, 0);
AppPanel.setBackground(new Color(173, 216, 230));
AppPanel.setBounds(129, 0, 471, 562);
lblWelcomeBack.setHorizontalAlignment(SwingConstants.CENTER);
lblWelcomeBack.setFont(new Font("Tahoma", Font.BOLD, 55));
lblWelcomeBack.setBounds(10, 215, 435, 140);
AppPanel.add(lblWelcomeBack);
this.add(AppPanel);
}
}
I'm not going to include the class that has the CardLayout in it because it's large. Anyway, any help is appreciated.
Upvotes: 0
Views: 1609
Reputation: 1617
In my case It happen because i created new JLabel inside method and then called the method few times and added the component to the panel.
so WindowBuilder plugin thought i added the same component few times, which is incorrect.
As solution I finally created the JLabel outside the method, and method was only modify it.
Upvotes: 0
Reputation: 64
Glancing over the code, I noticed you set layouts to null. This would definitely cause some issues to occur. A Visual Guide to Layout Managers is very useful in trying to set up a Swing interface. Try fixing the null layouts and then see if that resolves the issue. I would also recommend the GroupLayout or GridBagLayout for a complex layout. They might be difficult to learn how to use at first, but will definitely help you resolve your issues without forcing layouts to be null or sizing components haphazardly with setMin/Pref/Max.
Upvotes: 2