Results 1 to 1 of 1
  1. #1
    treeface99 is offline Member
    Join Date
    Jan 2012
    Rep Power

    Default Trouble displaying an image using GridBagLayout


    I am creating a GUI and have a number of labels and buttons within my JPanel. I have successfully added an image however it is not displaying in full. Only the top left corner is visible. I have constraints in place allowing me to position the image using gridx and gridy and have tried using gridHeight/gridWidth but it still does not sort the problem out. Here is my code.

    Java Code:
    public class GUI extends JFrame implements ActionListener{
    	int mode =0 ;
    	JFrame frame = new JFrame("interface");
    	JPanel panel;
    	public int run() {	
    		frame = new JFrame("Interface");
    		panel = new JPanel(new GridBagLayout());
    		frame.getContentPane().add(panel, BorderLayout.NORTH);
    		GridBagConstraints con = new GridBagConstraints();		
    		con.gridx = 0;
    		con.gridy =0;		
    		con.insets = new Insets(10,10,10,10);// spacing from item on left, right, up ,down
    		JLabel title = new JLabel("WELCOME TO BLACKJACK!");
    		Font tFont = new Font("serif",Font.BOLD,25);
    		JLabel option = new JLabel("Please select a mode");
    		con.gridx = 0;
    		con.gridy = 3;		
    		JButton button = new JButton ("Player");
    		con.gridx = 0;
    		con.gridy =4;
    		JButton button2 = new JButton ("Dealer");		
    		con.gridx = 1;
    		con.gridy =4;		
    		CardImages s = new CardImages();
    		con.gridx = 0;
    		con.gridy = 6;		
    The image is passed by the following class:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class CardImages extends JPanel{	
    	private ImageIcon image;
    	public CardImages() {
    	public void paintComponent (Graphics g){
    		image = new ImageIcon("QUEEN.jpg");
    		image.paintIcon(this,g, 0,0);		
    I have made a little progress. When i use

    Java Code:
    //con.fill = GridBagConstraints.HORIZONTAL;
    the whole width of the image was displayed but only a strip at the top of the image.

    Any suggestions??

    Many Thanks
    Last edited by DarrylBurke; 03-16-2012 at 03:34 AM.

Similar Threads

  1. some swing components are not displaying
    By mDennis10 in forum AWT / Swing
    Replies: 6
    Last Post: 01-27-2012, 10:26 AM
  2. Problem with displaying an image in Java Swing
    By BackInSoftware in forum Advanced Java
    Replies: 10
    Last Post: 01-11-2011, 07:56 PM
  3. Getting and Displaying Swing Components From DataBase
    By vivekkumar in forum AWT / Swing
    Replies: 1
    Last Post: 07-27-2010, 04:32 PM
  4. Trouble Displaying Image
    By LankanSniper in forum Java 2D
    Replies: 3
    Last Post: 12-10-2009, 10:26 AM
  5. Java Swing GridBagLayout Problem
    By hemanthjava in forum AWT / Swing
    Replies: 1
    Last Post: 06-29-2008, 09:51 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