Home > Software design >  How to add to a JPanel from another class
How to add to a JPanel from another class

Time:04-17

I've been spending the past few hours trying to figure out how to use a panel in my GUI class from a class named algorithms. I know this question has been asked before, But I cant seem to figure out how to incorporate it into my code, so I would appreciate any form of guidance.

Specifically, in my algorithms class I was testing trying to change the JLabel named title by creating a instance of my GUI class and then trying to use title.setText(), however it returns a Null Pointer Exception.

The title JLabel is located on my JPanel named topPanel

I know I'm not doing something right, I've struggled with this for the past few hours and cant seem to make any progress. Like I said, any guidance would be appreciated.

GUI.java

import javax.swing.*;
import java.awt.*;
import java.util.Date;

public class GUI extends JFrame {
    public JFrame myFrame;
    public JPanel firstFitDisplay,topPanel;
    public JLabel title;
    public Timer timer;
    public int count=0;


    public GUI(){



    }


    public void initGUI(){


        JFrame myFrame = new JFrame();
        myFrame.setTitle("CIS 452 Dynamic Memory Allocation Project");
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setResizable(true);
        myFrame.setLayout(new BorderLayout(6,6));
        myFrame.setSize(1000,700);
        myFrame.setVisible(true);
        //myFrame.getContentPane().setBackground(Color.white);

        //setup panels
        JPanel infoPanel = new JPanel();
        JPanel centerPanel = new JPanel();
        JPanel topPanel = new JPanel();
        JPanel bottom = new JPanel();
        infoPanel.setBackground(Color.cyan);
        centerPanel.setBackground(Color.red);
        topPanel.setBackground(Color.yellow);
        bottom.setBackground(Color.pink);
        infoPanel.setPreferredSize(new Dimension(200,1000));
        centerPanel.setPreferredSize(new Dimension(500,500));
        topPanel.setPreferredSize(new Dimension(750,20));
        bottom.setPreferredSize(new Dimension(750,25));



        //setup layout for panels, so that we can add subpanels
        infoPanel.setLayout(new BoxLayout(infoPanel,BoxLayout.PAGE_AXIS));

        //add panels to main frame
        myFrame.add(infoPanel,BorderLayout.WEST);
        myFrame.add(centerPanel,BorderLayout.CENTER);
        myFrame.add(topPanel,BorderLayout.NORTH);
        myFrame.add(bottom,BorderLayout.SOUTH);


        // setup sub panels for infoPanel
        JPanel infoSubPanel = new JPanel();
        infoSubPanel.setBackground(Color.pink);
        infoSubPanel.setPreferredSize(new Dimension(50,90));
        infoPanel.add(infoSubPanel);
        //setting up text for infoSubPanel
        JLabel subPanelTitle = new JLabel("Next Process Size to be allocated");
        JLabel firstFitNextUpLabel = new JLabel("First:0");
        JLabel bestFitNextUpLabel = new JLabel("Best:0");
        JLabel worstFitNextUpLabel = new JLabel("Worst:0");
        infoSubPanel.add(subPanelTitle);
        infoSubPanel.add(firstFitNextUpLabel);
        infoSubPanel.add(bestFitNextUpLabel);
        infoSubPanel.add(worstFitNextUpLabel);
        //subClockPanel
        JPanel infoSubClockPanel = new JPanel();
        infoSubClockPanel.setBackground(Color.GRAY);
        infoSubClockPanel.setPreferredSize(new Dimension(50,90));
        infoPanel.add(infoSubClockPanel);

        //setting up text for sub clock panel
        JLabel clockText = new JLabel("Seconds passed: ");
        int ten = 10;
        JLabel clockCounter = new JLabel("0");
        infoSubClockPanel.add(clockText);
        infoSubClockPanel.add(clockCounter);


        //logic for running timer;
        timer = new Timer(1000, e -> {
            clockCounter.setText(String.valueOf(count  ));
        });
        timer.start();

        //setting up sub panels for the main center panel
        centerPanel.setLayout(new FlowLayout());
        JPanel firstFitDisplay = new JPanel();
        JPanel bestFitDisplay = new JPanel();
        JPanel worstFitDisplay = new JPanel();
        firstFitDisplay.setPreferredSize(new Dimension(200,500));
        bestFitDisplay.setPreferredSize(new Dimension(200,500));
        worstFitDisplay.setPreferredSize(new Dimension(200,500));
        centerPanel.add(firstFitDisplay);
        centerPanel.add(bestFitDisplay);
        centerPanel.add(worstFitDisplay);
        //center components
        centerPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
        firstFitDisplay.setAlignmentX(Component.CENTER_ALIGNMENT);


        //setup title
        JLabel title = new JLabel("Dynamic Memory Allocation Simulator");
        topPanel.add(title);



    }
}


and here is algorithms.java

import javax.swing.*;
import java.awt.*;

public class algorithms {

    public static void main(String[] args) {
        GUI f = new GUI();
        f.initGUI();
        f.title.setText("HHHHHHHHHHH");
    }



}

CodePudding user response:

so your mistake is that you never define title in your GUI class. I think you intended to with this line:

JLabel title = new JLabel("Dynamic Memory Allocation Simulator");

This actually creates a new local variable called title instead of defining the global one. So the global variable is still null. To fix this simply change that to like to this:

this.title = new JLabel("Dynamic Memory Allocation Simulator");

Now it will define the global variable! This shouldn't give a null pointer exception now.

  • Related