public class ImportImageAction extends AbstractMDIAction { public ImportImageAction(String name) { super(app, name); this.setDescription("Open Image", "Open Image"); } 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); if (image == null) throw new Exception("Bad File type"); JScrollPane pane = new JScrollPane(new ImagePanel(image)); app.addTab(pane, image, file.getName()); } } }In this example, when we open an Image file, we have the automatic message in the Message area:
public class ImportImageAction extends AbstractMDIAction { public ImportImageAction(String name) { super(app, name); this.setDescription("Open Image", "Open Image"); } public void run() throws Exception { ((GUIApplication) app).noWriteMessages(); 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); if (image == null) { ((GUIApplication) app).getMessageArea().append("Error when Opening " + file.getName(), "red", MessageLogger.DEFAULT_FONT_SIZE, MessageLogger.BOLD); } else { JScrollPane pane = new JScrollPane(new ImagePanel(image)); gui.addTab(pane, image, file.getName()); ((GUIApplication) app).getMessageArea().append("Opened " + file.getName(), "green"); } } } }In this code:
(GUIApplication) app).noWriteMessages();
code ensures that we won't have the default message for the action((GUIApplication) app).getMessageArea().append("Opened " + file.getName(), "green");
code shows the message in green whe we open an Image((GUIApplication) app).getMessageArea().append("Error when Opening " + file.getName(), "red", MessageLogger.DEFAULT_FONT_SIZE, MessageLogger.BOLD);
code shows a red message in Bold if we can't open the imageCopyright 2006-2023 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences