Results 1 to 2 of 2
  1. #1
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default [SOLVED] JButton does not display ImageIcon properly

    Hi there all. I have an ongoing project with numerous classes, and I am working on my GUI. However, I have run into a problem with the code below. When I run the code, it compiles fine, but only displays the two ToolButtons(subclass of JButton) without the images. I don't want to post all the classes, as together it would make over a thousand lines of code, but I will try to describe what each class used by the code does.

    SpringUtilities: Available from links in Sun's tutorials. This class provides methods for easier use of SpringLayout.

    DrawingTool: abstract class allowing access to subclasses. The getDefaultIcon(Tool) is an abstract method, and each subclasx uses it to define which ImageIcon should be for the JLabel as a default.

    Tool: This is an enum describing which type of DrawingTool to use.

    EnumConverter: Takes my Enums (in this case the Enum is Tool) and returns whatever object or picture should be used. The methods return correctly.

    There is something wrong with how I set the Icon of my ToolButton, as println statements are returning the correct path to the image file.

    Here's my code:
    Java Code:
    package main;
    
    import java.awt.event.ActionEvent;
    
    import javax.swing.*;
    
    import drawtools.SpringUtilities;
    import drawtools.Tool;
    import drawtools.colorconstants.EnumConverter;
    import drawtools.tools.DrawingTool;
    
    public class ToolButtonPanel extends JPanel{
    
    	private static final long serialVersionUID = 1L;
    	private ToolButton diamond = new ToolButton(Tool.DIAMOND), cross = new ToolButton(Tool.CROSS);
    	public ToolButtonPanel(){
    		diamond.setAction(new ToolAction(Tool.DIAMOND));
    		cross.setAction(new ToolAction(Tool.CROSS));
    		
    		add(diamond);
    		add(cross);
    		
    		setLayout(new SpringLayout());
    		SpringUtilities.makeCompactGrid(this, 1, 2, 4, 4, 4, 4);
    	}
    	public static void main(String[]args){
    		ToolButtonPanel cbp = new ToolButtonPanel();
    		JFrame f = new JFrame();
    		f.setContentPane(cbp);
    		f.pack();
    		f.setVisible(true);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    	private class ToolAction extends AbstractAction{
    
    		private static final long serialVersionUID = 1L;
    		private Tool tool;
    		
    		public ToolAction(Tool t){
    			tool = t;
    		}
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			Main.selectedTool = tool;
    		}
    
    	}
    
    	private class ToolButton extends JButton{
    		
    		private static final long serialVersionUID = 1L;
    
    		public ToolButton(Tool tool){
    			DrawingTool i = EnumConverter.getTool(tool);
    			ImageIcon icon = i.getDefaultIcon();
    			setIcon(icon);
    			setSize(icon.getIconWidth(), icon.getIconHeight());
    		}
    	}
    }
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    nevermind this. I just changed ToolButton to be a regular button and it worked fine.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Netbeans Help JButton Imageicon
    By hitmen in forum AWT / Swing
    Replies: 1
    Last Post: 02-19-2009, 08:56 PM
  2. ImageIcon background
    By Yannick in forum New To Java
    Replies: 11
    Last Post: 02-12-2009, 05:13 PM
  3. send imageIcon over RMI
    By nadia in forum Advanced Java
    Replies: 4
    Last Post: 01-06-2009, 04:41 PM
  4. ImageIcon and size
    By wokena in forum Java 2D
    Replies: 3
    Last Post: 12-21-2008, 09:47 AM
  5. JButton to display JTable
    By Nemesis777 in forum New To Java
    Replies: 0
    Last Post: 12-08-2008, 01:16 PM

Posting Permissions

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