Add Jinternalframe to another Class

Asked

Viewed 172 times

1

I own a JInternalFrame for editing budgets, my JDesktopPane is in a different class from the class I call the JInternalFrame, how to add the JInternalFrame in the JDesktopPane.

Start Class

  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();
        }




    }


 }

Code of the Manage Schedules class

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);

    }


}

Code of the class Editarorcamento

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);






    }


}

Remarks:

  • The action is performed perfectly because on the console is displayed the message 'test', apparently the setvisible that is not being accepted.
  • If I call the edit method Ticking with a button inside the Start class, Jinternalframe is displayed normally.
  • Please add a [mcve] so that it is possible to run and test the problem.

  • Okay, I’ll create it now and update the question.

  • What is the main screen? How does this code run?

  • Yes, the Main is the first and is a Jframe to Manage Budgets is the second and is also a Jframe. I need to display the budget edit within the Main class and it should be called in the budget manage class.

  • It is a terrible practice to have 2 jframes. If you need more screen, you should use Jdialog.

  • In fact we are converting the whole system to Jinternalframe, it was made all with Jframe and got very bad to use.

  • Then you should convert this managerial class to jinternalframe, if you do this, it becomes easy to solve the problem. Has any problem converting it to jinternalframe?

  • I did the conversion I’ll update the code.

Show 3 more comments

1 answer

1


After editing, I realized that what you’re trying to do is open a InternalFrame from another, and to do this, just recover the JDesktopPane. This is possible thanks to the method getParent(), which recovers the container that keeps the current component, in your case the internal 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 bring the method editarOrcamento class Inicio to the correct place, which is at the bottom of the internal frame that starts the class EditarOrcamento. With this, the method can be deleted.

Your class Inicio was in the form below, and the class EditarOrcamento was 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.

  • The answer is correct, if you have problem, please inform. Negative is easy, difficult to point out the problem, if that was the reason why.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.