Results 1 to 5 of 5
  1. #1
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default Why can't I see the images in the buttons?

    I am writing this flashcard application where there's a toolbar panel at the bottom. The toolbar contains buttons with images and text in them. When the application runs, the text is visible but not the images. Where am I going wrong?

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.util.LinkedList;
    
    import javax.swing.AbstractButton;
    import javax.swing.GroupLayout;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenuBar;
    import javax.swing.JPanel;
    
    
    public class Flashcards implements ActionListener {
    	JFrame window;
    	JLabel statusLabel;
    
    	//Constructor
    	public Flashcards()
    	{
    		createUserInterface();
    	}
    	
    	//Public Methods
    	
    	
    	//Private Methods.
    	private void createUserInterface()
    	{
    		//Basic UI
    		window = new JFrame("Flashcards");
    			window.setSize(600,300);
    			window.setVisible(true);
    			window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    				
    			
    		JPanel mainPanel = new JPanel();
    			window.getContentPane().add(mainPanel, BorderLayout.CENTER);
    		
    		statusLabel = new JLabel("Label");
    			mainPanel.add(statusLabel);
    		
    		JPanel toolbar = new JPanel();
    			//toolbar layout and buttons.
    			window.getContentPane().add(toolbar, BorderLayout.SOUTH);
    			
    		JMenuBar menubar = new JMenuBar();
    			window.setJMenuBar(menubar);
    		
    		//The toolbar and the grouplayout
    			Icon[] arrayOfButtonImages = {new ImageIcon("/Images/Open.png")};
    			
    			JButton btnOpenList = new JButton("Open", arrayOfButtonImages[0]);
    				btnOpenList.setVerticalTextPosition(AbstractButton.CENTER);
    				btnOpenList.setHorizontalTextPosition(AbstractButton.CENTER);
    				btnOpenList.setActionCommand("Open");
    				btnOpenList.addActionListener(this);
    			
    			GroupLayout toolbarLayout = new GroupLayout(toolbar);
    			toolbar.setLayout(toolbarLayout);
    			
    			toolbarLayout.setHorizontalGroup(
    			toolbarLayout.createSequentialGroup()
    				.addComponent(btnOpenList);
    				
    			
    			toolbarLayout.setVerticalGroup(
    					toolbarLayout.createParallelGroup()
    						.addComponent(btnOpenList);
    
    	}
    	public void actionPerformed(ActionEvent event)
    	{
    		System.out.println(String.format("%s", event.getActionCommand()));
    		statusLabel.setText(String.format("%s", event.getActionCommand()));
    	}
    	
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new Flashcards();
    	}
    
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Why can't I see the images in the buttons?

    Is "/Images/Open.png" the path to the image? The first slash says this is an absolute path - not relative to the application, which would mean it is looking in a location independent of your application (not recommend). See the following for how to specify where an image is within a package
    How to Use Icons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Why can't I see the images in the buttons?

    Quit multiposting the same question: Why can't I see the images in the buttons?

  4. #4
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default Re: Why can't I see the images in the buttons?

    Quote Originally Posted by camickr View Post
    Quit multiposting the same question: Why can't I see the images in the buttons?
    Sorry about that mate! It wasn't intentional.

  5. #5
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default Re: Why can't I see the images in the buttons?

    Quote Originally Posted by doWhile View Post
    Is "/Images/Open.png" the path to the image? The first slash says this is an absolute path - not relative to the application, which would mean it is looking in a location independent of your application (not recommend). See the following for how to specify where an image is within a package
    How to Use Icons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    Thanks. I didn't notice that!
    It works now.

Similar Threads

  1. Why can't I see the images in the buttons?
    By eLancaster in forum New To Java
    Replies: 1
    Last Post: 10-18-2011, 01:15 AM
  2. Replies: 4
    Last Post: 12-16-2010, 04:32 PM
  3. Help: Displaying Images AND Adding Buttons
    By Rhez in forum New To Java
    Replies: 2
    Last Post: 08-05-2010, 06:19 AM
  4. Replies: 2
    Last Post: 04-30-2010, 03:06 AM
  5. Changing images by clicking arrow buttons. help?
    By ashton in forum New To Java
    Replies: 3
    Last Post: 02-08-2009, 11:29 AM

Posting Permissions

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