Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Images in GridLayout

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Images in GridLayout

    So, I wanna display a little image of a register at the top of each column to display a checkout line. I can't seem to get the Icon to show. What's wrong here? I've followed the example in my book and the icon won't show up at all.

    I'm using eclipse and I put the icon in the same directory as the .java files which is the src folder under the project name inside the workspace folder.

    So here is my view so far:

     
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.GridLayout;
     
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
     
    public class StoreView extends JFrame{
     
    	private JPanel topPanel;
    	private JPanel checkoutPanel;
     
    	private JButton createCustomer;
    //	private JPanel checkoutPanel = new JPanel();
     
    	private JLabel totalServiceTime;
     
    	private ImageIcon registerIcon;
    	private JLabel iconLabel;
     
     
    // private FlowLayout flow = new FlowLayout();
     
    	private StoreModel model;
     
    	public StoreView(StoreModel model){
    		this.model = model;
     
    		setTitle("Checkout Simulator");
     
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLayout(new GridLayout());
     
    		buildTopPanel();
    		buildCheckoutPanel();
     
    		add(topPanel);
    		add(checkoutPanel);
    		pack();
    		setVisible(true);
    	}
     
    	public void buildTopPanel(){
    		topPanel = new JPanel();
    		createCustomer = new JButton("CreateCustomer");
    		topPanel.add(createCustomer);
    	}
     
    	public void buildCheckoutPanel(){
     
    		checkoutPanel = new JPanel();
    		iconLabel = new JLabel();
     
    		registerIcon = new ImageIcon("register.png");
     
    		iconLabel.setIcon(registerIcon);
    		checkoutPanel.add(iconLabel);
    	}
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Images in GridLayout

    Try creating a File instance with that filename, then print out the full path to see where Java thinks it is.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Location
    Lucknow Uttar Pradesh, India
    Posts
    2
    My Mood
    Lonely
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Images in GridLayout

    Hi,
    You have used GridLayout with default constructor to set Layout of Frame. With default, It has only one row and one column.
    That is you can add only one component in Frame.
    And you trying to add two panel in that component.

    Use this statement for getting image:

    registerIcon = new ImageIcon(this.getClass().getResource("register.pn g"));

    You will get Icon.

  4. #4
    Member
    Join Date
    Feb 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Images in GridLayout

    I got it working, but I need the register icon to display at the top of each column. By doing new GridLayout(12,10) , that is 12 rows, 10 columns?

     
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
     
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
     
    public class StoreView extends JFrame{
     
    	private JPanel topPanel;
    	private JPanel checkoutPanel;
    	private JPanel serviceTimePanel;
     
    	private JButton createCustomer;
    //	private JPanel checkoutPanel = new JPanel();
     
    	private JLabel totalServiceTime;
     
    	private ImageIcon registerIcon = new ImageIcon("register.png");
    	private JLabel iconLabel = new JLabel(registerIcon);
     
     
    // private FlowLayout flow = new FlowLayout();
     
    	private StoreModel model;
     
    	public StoreView(StoreModel model){
    		this.model = model;
     
    		setTitle("Checkout Simulator");
     
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLayout(new GridLayout(12,10));
     
    		buildTopPanel();
    		buildServiceTimePanel();
    	 buildCheckoutPanel();
     
     
     
    		add(topPanel);
    		add(serviceTimePanel);
    	 add(checkoutPanel);
     
     
    		pack();
    		setVisible(true);
    	}
     
    	public void buildTopPanel(){
    		topPanel = new JPanel();
    		createCustomer = new JButton("CreateCustomer");
    		topPanel.add(createCustomer);
    	}
     
    	public void buildServiceTimePanel(){
    		serviceTimePanel = new JPanel();
     
    	}
     
    	public void buildCheckoutPanel(){
     
    		checkoutPanel = new JPanel();
    		iconLabel = new JLabel();
     
    		registerIcon = new ImageIcon("/Users//Dev//Documents//workspace//Java Gui//src//register.png");
     
    		iconLabel.setIcon(registerIcon);
    		checkoutPanel.add(iconLabel);
    	}
    }

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Location
    Lucknow Uttar Pradesh, India
    Posts
    2
    My Mood
    Lonely
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Images in GridLayout

    Yes, by using this statement.
    new GridLayout(12, 10);
    The LayoutManager will create a table type structure which contains 12 rows and 10 columns.
    and you can put 12*10 = 120 components.

    To display icons on the top of each columns you need to put the icon of each columns of first row.


Similar Threads

  1. [SOLVED] how to get the row and column of a component gridlayout
    By prettynew in forum AWT / Java Swing
    Replies: 0
    Last Post: March 13th, 2011, 06:39 PM
  2. Moving Icon in gridlayout SWING
    By Loodistobilo in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 24th, 2010, 07:59 PM
  3. Loading in images
    By eXcellion in forum Java Theory & Questions
    Replies: 3
    Last Post: January 17th, 2010, 12:13 PM
  4. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 10:26 AM
  5. What are the best way of placing images in GUI?
    By Ciwan in forum AWT / Java Swing
    Replies: 5
    Last Post: February 26th, 2009, 05:19 PM