Add JInternalFrame in another Class

1

I have JInternalFrame for budget editing, my JDesktopPane is in a class other than the class I call JInternalFrame , how to add JInternalFrame to JDesktopPane .

Class Start

  public class Inicio {

    JFrame frmTeczGerenciamento;
    static JLabel txtStatusDeConexao = new JLabel();
    JDesktopPane panel = new JDesktopPane();
    public JDesktopPane desktopPane = new JDesktopPane();
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Inicio window = new Inicio();
                    window.frmTeczGerenciamento.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Inicio() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {

        frmTeczGerenciamento = new JFrame();
        frmTeczGerenciamento.getContentPane().setMaximumSize(new Dimension(0, 0));
        frmTeczGerenciamento.getContentPane().setLocation(new Point(1280, 1024));
        frmTeczGerenciamento.getContentPane().setFocusTraversalKeysEnabled(false);
        frmTeczGerenciamento.setExtendedState(Frame.MAXIMIZED_BOTH);
        frmTeczGerenciamento.getContentPane().setBackground(Color.WHITE);
        frmTeczGerenciamento.setTitle("Vers\u00E3o 1.0.8");
        frmTeczGerenciamento.setBounds(100, 100, 1280, 900);
        frmTeczGerenciamento.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JToolBar toolBar = new JToolBar();
        frmTeczGerenciamento.getContentPane().add(toolBar, BorderLayout.SOUTH);






        txtStatusDeConexao.setEnabled(true);
        txtStatusDeConexao.setText("Status de conex\u00E3o");
        toolBar.add(txtStatusDeConexao);

        JLabel lblNewLabel_2 = new JLabel("                                                                                       TecZ Gerencial - TecZ Sistemas");
        lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel_2.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {

            }
        });
        toolBar.add(lblNewLabel_2);


        panel.setBorder(UIManager.getBorder("List.cellNoFocusBorder"));
        panel.setBackground(new Color(176, 196, 222));
        frmTeczGerenciamento.getContentPane().add(panel, BorderLayout.CENTER);
        panel.setLayout(null);


        desktopPane.setBounds(209, 0, 1227, 792);
        panel.add(desktopPane);

        JMenuBar menuBar = new JMenuBar();
        frmTeczGerenciamento.setJMenuBar(menuBar);

        JMenu mnModulosEmDesenvolvimento = new JMenu("PCP");
        mnModulosEmDesenvolvimento.setFont(new Font("Segoe UI", Font.PLAIN, 16));
        menuBar.add(mnModulosEmDesenvolvimento);

        JMenuItem mntmGerenciar = new JMenuItem("Gerenciar");
        mnModulosEmDesenvolvimento.add(mntmGerenciar);
        mntmGerenciar.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent arg0) {
                GerenciarOrcamentos orcamento;
                orcamento = new GerenciarOrcamentos();
                orcamento.setVisible(true);
            }
        });
        mntmGerenciar.setFont(new Font("Segoe UI", Font.BOLD, 12));


    }



    public void editarOrcamento(){
        Inicio novo = new Inicio();
        EditarOrcamento internalFrame;
        try {
            internalFrame = new EditarOrcamento();
            internalFrame.setClosable(true);
            internalFrame.setIconifiable(true);
            novo.desktopPane.add(internalFrame); 
            System.out.println("teste");
            internalFrame.setVisible(true);  
        } catch (FileNotFoundException | ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }




    }


 }

Class Code ManageOrches

public class GerenciarOrcamentos extends JInternalFrame {

    private JPanel contentPane;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GerenciarOrcamentos frame = new GerenciarOrcamentos();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public GerenciarOrcamentos() {
        setResizable(false);
        setTitle("Gerenciar Or\u00E7amentos");

        setMaximizable(true);

        setBounds(100, 100, 956, 637);

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JPanel filtros = new JPanel();
        filtros.setBackground(new Color(214, 217, 223));
        filtros.setBounds(47, 0, 53, 21);
        contentPane.add(filtros);
        filtros.setLayout(null);
        String data = new SimpleDateFormat("dd/MM/yyyy",
                Locale.getDefault()).format(new Date());



        JPanel detalhes_orcamentos = new JPanel();
        detalhes_orcamentos.setLayout(null);
        detalhes_orcamentos.setBackground(UIManager.getColor("ArrowButton.background"));
        detalhes_orcamentos.setBounds(47, 275, 144, 18);
        contentPane.add(detalhes_orcamentos);

        JPanel desc_item = new JPanel();
        desc_item.setLayout(null);
        desc_item.setBackground(UIManager.getColor("ArrowButton.background"));
        desc_item.setBounds(47, 458, 127, 27);
        contentPane.add(desc_item);

        JButton btnEmitirPedido = new JButton("Chamar JInternalFrame");
        btnEmitirPedido.setBounds(160, 107, 266, 42);
        contentPane.add(btnEmitirPedido);
        btnEmitirPedido.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {

                //Chama o JInternalFrame
                Inicio novo = new Inicio();             
                novo.editarOrcamento();



            }
        });
        btnEmitirPedido.setBackground(SystemColor.activeCaption);

        JLabel lblAoApertarO = new JLabel("Ao apertar o bot\u00E3o, o JInternalFrame deve ser exibido no JDesktopPane da Classe Inicio");
        lblAoApertarO.setBounds(6, 75, 491, 27);
        contentPane.add(lblAoApertarO);

    }


}

Class Code EditBrowse

public class EditarOrcamento extends JInternalFrame{
    private JPanel contentPane;
    private static final int BUFFER_SIZE = 4096;
    String valorTotalRec = null;
    String id = null;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {

        try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    EditarOrcamento frame = new EditarOrcamento();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     * @throws FileNotFoundException 
     * @throws ParseException 
     */
    public EditarOrcamento() throws FileNotFoundException, ParseException {
        setTitle("Editar Or\u00E7amento - Tratermik Metais");
        setMaximizable(true);

        setBounds(100, 100, 956, 637);


        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JSeparator separator_1 = new JSeparator();
        separator_1.setBounds(568, 106, 372, 1);
        contentPane.add(separator_1);

        JLabel lblProduto = new JLabel("Produto");
        lblProduto.setFont(new Font("Arial", Font.PLAIN, 12));
        lblProduto.setBounds(100, 115, 83, 14);






    }


}

Comments:

  • The action is perfectly executed because the console displays the 'test' message, apparently the setvisible that is not being accepted.
  • If I call the Edit method with a button inside the Start class, the JInternalFrame is normally displayed.
asked by anonymous 25.06.2017 / 15:45

1 answer

1

After editing, I realized that what you're trying to do is open InternalFrame from another, and to do that, just recover JDesktopPane . This is possible thanks to the getParent() method, which retrieves the container that holds the current component, in its case, the inner frame GerenciarOrcamentos .

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.SystemColor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

public class GerenciarOrcamentos extends JInternalFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GerenciarOrcamentos frame = new GerenciarOrcamentos();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public GerenciarOrcamentos() {
        setResizable(false);
        setTitle("Gerenciar Or\u00E7amentos");

        setMaximizable(true);

        setBounds(100, 100, 956, 637);

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JPanel filtros = new JPanel();
        filtros.setBackground(new Color(214, 217, 223));
        filtros.setBounds(47, 0, 53, 21);
        contentPane.add(filtros);
        filtros.setLayout(null);
        String data = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault()).format(new Date());

        JPanel detalhes_orcamentos = new JPanel();
        detalhes_orcamentos.setLayout(null);
        detalhes_orcamentos.setBackground(UIManager.getColor("ArrowButton.background"));
        detalhes_orcamentos.setBounds(47, 275, 144, 18);
        contentPane.add(detalhes_orcamentos);

        JPanel desc_item = new JPanel();
        desc_item.setLayout(null);
        desc_item.setBackground(UIManager.getColor("ArrowButton.background"));
        desc_item.setBounds(47, 458, 127, 27);
        contentPane.add(desc_item);

        JButton btnEmitirPedido = new JButton("Chamar JInternalFrame");
        btnEmitirPedido.setBounds(160, 107, 266, 42);
        contentPane.add(btnEmitirPedido);
        btnEmitirPedido.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                EditarOrcamento internalFrame;
                try {
                    internalFrame = new EditarOrcamento();
                    internalFrame.setClosable(true);
                    internalFrame.setIconifiable(true);
                    getParent().add(internalFrame);
                    System.out.println("teste");
                    internalFrame.setVisible(true);
                } catch (FileNotFoundException | ParseException ex) {
                    // TODO Auto-generated catch block
                    ex.printStackTrace();
                }
            }
        });

        btnEmitirPedido.setBackground(SystemColor.activeCaption);

        JLabel lblAoApertarO = new JLabel(
                "Ao apertar o bot\u00E3o, o JInternalFrame deve ser exibido no JDesktopPane da Classe Inicio");
        lblAoApertarO.setBounds(6, 75, 491, 27);
        contentPane.add(lblAoApertarO);

    }

}

What I did there was to bring the editarOrcamento method of class Inicio to the correct place, which is in the inner frame button that starts the EditarOrcamento class. With this, the method can be deleted.

Your class Inicio is as follows, and the EditarOrcamento class has not changed.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileNotFoundException;
import java.text.ParseException;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

public class Inicio {

    JFrame frmTeczGerenciamento;
    static JLabel txtStatusDeConexao = new JLabel();
    JDesktopPane panel = new JDesktopPane();
    public JDesktopPane desktopPane = new JDesktopPane();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Inicio window = new Inicio();
                    window.frmTeczGerenciamento.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Inicio() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {

        frmTeczGerenciamento = new JFrame();
        frmTeczGerenciamento.getContentPane().setMaximumSize(new Dimension(0, 0));
        frmTeczGerenciamento.getContentPane().setLocation(new Point(1280, 1024));
        frmTeczGerenciamento.getContentPane().setFocusTraversalKeysEnabled(false);
        frmTeczGerenciamento.setExtendedState(Frame.MAXIMIZED_BOTH);
        frmTeczGerenciamento.getContentPane().setBackground(Color.WHITE);
        frmTeczGerenciamento.setTitle("Vers\u00E3o 1.0.8");
        frmTeczGerenciamento.setBounds(100, 100, 1280, 900);
        frmTeczGerenciamento.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JToolBar toolBar = new JToolBar();
        frmTeczGerenciamento.getContentPane().add(toolBar, BorderLayout.SOUTH);

        txtStatusDeConexao.setEnabled(true);
        txtStatusDeConexao.setText("Status de conex\u00E3o");
        toolBar.add(txtStatusDeConexao);

        JLabel lblNewLabel_2 = new JLabel(" TecZ Gerencial - TecZ Sistemas");
        lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel_2.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {

            }
        });
        toolBar.add(lblNewLabel_2);

        panel.setBorder(UIManager.getBorder("List.cellNoFocusBorder"));
        panel.setBackground(new Color(176, 196, 222));
        frmTeczGerenciamento.getContentPane().add(panel, BorderLayout.CENTER);
        panel.setLayout(null);

        desktopPane.setBounds(209, 0, 1227, 792);
        panel.add(desktopPane);

        JMenuBar menuBar = new JMenuBar();
        frmTeczGerenciamento.setJMenuBar(menuBar);

        JMenu mnModulosEmDesenvolvimento = new JMenu("PCP");
        mnModulosEmDesenvolvimento.setFont(new Font("Segoe UI", Font.PLAIN, 16));
        menuBar.add(mnModulosEmDesenvolvimento);

        JMenuItem mntmGerenciar = new JMenuItem("Gerenciar");
        mnModulosEmDesenvolvimento.add(mntmGerenciar);
        mntmGerenciar.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent arg0) {
                GerenciarOrcamentos orcamento;
                orcamento = new GerenciarOrcamentos();
                desktopPane.add(orcamento);
                orcamento.setVisible(true);
            }
        });
        mntmGerenciar.setFont(new Font("Segoe UI", Font.BOLD, 12));
    }
}

With this, the code works perfectly.

    
25.06.2017 / 16:40