public void run() throws Exception { JFileChooser chooser = new JFileChooser("Open Image"); chooser.setDialogTitle("Open Image"); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); if (chooser.showOpenDialog(app.getApplicationWindow()) == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); BufferedImage image = ImageIO.read(file); // the object associated with the tab if (image == null) throw new Exception("Bad File type"); JScrollPane pane = new JScrollPane(new ImagePanel(image)); // the content of the panel to show in the tab String tabName = file.getName(); // the name of the tab app.addTab(pane, image, tabName); } }A more elaborate way to do the same thing using a FileProperties instance would be:
public void run() throws Exception { JFileChooser chooser = new JFileChooser("Open Image"); chooser.setDialogTitle("Open Image"); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); if (chooser.showOpenDialog(app.getApplicationWindow()) == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); BufferedImage image = ImageIO.read(file); // the object associated with the tab if (image == null) throw new Exception("Bad File type"); JScrollPane pane = new JScrollPane(new ImagePanel(image)); // the content of the panel to show in the tab String tabName = file.getName(); // the name of the tab SwingFileProperties properties = new SwingFileProperties(tabName, comp, image); app.addTab(properties); } }We can also want to add a tooltip on the tab, for example here to show the path of the file (here we choose not to have an Icon on the tab):
public void run() throws Exception { JFileChooser chooser = new JFileChooser("Open Image"); chooser.setDialogTitle("Open Image"); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); if (chooser.showOpenDialog(app.getApplicationWindow()) == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); BufferedImage image = ImageIO.read(file); // the object associated with the tab if (image == null) throw new Exception("Bad File type"); JScrollPane pane = new JScrollPane(new ImagePanel(image)); // the content of the panel to show in the tab String tabName = file.getName(); // the name of the tab String path = file.getAbsolutePath(); SwingFileProperties properties = new SwingFileProperties(tabName, comp, image); app.addTab(null, properties, path); } }
public class MyApplication extends AbstractMDIApplication { public MyApplication() { super("My Application"); this.hasClosableTab(true); } }
Copyright 2006-2023 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences