Java: Swing – access to an JPanel on JInternalFrame and change background color

01

02

PDFPanel

package de.professional_webworkx.blog.pdfinternalframes.panels;

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Patrick Ott 
 * @version 1.0
 */
public class PDFPanel extends JPanel {

    public PDFPanel() {
        this.setBackground(Color.gray);
        this.setSize(new Dimension(200, 200));
        this.add(new JLabel("PDF JPanel"));
    }
    
    public void setDefaultColor(Color color) {
        this.setBackground(color);
    }
}

MainDesktop

package de.professional_webworkx.blog.pdfinternalframes.desktop;

import de.professional_webworkx.blog.pdfinternalframes.panels.PDFPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;

/**
 *
 * @author Patrick Ott 
 * @version 1.0
 */
public class MainDesktop extends JFrame {

    private JDesktopPane desktopPane;
    private JPanel btnPanel;
    private JButton addInternalFrame;
    private JButton categoryActionBtn;
    
    public MainDesktop() {
        
        initComponents();
        
        this.setTitle("JDesktop Pane");
        this.setSize(new Dimension(1024, 768));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.getContentPane().add(btnPanel, BorderLayout.NORTH);
        this.getContentPane().add(desktopPane, BorderLayout.CENTER);
        this.setVisible(true);
    }

    private void initComponents() {
        
        btnPanel = new JPanel(new GridLayout(1, 2));
        desktopPane = new JDesktopPane();
        addInternalFrame = new JButton("add");
        addInternalFrame.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JInternalFrame internalFrame = new JInternalFrame("Title", true, true, true);
                internalFrame.setBackground(Color.red);
                internalFrame.setSize(new Dimension(300, 300));
                internalFrame.add(new PDFPanel());
                internalFrame.setVisible(true);
                desktopPane.add(internalFrame);
            }
        });
        
        categoryActionBtn = new JButton("change Background Color");
        categoryActionBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                categoryAction();
            }
        });
        
        btnPanel.add(addInternalFrame);
        btnPanel.add(categoryActionBtn);
    }
    
    private void categoryAction() {
        JInternalFrame selectedFrame = desktopPane.getSelectedFrame();
        PDFPanel pdfPanel = (PDFPanel)selectedFrame.getContentPane().getComponent(0);
        pdfPanel.setDefaultColor(Color.yellow);
    }
}

Start the App

/*
 * Copyright 2014 Patrick Ott .
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.professional_webworkx.blog.pdfinternalframes;

import de.professional_webworkx.blog.pdfinternalframes.desktop.MainDesktop;
import javax.swing.SwingUtilities;

/**
 *
 * @author Patrick Ott 
 */
public class App {

    /**
     * Startingpoint for our app
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new MainDesktop();
            }
        });
    }
    
}