Results 1 to 5 of 5
  1. #1
    rakosky is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Angry ActionListener Help

    In the ActionListener for JButton OK I want to return the image and dispose the frame, but I don't know how. Help?
    Java Code:
    ImageIcon NewPic()
    	{
    		final JFrame PicSelecter = new JFrame();
    		PicSelecter.setTitle("New Picture");
    		PicSelecter.setLocation(new Point(400,300));
    		PicSelecter.setSize(400, 400);
    		PicSelecter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		PicSelecter.setResizable(false);
    		PicSelecter.setVisible(true);
    		PicSelecter.setLayout(null);
    		final String[] PicNames = {"Select","MLG","Swag","Hi-Rez","Pizza"};
    		final JComboBox Choices = new JComboBox(PicNames);
    		Choices.setBounds(20,20,100,25);
    		PicSelecter.add(Choices);
    
    		JPanel PreviewHolder = new JPanel();
    		PreviewHolder.setLayout(null);
    		PicSelecter.add(PreviewHolder);
    		PreviewHolder.setBounds(50,60,300,250);
    
    		Preview.setBounds(0,0,PreviewHolder.getWidth(),PreviewHolder.getHeight());
    		
    		final ImageIcon MLG = new ImageIcon("E:/Pictures/MLG.jpg");
    		final Image newMLG = MLG.getImage().getScaledInstance(Preview.getWidth(), Preview.getHeight(),  java.awt.Image.SCALE_SMOOTH);   
    		MLG.setImage(newMLG);
    
    		final ImageIcon Swag = new ImageIcon("E:/Pictures/Swag.jpg");
    		final Image newSwag = Swag.getImage().getScaledInstance(Preview.getWidth(), Preview.getHeight(),  java.awt.Image.SCALE_SMOOTH);   
    		Swag.setImage(newSwag);
    		
    		final ImageIcon Hi_Rez = new ImageIcon("E:/Pictures/Hi Rez.jpg");
    		final Image newHi_Rez = Hi_Rez.getImage().getScaledInstance(Preview.getWidth(), Preview.getHeight(),  java.awt.Image.SCALE_SMOOTH);   
    		Hi_Rez.setImage(newHi_Rez);
    		
    		final ImageIcon Pizza = new ImageIcon("E:/Pictures/Pizza.jpg");
    		final Image newPizza = Pizza.getImage().getScaledInstance(Preview.getWidth(), Preview.getHeight(),  java.awt.Image.SCALE_SMOOTH);   
    		Pizza.setImage(newPizza);
    		
    		Choices.addActionListener(new ActionListener(){
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				int choice = Choices.getSelectedIndex();
    				
    				if(choice == 1)
    					Preview.setIcon(MLG);
    				else if(choice == 2)
    					Preview.setIcon(Swag);
    				else if(choice == 3)
    					Preview.setIcon(Hi_Rez);
    				else if(choice == 4)
    					Preview.setIcon(Pizza);
    			}
    		});
    		PreviewHolder.add(Preview);
    		
    		final JButton OK = new JButton("OK");
    		OK.setBounds(50,325,100,35);
    		
    		OK.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent arg0) {
    				Preview.getIcon();
    				
    				PicSelecter.dispose();
    			}
    		});
    		PicSelecter.add(OK);
    		
    		final JButton Cancel = new JButton("Cancel");
    		Cancel.setBounds(250,325,100,35);
    		
    		Cancel.addActionListener(new ActionListener(){
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				PicSelecter.dispose();
    			}
    		});
    		PicSelecter.add(Cancel);
    		repaint();
    		validate();
    		
    		return;
    	}
    Last edited by Norm; 04-06-2012 at 04:01 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: ActionListener Help

    You can't return any value from a listener. You could assign a value to a class variable.

    Your use of variable names with uppercase first letters. The coding standards use uppercase for class names and lowercase for variables.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    rakosky is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: ActionListener Help

    So what you're saying is that I should make NewPic method a seperate class?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,241
    Rep Power
    19

    Default Re: ActionListener Help

    Moved from Advanced Java. And it's not an advanced topic anyways.

    Look around the forum FAQs and discover how to post code so that it retains its formatting.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: ActionListener Help

    How is the newPic method related to the problem?
    Putting the method in a separate class won't be needed.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Please help with actionlistener
    By ADustedEwok in forum New To Java
    Replies: 5
    Last Post: 12-08-2011, 10:04 PM
  2. ActionListener
    By jaylimix in forum Java Applets
    Replies: 5
    Last Post: 11-06-2011, 06:05 PM
  3. Please Help With ActionListener
    By Daman12 in forum New To Java
    Replies: 29
    Last Post: 10-26-2011, 07:43 AM
  4. JMenu and ActionListener
    By sairus in forum AWT / Swing
    Replies: 0
    Last Post: 02-22-2011, 06:11 PM
  5. ActionListener w/ Dialog
    By BariMutation in forum New To Java
    Replies: 1
    Last Post: 12-03-2010, 04:50 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •