import org.mdi.app.swing.AbstractMDIApplication; public class TabiconsTutorialMDI extends AbstractMDIApplication { public TabiconsTutorialMDI() { super("TabiconsTutorialMDI"); // initialize configuration without Preferences this.initConfiguration(); // create the Application Panels with // preparePanels(int messageAreaSize, boolean hasStatusbar, boolean hasToolBar, MDIMenuFactory mfactory) // - a Message Area of 4 rows height // - a status bar // - no tool bar // - our Menu Factory super.preparePanels(4, true, true, mfactory); // set the size of the Application window this.setSize(500, 500); } @Override public boolean writeTimeMessages() { // we will not allow to write the time of each action on the logger area return false; } public static void main(String[] args) { TabiconsTutorialMDI mdi = new TabiconsTutorialMDI(); mdi.setVisible(true); } }
import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JPopupMenu; import org.mdi.app.swing.AbstractMDIMenuFactory; public class TabiconsMenuFactory extends AbstractMDIMenuFactory { private final JMenu filemenu = new JMenu("File"); private final JMenu openmenu = new JMenu("Open"); public TabiconsMenuFactory() { } @Override protected void initMenus() { // the exit action exists already, we use it JMenuItem exitItem = new JMenuItem(getDefaultExitAction()); // create the menus filemenu.add(openmenu); filemenu.add(exitItem); // create Menu bar Mbar.add(filemenu); Mbar.add(helpmenu); } @Override public void createPopupMenu(JPopupMenu menu) { // there is only a close item for tabs JMenuItem close = this.getDefaultCloseItem(); menu.add(close); } }
JEditorPane showing the content of a text file opened in the action. The TabbedApplication.addTab(JComponent comp, SwingFileProperties fileProperties) will be used to create a tab with a JScrollPane holding this editor pane:import java.io.File; import java.net.URL; import javax.swing.JEditorPane; import javax.swing.JFileChooser; import javax.swing.JScrollPane; import org.mdi.bootstrap.swing.AbstractMDIAction; import org.mdi.bootstrap.swing.GUIApplication; import org.mdi.bootstrap.swing.SwingFileProperties; import org.mdi.bootstrap.swing.TabbedApplication; public class ImportDocumentAction extends AbstractMDIAction { public ImportDocumentAction(GUIApplication appli, String name) { super(appli, name); this.setDescription("Open Document", "Open Document"); } public void run() throws Exception { TabbedApplication gui = (TabbedApplication) app; JFileChooser chooser = new JFileChooser("Open Document"); chooser.setDialogTitle("Save Document"); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); if (chooser.showOpenDialog(gui.getApplicationWindow()) == JFileChooser.APPROVE_OPTION) { URL url = chooser.getSelectedFile().toURL(); File file = chooser.getSelectedFile(); // create a JScrollPane to show the text file JEditorPane pane = new JEditorPane(); JScrollPane scroll = new JScrollPane(pane); pane.setPage(url); // add the tab associated with the text file SwingFileProperties prop = new SwingFileProperties(file.getName(), scroll, pane.getDocument()); gui.addTab(pane, prop); } } }
ImagePanel class to show an image:import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import javax.swing.JPanel; public class ImagePanel extends JPanel { private final Image image; public ImagePanel(Image image) { super(); this.image = image; } @Override public void paint(Graphics g) { int width = image.getWidth(this); int height = image.getHeight(this); g.drawImage(image, 0, 0, width, height, this); this.setPreferredSize(new Dimension(width, height)); } }The we will use this image panel in the action to open the image. This action will create an
ImagePanel showing the content of an image file opened in the action. The TabbedApplication.addTab(JComponent comp, SwingFileProperties fileProperties) will be used to create a tab with a JScrollPane holding this image panel:import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import javax.swing.JFileChooser; import javax.swing.JScrollPane; import org.mdi.bootstrap.MDIApplication; import org.mdi.bootstrap.swing.AbstractMDIAction; import org.mdi.bootstrap.swing.SwingFileProperties; import org.mdi.bootstrap.swing.TabbedApplication; public class ImportImageAction extends AbstractMDIAction { public ImportImageAction(MDIApplication appli, String name) { super(appli, name); this.setDescription("Open Image", "Open Image"); } public void run() throws Exception { TabbedApplication gui = (TabbedApplication) app; JFileChooser chooser = new JFileChooser("Open Image"); chooser.setDialogTitle("Open Image"); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); if (chooser.showOpenDialog(gui.getApplicationWindow()) == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); BufferedImage image = ImageIO.read(file); if (image == null) { throw new Exception("Bad File type"); } JScrollPane pane = new JScrollPane(new ImagePanel(image)); SwingFileProperties prop = new SwingFileProperties(file.getName(), pane, image); gui.addTab(pane, prop); } } }
import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JPopupMenu; import org.mdi.app.swing.AbstractMDIMenuFactory; public class TabiconsMenuFactory extends AbstractMDIMenuFactory { private final JMenu filemenu = new JMenu("File"); private final JMenu openmenu = new JMenu("Open"); public TabiconsMenuFactory() { } @Override protected void initMenus() { // the exit action exists already, we use it JMenuItem exitItem = new JMenuItem(getDefaultExitAction()); ImportImageAction importImageAction = new ImportImageAction(this.getApplication(), "Import Image"); openmenu.add(importImageAction); ImportDocumentAction importDocumentAction = new ImportDocumentAction(this.getApplication(), "Import Document"); openmenu.add(importDocumentAction); // create the menus filemenu.add(openmenu); filemenu.add(exitItem); // create Menu bar Mbar.add(filemenu); Mbar.add(openmenu); } @Override public void createPopupMenu(JPopupMenu menu) { // there is only a close item for tabs JMenuItem close = this.getDefaultCloseItem(); menu.add(close); } }The result is:
ImportDocumentAction action:import java.io.File; import java.net.URL; import javax.swing.JEditorPane; import javax.swing.JFileChooser; import javax.swing.JScrollPane; import org.mdi.bootstrap.swing.AbstractMDIAction; import org.mdi.bootstrap.swing.GUIApplication; import org.mdi.bootstrap.swing.SwingFileProperties; import org.mdi.bootstrap.swing.TabbedApplication; import javax.swing.ImageIcon; public class ImportDocumentAction extends AbstractMDIAction { public ImportDocumentAction(GUIApplication appli, String name) { super(appli, name); this.setDescription("Open Document", "Open Document"); } public void run() throws Exception { TabbedApplication gui = (TabbedApplication) app; JFileChooser chooser = new JFileChooser("Open Document"); chooser.setDialogTitle("Save Document"); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); if (chooser.showOpenDialog(gui.getApplicationWindow()) == JFileChooser.APPROVE_OPTION) { URL url = chooser.getSelectedFile().toURL(); File file = chooser.getSelectedFile(); // create a JScrollPane to show the text file JEditorPane pane = new JEditorPane(); JScrollPane scroll = new JScrollPane(pane); pane.setPage(url); // add the tab associated with the text file SwingFileProperties prop = new SwingFileProperties(file.getName(), scroll, pane.getDocument()); url = TabiconsTutorialMDI.class.getResource("texticon.png"); gui.addTab(pane, new ImageIcon(url), prop); } } }We now have the following result when we open a tab:
public class TabiconsTutorialMDI extends AbstractMDIApplication { public TabiconsTutorialMDI() { super("TabiconsTutorialMDI"); // initialize configuration without Preferences this.initConfiguration(); // associate types with icons getTabTypeManager(); // here the type manager is created URL url = TabiconsTutorialMDI.class.getResource("pngicon.png"); tabTypeManager.registerIcon(url, "image"); url = TabiconsTutorialMDI.class.getResource("texticon.png"); tabTypeManager.registerIcon(url, "text"); // create the menus with the Application menu factory mfactory = new TabiconsMenuFactory(); super.preparePanels(4, true, true, mfactory); // set the size of the Application window this.setSize(500, 500); } @Override public boolean writeTimeMessages() { return false; } public static void main(String[] args) { TabiconsTutorialMDI mdi = new TabiconsTutorialMDI(); mdi.setVisible(true); } }
public class ImportDocumentAction extends AbstractMDIAction { public ImportDocumentAction(GUIApplication appli, String name) { super(appli, name); this.setDescription("Open Document", "Open Document"); } public void run() throws Exception { TabbedApplication gui = (TabbedApplication) app; JFileChooser chooser = new JFileChooser("Open Document"); chooser.setDialogTitle("Save Document"); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); if (chooser.showOpenDialog(gui.getApplicationWindow()) == JFileChooser.APPROVE_OPTION) { URL url = chooser.getSelectedFile().toURL(); File file = chooser.getSelectedFile(); JEditorPane pane = new JEditorPane(); JScrollPane scroll = new JScrollPane(pane); pane.setPage(url); SwingFileProperties prop = new SwingFileProperties(file.getName(), scroll, pane.getDocument()); prop.setType("text"); gui.addTab(pane, prop); } } }
Copyright 2006-2023 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences