http://Bj83.de

mdi windows / frames in java

double click into source to “convert” it to text, and Ctrl-A, Ctrl-C to copy whole “file” content at once

// MainFrame.java
package de.atm.gui;

import java.awt.Toolkit;
import java.awt.Component;
import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JDesktopPane;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.DefaultDesktopManager;

import net.miginfocom.swing.MigLayout;

/**
 * parent window as JFrame
 * @author mike
 */
public class MainFrame extends JFrame{
    private static final long serialVersionUID = -8589404623048304139L;

	//using Desktoppane for non-maximised windows
	final private JDesktopPane desk = new JDesktopPane();

	/**
	 * constructor opens (parent)window
	 * @param args
	 */
	public static void main(String[] args) {
		new MainFrame();
	}

	/**
	 * draw JFrame window 
	 */
	public MainFrame(){
		super("DummyWindow");

		// set window icon (folder have to be in the src folder)
		setIconImage(Toolkit.getDefaultToolkit().getImage(MainFrame.class.getResource("/res/main_16.png")));

		// set interface styles to os defaults
		try {
		      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) { }

		// exit programm if window closed
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

		// set startposition and size
		setBounds(200, 200, 1100, 800);

		// resizeable & visible
		setResizable(true);
		setVisible(true);

		// Toolbar with buttons ++
		// Bar itself
		JToolBar toolBar = new JToolBar();
		// no drag an repostioning
		toolBar.setFloatable(false);
		// position tabs vertical instead of horizontal
		toolBar.setOrientation(SwingConstants.VERTICAL);
		// add bar to the window
		getContentPane().add(toolBar, BorderLayout.WEST);

		// fake buttons
		JButton btnDummy1 = new JButton("Dmy1");
		JButton btnDummy2 = new JButton("Dmy2");

		// pannel for positioning
		JPanel panel = new JPanel();
		toolBar.add(panel);
		// use miglayout for "manual" positions (miglayout15-swing lib)
		panel.setLayout(new MigLayout("", "[]", "[][][][][][][][]"));
		// add buttons to panel
		panel.add(btnDummy1, "cell 0 11,growx");
		panel.add(btnDummy2, "cell 0 13,growx");
		// Toolbar with buttons --

		// background and bg-color for mdi area and window
		getContentPane().setBackground(new java.awt.Color( 184, 207, 229));
		desk.setBackground(new java.awt.Color( 184, 207, 229));
		// add the desktop pane to the window
		getContentPane().add(desk);

		DefaultDesktopManager deskmn = new DefaultDesktopManager();
		desk.setDesktopManager(deskmn);

		// add menubar
		setJMenuBar(MenuBar());

		// add pop menu
		addPopup(getContentPane(), PopUpMenu());
	}

	/**
	 * adds an entry to the menue
	 * @return menue option including menue items
	 */
	private JMenu addHilfeEintrag(){
		// menue option
		JMenu mnHilfe = new JMenu("?");
		// menue item
		JMenuItem miHilfe = new JMenuItem("Hilfe");
		JMenuItem miVersion = new JMenuItem("Version");
		// add some click action to an item
		miVersion.addActionListener(new ActionListener() {
			// listen for every action
			public void actionPerformed(ActionEvent e) {
				// call a new window
				desk.add(VersionFrame.getWindow());
			}
		});
		// add the menue items to the menue option
		mnHilfe.add(miHilfe);
		mnHilfe.add(miVersion);
		// return the whole thing
		return mnHilfe;
	}

	/**
	 * adds an entry to the menue
	 * @return menue option including menue items
	 */
	private JMenu addDateiEintrag(){
		// menue option
		JMenu mnDatei        = new JMenu("File");
		// menue item
		JMenuItem miSettings = new JMenuItem("Settings");
		JMenuItem miExit     = new JMenuItem("Quit");
		// add some click action to an item
		miExit.addActionListener(new ActionListener() {
			// listen for every action
			public void actionPerformed(ActionEvent e) {
				// close the whole window
				dispose();
			}
		});
		// add the menue items to the menue option
		mnDatei.add(miSettings);
		mnDatei.add(miExit);
		// return the whole thing
		return mnDatei;
	}

	/**
	 * add entries to the menue bar
	 * @return menue bar element
	 */
	private JMenuBar MenuBar(){
		// create new bar
		JMenuBar mb = new JMenuBar();

		// add entries from function(s)
		mb.add(addDateiEintrag());
		mb.add(addHilfeEintrag());

		// returns the whole bar including entries
		return mb;
	}

	/**
	 * creates a popupmenue
	 * @return popup menue element
	 */
	private JPopupMenu PopUpMenu(){
		// create a new one
		JPopupMenu pm = new JPopupMenu();
		// add entries from function(s)
		mb.add(addDateiEintrag());
		mb.add(addHilfeEintrag());
		// return menue with entries
		return pm;
	}

	/**
	 * creates visual component of popup menue
	 * (automatic generation or 1:1 copy)
	 * @param component
	 * @param popup
	 */
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
// VersionFrame.java
package de.atm.gui;

import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.ImageIcon;
import javax.swing.JInternalFrame;

/**
 * creates mdi/child window
 * @author mike
 *
 */
public class VersionFrame {
    /**
     * direct call to get a new window
	 * -
	 * for the eclipse window designer
	 * @wbp.parser.entryPoint
	 */
	public static JInternalFrame getWindow(){
		final JInternalFrame iFrame = new JInternalFrame("Versioninfo");

		// set window icon (folder have to be in the src folder)
		iFrame.setFrameIcon(new ImageIcon(VersionFrame.class.getResource("/res/child_16.png")));

		// not used in this scenario but possible for window
//		iFrame.setIconifiable(true);
//		iFrame.setMaximizable(true);
//		iFrame.setResizable(true);
//		iFrame.setClosable(true);
//		iFrame.setSize(120, 120);
		// set starting location and size
		iFrame.setBounds(100, 100, 200, 200);
		// when window close, dispose class
		iFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		// new panel for content
		JPanel jp = new JPanel();
		// set background color for window
		jp.setBackground(SystemColor.window);

		// multiline labe for displaying text
		JTextArea jt = new JTextArea();
		// workaround to prevent "white" background on label
		jt.setBackground(SystemColor.window);
		// set location and size because default layout is fixed style
		jt.setBounds(5, 5, 190, 120);
		// text to be shown
		jt.setText("Some\nMultilined\n\nText by:\nMichael Attenberger");
		// set font color to black
		jt.setForeground(new java.awt.Color( 0,0,0));

		// a new button
		JButton jb = new JButton();
		// set location and size (remember default style is fixed)
		jb.setBounds(5, 130, 180, 35);
		// caption of button
		jb.setText("close");
		// react to any action
		jb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// on click close this (child) window
				iFrame.dispose();
			}
		});

		// to be safe (in different workspaces maybe different styles are default) set style to fixed 
		jp.setLayout(null);

		// add label and button to panel
		jp.add(jt);
		jp.add(jb);
		// add panel to (child)window
		iFrame.getContentPane().add(jp);
		// finally make the window visible
		iFrame.setVisible(true);

		// return the whole window to parent process
		return iFrame;
	}
}

miglayout libary (to reduce search time 😉 )

miglayout15-swing

just rename the extionsion from .zip to .jar

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: