0

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.

  • Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Mar 20 '15 at 02:09
  • My program doesn't "have to work on different OS'" The problem is not with the `setLayout(null);` My question is specific... –  Mar 20 '15 at 07:16
  • *"..the layout code is not the problem."* It is if you expect to get help. People won't waste effort trying to polish a turd. And given you posted a question to SO, it seems likely you want help. As to *"My program doesn't "have to work on different OS'""* Then why use Java? I'll bet it is common in .Net apps. to set control locations and sizes. – Andrew Thompson Mar 20 '15 at 07:35
  • Did you even read the question? I guess not because you want me to use your "method" –  Mar 20 '15 at 07:58
  • Why is your `protected static MenuPanel menuPanel = new MenuPanel();` static. – matt Aug 05 '20 at 13:59

2 Answers2

2

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.

pietas
  • 64
  • 5
  • Thanks for the advice. However, that isn't the problem I'm facing. In fact, the WindowBuilder in Eclipse set the layout code all by itself with those pixel values. But I should learn to use more of these Layout Managers... –  Mar 19 '15 at 22:02
  • 1
    *".. the WindowBuilder in Eclipse set the layout code all by itself with those pixel values."* WindowBuilder can be used with layouts. I don't use it or know how, but know that it can. So *stop* blaming the tools. *"But I should learn to use more of these Layout Managers..."* Yes, you should. It is pointless trying to help until the code uses layouts.. – Andrew Thompson Mar 20 '15 at 02:11
  • @AndrewThompson, again, the layout code is not the problem. –  Mar 20 '15 at 07:11
0

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.

Adir Dayan
  • 1,308
  • 13
  • 21