Calling a JInternalFrame from a JDialog
Yesterday, my friend Iswi asked me a question, how can she called a JinternalFrame from a JDialog, and put it in an existing JDesktopPane? Well Iswi, here are your answers.
First i create a very simple JFrame application
package com.edw.ui; import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import org.apache.log4j.Logger; /** * * @author edw */ public class FrameMain extends JFrame implements ActionListener { private JDesktopPane desktopPane = new JDesktopPane(); private JMenuBar jMenuBar = new JMenuBar(); private JMenu menuTest = new JMenu("Test"); private JMenuItem jMenuItem = new JMenuItem("execute"); private Logger logger = Logger.getLogger(this.getClass()); public FrameMain() { Container con = getContentPane(); con.setLayout(new BorderLayout()); con.add(desktopPane, BorderLayout.CENTER); menuTest.add(jMenuItem); jMenuBar.add(menuTest); setJMenuBar(jMenuBar); jMenuItem.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource() == jMenuItem) { logger.debug("in jmenuitem"); InternalTestOne internalTestSatu = new InternalTestOne(this); internalTestSatu.setVisible(true); getDesktopPane().add(internalTestSatu); } } public static void main(String[] args) { FrameMain frameMain = new FrameMain(); frameMain.setVisible(true); frameMain.setLocationRelativeTo(null); frameMain.setSize(800, 500); frameMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frameMain.setTitle("bubu"); } public JDesktopPane getDesktopPane() { return desktopPane; } public void setDesktopPane(JDesktopPane desktopPane) { this.desktopPane = desktopPane; } }
and this is my first JInternalFrame, it will call a JDialog class,
package com.edw.ui; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JInternalFrame; import org.apache.log4j.Logger; /** * * @author edw */ public class InternalTestOne extends JInternalFrame implements ActionListener { private Logger logger = Logger.getLogger(this.getClass()); private JButton button = new JButton("Klik Dialog"); private FrameMain frameMain = new FrameMain(); public void actionPerformed(ActionEvent e) { if(e.getSource() == button){ logger.debug("in ActionPerformed InternalTestSatu"); // show jdialog DialogTest dialogTest = new DialogTest(frameMain); dialogTest.setVisible(true); } } public InternalTestOne(FrameMain frameMain) { super("internal satu", true, true, true, true); this.frameMain = frameMain; setSize(300,300); logger.debug("in InternalTestSatu"); Container con = getContentPane(); con.setLayout(new FlowLayout()); con.add(button); button.addActionListener(this); } }
and this is my JDialog class
package com.edw.ui; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JDialog; import org.apache.log4j.Logger; /** * * @author edw */ public class DialogTest extends JDialog implements ActionListener { private JButton button = new JButton("call Internal Frame"); private Logger logger = Logger.getLogger(this.getClass()); private FrameMain frameMain = new FrameMain(); public void actionPerformed(ActionEvent e) { if(e.getSource() == button){ logger.debug("at actionPerformed "+this.getClass().getName()); // call jinternalframe from here InternalTestTwo internalTestTwo = new InternalTestTwo(); internalTestTwo.setVisible(true); frameMain.getDesktopPane().add(internalTestTwo); this.setVisible(false); this.dispose(); } } public DialogTest(FrameMain frameMain) { super(frameMain); this.frameMain = frameMain; setTitle("Calling JInternalFrame"); Container con = getContentPane(); con.setLayout(new FlowLayout()); con.add(button); setLocationRelativeTo(null); setSize(300,200); pack(); button.addActionListener(this); } }
and finally, this is my second JInternalFrame , it will be called from a JDialog
package com.edw.ui; import java.awt.Container; import java.awt.FlowLayout; import javax.swing.JInternalFrame; import javax.swing.JLabel; import org.apache.log4j.Logger; /** * * @author edw */ public class InternalTestTwo extends JInternalFrame { private JLabel label = new JLabel("Called from JDialog"); private Logger logger = Logger.getLogger(this.getClass()); public InternalTestTwo() { super("internal Two Called from JDialog", true, true, true, true); setSize(300, 300); setLocation(100,200); logger.debug("constructor of "+this.getClass().getName()); Container con = getContentPane(); con.setLayout(new FlowLayout()); con.add(label); } }
and this is how my simple app looks like,
Thanks for your questions, Iswi. Always remember, stay hungry, stay foolish.
No Comments