7 Replies - 412 Views - Last Post: 05 April 2013 - 11:14 AM Rate Topic: -----

#1 Naked Snake  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-December 12

ImageIcon not working

Posted 05 April 2013 - 09:56 AM

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.TitledBorder;

public class ImageViewerFrame2 extends JFrame
{

private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 400;

private JRadioButton Area1;
private JRadioButton Area2;
private JRadioButton Area3;
private JRadioButton Area4;
private JRadioButton Area5;
private JRadioButton Area6;
private ActionListener listener;
private JLabel GeneralLocation;
private JLabel PreciseInfo;
private JLabel pictureLabel;
private String Description;

public ImageViewerFrame2()
{

GeneralLocation = new JLabel("This is the area of.... ");
add(GeneralLocation, BorderLayout.NORTH);

PreciseInfo = new JLabel ("" + Description);
add(PreciseInfo, BorderLayout.SOUTH);

pictureLabel = new JLabel("images/GregoryLake.JPG");
add(pictureLabel,BorderLayout.CENTER);



class ChoiceListener implements ActionListener
{ 
 
public void actionPerformed(ActionEvent event)
	{ 
 	setText();
 	setPicture();
	}

}

listener = new ChoiceListener();

createControlPanel();
setText();
setSize(FRAME_WIDTH, FRAME_HEIGHT);

}


public void createControlPanel()
{

JPanel AreaGroupPanel = createRadioButtons();

// Line up component panels

JPanel controlPanel = new JPanel();
controlPanel.add(AreaGroupPanel);

// Add panels to content pane

add(controlPanel, BorderLayout.WEST);
}

// Creates RadioButton for Cash, Check and Credit
public JPanel createRadioButtons()
{
Area1 = new JRadioButton("Area");
Area1.addActionListener(listener);

Area2 = new JRadioButton("Area");
Area2.addActionListener(listener);

Area3 = new JRadioButton("Area");
Area3.addActionListener(listener);
Area3.setSelected(true);

Area4 = new JRadioButton("Area");
Area4.addActionListener(listener);

Area5 = new JRadioButton("Area");
Area5.addActionListener(listener);

Area6 = new JRadioButton("Area");
Area6.addActionListener(listener);






// Add radio buttons to button group

ButtonGroup group = new ButtonGroup();
group.add(Area1);
group.add(Area2);
group.add(Area3);
group.add(Area4);
group.add(Area5);
group.add(Area6);

JPanel panel = new JPanel();
panel.add(Area1);
panel.add(Area2);
panel.add(Area3);
panel.add(Area4);
panel.add(Area5);
panel.add(Area6);
panel.setBorder(new TitledBorder("New Area"));

return panel;
}

/*
Creates instant variable paymentMethod to ""
Creates instant variables for cash, check and credit
if statment checks of buttons are selected
*/
public void setText()
{

String Description = "";

final String Area1Method = "You have selected Area1";
final String Area2Method = "You have selected Area2";
final String Area3Method = "You have selected Area3";
final String Area4Method = "You have selected Area4";
final String Area5Method = "You have selected Area5";
final String Area6Method = "You have selected Area6";

if (Area1.isSelected()) { Description = Area1Method; }
else if (Area2.isSelected()) { Description = Area2Method; }
else if (Area3.isSelected()) { Description = Area3Method; }
else if (Area4.isSelected()) { Description = Area4Method; }
else if (Area5.isSelected()) { Description = Area5Method; }
else if (Area6.isSelected()) { Description = Area6Method; }

// Set text of text field

PreciseInfo.setText(Description);      
PreciseInfo.repaint();
}

public void setPicture()
{
	pictureLabel = new JLabel("");
	
	if (Area1.isSelected()) {pictureLabel.setIcon(new ImageIcon("images/GregoryLake.JPG"));}
	else if (Area2.isSelected()) {pictureLabel.setIcon(new ImageIcon("images/GregoryLake.JPG"));}
	else if (Area3.isSelected())  {pictureLabel.setIcon(new ImageIcon("images/GregoryLake.JPG"));}
	else if (Area4.isSelected()) {pictureLabel.setIcon(new ImageIcon("images/GregoryLake.JPG"));}
	else if (Area5.isSelected()) {pictureLabel.setIcon(new ImageIcon("images/GregoryLake.JPG"));}
	else if (Area6.isSelected()) {pictureLabel.setIcon(new ImageIcon("images/GregoryLake.JPG"));}
  

pictureLabel.repaint();
}



}



import javax.swing.JFrame;

public class ImageViewer
{

public static void main(String[] args)
{ 
 
JFrame frame = new ImageViewerFrame2();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Images");
frame.setVisible(true);
      
}

}





I have been trying to get this code to output an image when one of the radio buttons are clicked. But no image is displayed. I have been at this for a while and do not understand why there aren't any images

Is This A Good Question/Topic? 0
  • +

Replies To: ImageIcon not working

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,883
  • Joined: 06-March 08

Re: ImageIcon not working

Posted 05 April 2013 - 10:23 AM

This

pictureLabel = new JLabel("");

will create a new JLabel into which you will put your ImageIcon but this is not the JLabel displayed in your JFrame

Simply remove that line... you want to setIcon() to the displayed pictureLabel
Was This Post Helpful? 1
  • +
  • -

#3 Naked Snake  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-December 12

Re: ImageIcon not working

Posted 05 April 2013 - 10:31 AM

View Postpbl, on 05 April 2013 - 10:23 AM, said:

This

pictureLabel = new JLabel("");

will create a new JLabel into which you will put your ImageIcon but this is not the JLabel displayed in your JFrame

Simply remove that line... you want to setIcon() to the displayed pictureLabel

I removed that line but there still no images being displayed.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,883
  • Joined: 06-March 08

Re: ImageIcon not working

Posted 05 April 2013 - 10:35 AM

Extend your JFrame size just for fun by dragging its right border... I'll bet the image is too much to the right of your JFrame (if the Image have been read correctly)
Was This Post Helpful? 0
  • +
  • -

#5 Naked Snake  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-December 12

Re: ImageIcon not working

Posted 05 April 2013 - 10:38 AM

All I get is text. image/GregoryLake.JPG. no images.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,883
  • Joined: 06-March 08

Re: ImageIcon not working

Posted 05 April 2013 - 10:46 AM

And learn how to use array
And no need top repaint() changed JComponent, they do it themselve

public class ImageViewerFrame2 extends JFrame
{

	private static final int FRAME_WIDTH = 300;
	private static final int FRAME_HEIGHT = 400;

	private JRadioButton[] radio = new JRadioButton[6];
	private ActionListener listener;
	private JLabel GeneralLocation;
	private JLabel PreciseInfo;
	private JLabel pictureLabel;
	private String Description;

	public ImageViewerFrame2()
	{

		GeneralLocation = new JLabel("This is the area of.... ");
		add(GeneralLocation, BorderLayout.NORTH);

		PreciseInfo = new JLabel ("" + Description);
		add(PreciseInfo, BorderLayout.SOUTH);

		pictureLabel = new JLabel("jasdklajsdljasldj");
		add(pictureLabel,BorderLayout.CENTER);



		class ChoiceListener implements ActionListener
		{ 

			public void actionPerformed(ActionEvent event)
			{ 
				setText();
			}

		}

		listener = new ChoiceListener();

		createControlPanel();
		setText();
		setSize(FRAME_WIDTH, FRAME_HEIGHT);

	}


	public void createControlPanel()
	{

		JPanel AreaGroupPanel = createRadioButtons();

		// Line up component panels

		JPanel controlPanel = new JPanel();
		controlPanel.add(AreaGroupPanel);

		// Add panels to content pane

		add(controlPanel, BorderLayout.WEST);
	}

	// Creates RadioButton for Cash, Check and Credit
	public JPanel createRadioButtons()
	{
		ButtonGroup group = new ButtonGroup();
		JPanel panel = new JPanel();
		for(int i = 0; i < radio.length; ++i) {
			radio[i] = new JRadioButton("Area " + (i+1));
			radio[i].addActionListener(listener);
			group.add(radio[i]);
			panel.add(radio[i]);
		}

		panel.setBorder(new TitledBorder("New Area"));

		return panel;
	}

	/*
Creates instant variable paymentMethod to ""
Creates instant variables for cash, check and credit
if statment checks of buttons are selected
	 */
	public void setText()
	{

		String[] imageStr = {"celleCi.jpg","celleCi.jpg","celleCi.jpg","celleCi.jpg","celleCi.jpg","celleCi.jpg"}; 

		for(int i = 0; i < radio.length; ++i) {
			if(radio[i].isSelected()) {
				String Description = "You have selected area: " + (i+1);
				pictureLabel.setIcon(new ImageIcon("image/" + imageStr[i]));
				PreciseInfo.setText(Description);      
				break;
			}
		}

		// Set text of text field
	}



}


This post has been edited by pbl: 05 April 2013 - 11:06 AM

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,883
  • Joined: 06-March 08

Re: ImageIcon not working

Posted 05 April 2013 - 10:51 AM

Works for me
Your directory structure must not be what you think
Describe your directory structure... where are your .java where are your .class where is \image ?
Was This Post Helpful? 0
  • +
  • -

#8 Naked Snake  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-December 12

Re: ImageIcon not working

Posted 05 April 2013 - 11:14 AM

Thanks I got it to work. Thank you so much man.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1