Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to change JPanel color from another class?

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to change JPanel color from another class?

    I am trying to change the color of mainPanel from a different class. I have a Menu to select a radio button(black background or blue background) that is supposed to change the color of my mainPanel however, I am newer to Java and I have no idea what I am doing wrong. The code is below:


    import java.awt.BorderLayout;
    import java.awt.Color;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
     
    public class MainBoard extends JFrame{
     
    	private JPanel mainPanel;
     
    	public static void main(String[] args) {
    		MainBoard mb = new MainBoard();
    		mb.setVisible(true);
    		mb.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    	}
     
    	public MainBoard() {
    		setTitle("Tic-Tac-Toe");
    		setSize(600,600);
     
    		mainPanel = new JPanel(new BorderLayout());
    		mainPanel.setBackground(Color.BLACK);
    		add(mainPanel);
     
    		Menu menu = new Menu();
    		setJMenuBar(menu.getMenu());
    	}
     
    	public JPanel getMainPanel() {
    		return mainPanel;
    	}
     
    	public void setMainPanel(Color c) {
    		this.mainPanel.setBackground(c);
    	}
    }


    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.ButtonGroup;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JRadioButtonMenuItem;
     
    public class Menu extends JMenu{
    	private JMenuBar menu;
    	private JMenu file;
    	private JMenu editGUI;
    	private JMenuItem newGame;
    	private JRadioButtonMenuItem blackBackground;
    	private JRadioButtonMenuItem blueBackground;
    	private JMenuItem exit;
     
    	public Menu() {
    		menu = new JMenuBar();
     
    		file = new JMenu("File");
    		exit = new JMenuItem("Exit");
    		newGame = new JMenuItem("New Game");
    		menu.add(file);
    		file.add(newGame);
    		file.add(exit);
     
    		editGUI = new JMenu("Edit GUI");
    		ButtonGroup bg = new ButtonGroup();
    		blackBackground = new JRadioButtonMenuItem("Black Background",false);
    		blueBackground = new JRadioButtonMenuItem("Blue Background",false);
    		bg.add(blueBackground);
    		bg.add(blackBackground);
    		menu.add(editGUI);
    		editGUI.add(blackBackground);
    		editGUI.add(blueBackground);
     
    		MenuHandler mh = new MenuHandler();
    		exit.addActionListener(mh);
    		blueBackground.addActionListener(mh);
     
    	}
     
    	public JMenuBar getMenu() {
    		return this.menu;
    	}
     
    	private class MenuHandler implements ActionListener {
     
    	    public void actionPerformed(ActionEvent e){
    	        if(e.getSource() == exit) {
    	            System.exit(0);
    	        }else if(e.getSource() == blueBackground) {
    	            MainBoard mb = new MainBoard();
    	            mb.setMainPanel(Color.BLUE);
    	        }
    	    }
    	}
    }

    The exit menu item will work, but the blueBackground menu item will not. Thank you for any help!
    Last edited by uk1992; March 19th, 2019 at 09:58 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to change JPanel color from another class?

    Can you add the import statements so that the code can be compiled and executed for testing?

    The code needs to access the currently shown mainPanel object to change it. The code is creating a new MainBoard object with its own mainPanel object. The Menu object needs a reference to the existing MainBoard object so it can call its setMainPanel method.

    Also mainPanel should not be static.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to change JPanel color from another class?

    No problem!

Similar Threads

  1. How to change color in blue j
    By RITESH92 in forum Java IDEs
    Replies: 2
    Last Post: March 21st, 2018, 02:23 PM
  2. Replies: 2
    Last Post: June 9th, 2014, 04:06 PM
  3. How do I change the color of my cursor?
    By Chokladmunken in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 17th, 2014, 10:54 AM
  4. Trying To Change The Color Of JScrollBar
    By Damian3395 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 4th, 2013, 06:40 PM
  5. Change font color and size
    By javanovice in forum AWT / Java Swing
    Replies: 2
    Last Post: April 20th, 2010, 09:57 AM

Tags for this Thread