Hi I have this little app in java that looks like that :
[IMG]http://img399.imageshack.us/img399/2046/wardrobe6cd.jpg[/IMG]

Its a JFrame a ScrollPane inside and a JTable in it. I'd like to have an image displayed uder the table but I have no clue how to do this (I'm a begginer whit java)
the code looks like this

public class WardrobeFrame extends JFrame implements ChangeListener{

	private WardrobeTableModel _wardrobeTableModel;
	private JTable _wardrobeTable;

	public WardrobeFrame(Wardrobe wardrobe) {
		super("My Wardrobe");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		_wardrobeTableModel = new WardrobeTableModel(wardrobe);
		_wardrobeTable = new JTable(_wardrobeTableModel);
		
		Clock.getClock().addChangeListener(this);
		
		initGUI();
		pack();
		setLocationRelativeTo(null);
	}
	
	private void initGUI() {
		getContentPane().setLayout(new BorderLayout(5,5));
		
		// setting main GUI components
		_wardrobeTable.setRowHeight(45);
		_wardrobeTable.getColumnModel().getColumn(0).setCellRenderer( 
				new ClothesTypeTableRenderer());
		_wardrobeTable.getColumnModel().getColumn(6).setCellRenderer( 
				new TrueFalseTableRenderer());
		
		JScrollPane listScroller = new JScrollPane(_wardrobeTable);
		getContentPane().add(listScroller,BorderLayout.CENTER);

		JPanel actionPanel = new JPanel();
		actionPanel.setLayout(new GridLayout(9, 1));
		getContentPane().add(actionPanel,BorderLayout.SOUTH);
		
		// creating action buttons
		JButton createBtn = new JButton("Create new item");
		actionPanel.add(createBtn);
		
		JButton putOnBtn = new JButton("Put on");
		actionPanel.add(putOnBtn);
		
		JButton takeOffBtn = new JButton("Take off");
		actionPanel.add(takeOffBtn);
		
		JButton sendToLaundryBtn = new JButton("Send to laundry");
		actionPanel.add(sendToLaundryBtn);
		
		JButton removeBtn = new JButton("Remove selected item");
		actionPanel.add(removeBtn);
		
		JButton removeAllBtn = new JButton("Remove all clothes");
		actionPanel.add(removeAllBtn);
		
		JButton saveBtn = new JButton("Save wardrobe content");
		actionPanel.add(saveBtn);
		saveBtn.setForeground(Color.BLUE);
		
		JButton loadBtn = new JButton("Load wardrobe content");
		actionPanel.add(loadBtn);
		loadBtn.setForeground(Color.BLUE);
		
		JButton closeBtn = new JButton("Close wardrobe");
		closeBtn.setFont(new Font(closeBtn.getFont().getFamily(), Font.BOLD, 16));
		closeBtn.setForeground(Color.RED);
		actionPanel.add(closeBtn);
		
		// setting listeners
		createBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				actionCreateAndPut();
			}
		});
		
		putOnBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				actionPutOnSelected();
			}
		});
		
		takeOffBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				actionTakeOffSelected();
			}
		});
			
		sendToLaundryBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				actionSendSelectedToLaundry();
			}
		});
		
		removeBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				actionRemoveSelected();
			}
		});
		
		removeAllBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				actionRemoveAll();
			}
		});
		
		saveBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				actionSaveToFile();
			}
		});
		
		loadBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				 actionLoadFromFile();
			}
		});
		
		closeBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				closeApp();
			}
		});
	}	
	
	//setting actions
	private void actionCreateAndPut() {
		CreateDialog createDialog = new CreateDialog(this);
		createDialog.setVisible(true);
		
		if (!createDialog.isCanceled()) {
			IWearable item = createDialog.getItem();
			_wardrobeTableModel.putItem(item);
		}
	}
	/**
	 * pobiera warunki z tableModel odnosnie metody putOn jesli wszystko sie zgadza odswierza tabele
	*/
	private void actionPutOnSelected() {
		int itemIndex = _wardrobeTable.getSelectedRow();
		if (itemIndex > -1 && itemIndex < _wardrobeTableModel.getRowCount()) {
			int result = _wardrobeTableModel.putOn(itemIndex);
			if (result == 1) {
				JOptionPane.showMessageDialog(this,
						"The item is already in use.","Error", JOptionPane.ERROR_MESSAGE);
			}else if (result == 2){
				JOptionPane.showMessageDialog(this,
						"The item has not returned from loundry.Pleas wait.",
						"Error", JOptionPane.ERROR_MESSAGE);
			}else if (result == 3){
				stateChanged (null); // zeby obrazek true/false startowal odrazu
			}
		}
	}

	private void actionTakeOffSelected() {
		int itemIndex = _wardrobeTable.getSelectedRow();
		if (itemIndex > -1 && itemIndex < _wardrobeTableModel.getRowCount()) {
			boolean result = _wardrobeTableModel.takeOffItem(itemIndex);
			if (result == false)
				JOptionPane.showMessageDialog(this,
						"The item is not in use.","Error", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void actionSendSelectedToLaundry() {
		int itemIndex = _wardrobeTable.getSelectedRow();
		if (itemIndex > -1 && itemIndex < _wardrobeTableModel.getRowCount()) {
			int result = _wardrobeTableModel.sendItemToLaundryConditions(itemIndex);
			if (result == 1) {
				JOptionPane.showMessageDialog(this,
						"You need to take this item off first!", "Error",
						JOptionPane.ERROR_MESSAGE);
			}else if (result == 2){
				JOptionPane.showMessageDialog(this,
						"This item is already in laundry!", "Error",
						JOptionPane.ERROR_MESSAGE);	
			}else if (result == 3){	
				int n = JOptionPane.showConfirmDialog(this,
						"This item seems clean! Would you like to send it to the laundry?","Question",
						JOptionPane.YES_NO_OPTION);
                if (n == JOptionPane.YES_OPTION) {
                	_wardrobeTableModel.sendItemToLaundry(itemIndex);
                	JOptionPane.showMessageDialog(this,
                			"Your clothes have been sent to the laundry!", "Succes",
                			JOptionPane.INFORMATION_MESSAGE);
                } else if (n == JOptionPane.NO_OPTION) {
               } 
			}
			else if (result == 4){
				_wardrobeTableModel.sendItemToLaundry(itemIndex);
				JOptionPane.showMessageDialog(this,
						"Your clothes have been sent to the laundry!", "Succes",
						JOptionPane.INFORMATION_MESSAGE);
			}
		}
	}
	
	private void actionRemoveSelected() {
		int itemIndex = _wardrobeTable.getSelectedRow();
		if (itemIndex > -1 && itemIndex < _wardrobeTableModel.getRowCount()) {
			boolean result = _wardrobeTableModel.removeItem(itemIndex);
			if (result == false) {
				JOptionPane.showMessageDialog(this, 
						"You need to take this item off first!", "Error",
						JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	private void actionRemoveAll() {
		int n = JOptionPane.showConfirmDialog(this,
				"This will remove ALL of the items. Continue?","Question",
				JOptionPane.YES_NO_OPTION);
        if (n == JOptionPane.YES_OPTION) {
        	_wardrobeTableModel.removeAll();
        } else if (n == JOptionPane.NO_OPTION) {
       } 
	}
	
	private void actionSaveToFile() {
		try {
			final JFileChooser fc = new JFileChooser();
	        int returnVal = fc.showOpenDialog(this);
	        	if (returnVal == JFileChooser.APPROVE_OPTION){
	        			File file = fc.getSelectedFile();
	        			FileOutputStream fout = new FileOutputStream(file);
	        			ObjectOutputStream oout = new ObjectOutputStream(fout);
	        			oout.writeObject(_wardrobeTableModel.getWardrobe());
	        			oout.close();
	        			JOptionPane.showMessageDialog(this, "Wardrobe saved");
	        	}else {
	        	}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this,
					"Cannot save wardrobe: "+e.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
	}
	
	private void actionLoadFromFile(){
		try {
			final JFileChooser fc = new JFileChooser();
	        int returnVal = fc.showOpenDialog(this);
	        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
	        if (returnVal == JFileChooser.APPROVE_OPTION){
	        File file = fc.getSelectedFile();
	        FileInputStream fin = new FileInputStream(file);
			ObjectInputStream oin = new ObjectInputStream(fin);
			_wardrobeTableModel.setWardrobe( (Wardrobe) oin.readObject());
			oin.close();
			JOptionPane.showMessageDialog(this, "Wardrobe loaded");
	        	}else {
        	}
			
		}catch (EOFException eof) {
			JOptionPane.showMessageDialog(this, "Cannot load wardrobe: Unexpected end of file", 
                    "Error", JOptionPane.ERROR_MESSAGE);
			eof.printStackTrace();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Cannot load wardrobe: "+e.getMessage(), 
                    "Error", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
	}
	private void closeApp() {
		int n = JOptionPane.showConfirmDialog(this,
				"Do you really want to quit?","Question",JOptionPane.YES_NO_OPTION);
        if (n == JOptionPane.YES_OPTION) {
        	System.exit(0);
        } else if (n == JOptionPane.NO_OPTION) {
       } 
	}

	public void stateChanged (ChangeEvent arg0){
		repaint();
	}
}

the question is simple how do I load an image as a backgorund

I'd appriciate any help

Personally, I would create a seperate class that extends JPanel and add it to the other class:

import javax.swing;

public class ImagePanel extends JPanel
{
   public ImagePanel
   {
   }

    public void paintComponent(Graphics g)
    {
    }

}

Then add it to your normal class:

public class WardrobeFrame blah blah blah
{
    ImagePanel ip;

   public WardrobeFrame()
   {
      ip = new ImagePanel();
      add(ip, BorderLayout.CENTER);
   }
}

Something like that ;)

ok so I've made an ImagePanel class and put this code below to my WardrobeFrame now it looks like this:

public WardrobeFrame(Wardrobe wardrobe) {
		super("My Wardrobe");
		
		 ip = new ImagePanel();
	      add(ip, BorderLayout.CENTER);
	      
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		_wardrobeTableModel = new WardrobeTableModel(wardrobe);
		_wardrobeTable = new JTable(_wardrobeTableModel);
		
		Clock.getClock().addChangeListener(this);
		
		initGUI();
		pack();
		setLocationRelativeTo(null);
	}

and now its time for a stupid quesstion... how do I load an image? :)

I expected that question ;)

It's something like this:

INSIDE YOUR PAINTCOMPONENT METHOD:


g.drawImage(image, x, y, this);

or


g.drawImage(image, x, y, null);


I can't remember the last part, but I know it's one of the two.

This article has been dead for over six months. Start a new discussion instead.