Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Swing Help

Options
  • 03-04-2004 5:26pm
    #1
    Closed Accounts Posts: 2,951 ✭✭✭


    Im creating a photo application in java, using awt and swing. I have an application and i want to create a menu which will allow you to open a file in the application. Specifically i want to be able to browse for, and open up photo files e.g. jpeg, gif etc. Can someone tell me what i need to do to add functionality for opening files? i.e. file->open , how do i do that? Also i want the photo to open up inside the application in a window. Would this be hard?


    Thanks for the help


Comments

  • Closed Accounts Posts: 302 ✭✭Auburn


    To browse and open up files, Google "filechooser" or "JFilechooser".
    There's loads of examples on the java.sun.com forums too.


  • Registered Users Posts: 19,396 ✭✭✭✭Karoma


    wow. first result in this google-malarky.. http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html

    shockin' bai!


  • Registered Users Posts: 2,758 ✭✭✭Peace


    Originally posted by Karoma
    wow. first result in this google-malarky.. http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html

    shockin' bai!

    Sarcasm detector is off the scale on this one lads.


  • Closed Accounts Posts: 2,951 ✭✭✭L5


    Go easy now lads! Thanks for the help all the same!


  • Closed Accounts Posts: 2,951 ✭✭✭L5


    I have an application running with a simple menu system. I want to be able to open an image and when i open it, it will open up _inside_ the GUI. Does anyone know what kind of component i need to display the image onto? Will it display on a label?

    Thanks


  • Advertisement
  • Closed Accounts Posts: 302 ✭✭Auburn


    It could be displayed on a JLabel, JEditorPane, etc... lots of possibilities.
    Just be careful not to specify sizes - this could lead to problems because the images could all be different sizes. Let the component(s) be resized dynamically.


  • Closed Accounts Posts: 2,951 ✭✭✭L5


    Thanks for that, i thought i could use a JPanel alright. I havent figured out how to implement the action listener to open the image onto the panel though :(
    Im new to swing/awt , I managed to implement the action listener to allow me to look for a file if i click the "Open" menu, and also the "Save" button. I want to be chose an image when im opening it and for it to open to the JPanel. Any ideas how this would be done. Would i do it as an action listener for the open button?
    Heres my code at the minute:
    Thanks, and im sorry if this sounds like a stupid question, but ive been trying this for ages myself and cant get it.



    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.event.*;

    public class ImageEditor extends Frame
    implements ActionListener {
    public static void main(String args[]) {
    ImageEditor _ImageEditor__ = new ImageEditor();
    _ImageEditor__.pack();
    _ImageEditor__.show();
    }


    JMenuItem menuItem9 = new JMenuItem("Open");
    JMenuItem menuItem12 = new JMenuItem("Save");
    JMenu menu6 = new JMenu("File");
    JMenuItem menuItem10 = new JMenuItem("Undo");
    JMenu menu7 = new JMenu("Edit");
    JMenuItem menuItem11 = new JMenuItem("Topics");
    JMenuItem menuItem13 = new JMenuItem("About");
    JMenu menu8 = new JMenu("Help");
    JMenuBar menuBar5 = new JMenuBar();
    JLabel jLabel16 = new JLabel();
    Panel border1 = new Panel(new BorderLayout(5,5));

    public ImageEditor() {
    super("Image Editor");
    this.setResizable(true);
    (Cursor.DEFAULT_CURSOR));
    border1.setFont(null);
    border1.add("North",menuBar5);
    border1.add("Center",jLabel16);
    add("Center",border1);
    }

    //LOAD OF STUFF TAKEN OUT TO CUT DOWN ON SIZE


    public void actionPerformed(ActionEvent evt) {
    if ( evt.getSource() == menuItem9 ) {
    FileDialog fd = new FileDialog(_getFrame(this),
    "Choose an image to open", FileDialog.LOAD);
    fd.show();
    if( fd.getFile() == null ) return; // user pressed Cancel
    File theFile = new File(fd.getDirectory(),fd.getFile());
    }
    if ( evt.getSource() == menuItem12 ) {
    FileDialog fd = new FileDialog(_getFrame(this),
    "Choose a file to save to", FileDialog.SAVE);
    fd.show();
    if( fd.getFile() == null ) return; // user pressed Cancel
    File theFile = new File(fd.getDirectory(),fd.getFile());
    }
    if ( evt.getSource() == menuItem10 ) {
    }
    }


    public static Frame _getFrame(Component comp) {
    Component c = comp;

    try {
    while( !(c instanceof Frame) ) c = c.getParent();
    return (Frame)c;
    } catch ( NullPointerException e ) {
    return null;
    }
    }


    }


  • Closed Accounts Posts: 428 ✭✭skipn_easy


    You'll see if you look at the JFileChooser api that you can set the kind of files that you want to show when browsing, and access the file that was chosen (something like getSelectedFile). So then you can do whatever you want with the file, display it or open it or whatever.


  • Closed Accounts Posts: 2,951 ✭✭✭L5


    Excellent, got it working thanks


Advertisement