Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 109,157 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,059 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Open an image with Java GUI

 
Reply to this topicStart new topic

Open an image with Java GUI

dewjunkie
post 30 Jun, 2008 - 04:18 PM
Post #1


New D.I.C Head

*
Joined: 6 Mar, 2008
Posts: 32

Hey all!

I am just learning Java here and was hoping someone could give me a hand. I have made a very simple GUI and am trying to get an image to load into it. That is where I am having issues. I can get to the point where I go and choose a file off of my computer, but once I select it, it never shows up. I am sure it's just a dumb mistake somewhere I just can't see it. Thanks for any help!!!!!!

CODE

package img;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.BorderFactory;
import javax.swing.JFileChooser;

public class gui2 extends JFrame {

    public gui2(){

        this.setTitle("Name");
        JPanel content = new JPanel();
        content.setBorder(BorderFactory.createEmptyBorder(20,5,10,5));
        GridLayout layout = new GridLayout(5, 2, 5, 3);
        content.setLayout(layout);

        JMenuBar menu = new JMenuBar();
        JMenu fileMenu = new JMenu ("File");
        JMenu editMenu = new JMenu ("Edit");
        JMenu helpMenu = new JMenu ("Help");
        menu.add (fileMenu);
        menu.add (editMenu);
        menu.add (helpMenu);

        setJMenuBar(menu);

        JMenuItem open = new JMenuItem("Open");
        JMenuItem save = new JMenuItem("Save As");
        JMenuItem close  = new JMenuItem("Close");
        JMenuItem exititem = new JMenuItem("Exit");

        JMenuItem clone = new JMenuItem("Clone");
        JMenuItem invert = new JMenuItem("Invert Colors");
        JMenuItem decolor  = new JMenuItem("Decolor");
        JMenuItem threshold = new JMenuItem("Threshold");
        JMenuItem flipHoriz = new JMenuItem("Flip Horizontal");
        JMenuItem flipVert = new JMenuItem("Flip Vertical");
        JMenuItem rotateLeft  = new JMenuItem("Rotate Left");
        JMenuItem rotateRight = new JMenuItem("Rotate Right");
        JMenuItem resize  = new JMenuItem("Resize");
        JMenuItem crop = new JMenuItem("Crop");


        // Create a text area
        JTextArea jta = new JTextArea();

        // Set Mnemonics
        open.setMnemonic('O');
        save.setMnemonic('S');
        close.setMnemonic('L');
        exititem.setMnemonic('E');

        clone.setMnemonic('C');
        invert.setMnemonic('I');
        decolor.setMnemonic('D');
        threshold.setMnemonic('T');
        flipHoriz.setMnemonic('H');
        flipVert.setMnemonic('V');
        rotateLeft.setMnemonic('L');
        rotateRight.setMnemonic('R');
        resize.setMnemonic('s');
        crop.setMnemonic('o');

        //add the sub menu items to their file menu
        fileMenu.add(open);
        fileMenu.add(save);
        fileMenu.add(close);
        fileMenu.add(exititem);

        //add the sub menu items to their edit menu
        editMenu.add(clone);
        editMenu.add(invert);
        editMenu.add(decolor);
        editMenu.add(threshold);
        editMenu.add(flipHoriz);
        editMenu.add(flipVert);
        editMenu.add(rotateLeft);
        editMenu.add(rotateRight);
        editMenu.add(resize);
        editMenu.add(crop);

        // Method to open an image
        open.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                final JFileChooser fc = new JFileChooser();
                int returnVal = fc.showOpenDialog(getComponent(0));

            }
        });                

        // Method to exit out of program
        exititem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(-1);
            }
        });

        //this.setLocationRelativeTo(null);
        this.setSize(550,400);
        this.setLocation(150,150);
        //this.pack();

        // last thing we want to have happen
        this.setVisible(true);
    }

    public static void main(String[] args) {

        gui2 newGUI = new gui2();

    }

}

User is offlineProfile CardPM

Go to the top of the page


pbl
post 30 Jun, 2008 - 05:09 PM
Post #2


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 1,909



Thanked 113 times

Dream Kudos: 75
My Contributions


And where in your code are you loading an Image and displaying it (or think you do so) ?
Your JFileChooser may let you select an Image but you still have to read it and to display it (in a JLabel that is the easiest way)

There are 2 topics about that problem just a few topics before your

http://www.dreamincode.net/forums/showtopic56271.htm
and
http://www.dreamincode.net/forums/showtopic55126.htm


This post has been edited by pbl: 30 Jun, 2008 - 05:16 PM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/5/08 05:26PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month