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 8 of 8

Thread: Grid GUI Library

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,619
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Grid GUI Library

    Does anyone know of a good, easy to use, Java Library that will allow me to create an interactive Grid GUI?

    Any help is appreciated.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 820 Times in 763 Posts
    Blog Entries
    5

    Default Re: Grid GUI Library

    Can you elaborate a bit more on what you mean by an interactive Grid GUI? What I would envision is something like a checkerboard type of grid, where the squares can be moved around (which could be relatively easy to do in principle), but this could be far from what you need.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,619
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: Grid GUI Library

    I'm actually thinking more of something like a Grid of Images that I can swap out. So the user can click on a Cell and change it's Image somehow. Alternatively, I would also like the Cells to be able to contain things like ints or Strings or something that could possibly be edited by the user.

    I'm not interested in moving around cells at the moment, but more changing their traits.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 820 Times in 763 Posts
    Blog Entries
    5

    Default Re: Grid GUI Library

    Should be quite feasible with just a GridLayout or GridBagLayout. A Visual Guide to Layout Managers

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,619
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: Grid GUI Library

    I was playing around with it, drawing rectangles and whatnot. Then I came across an issue. How can I make the JPanel repaint itself every time it is clicked?

    import javax.swing.JPanel;
     
    import java.awt.Graphics;
     
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
     
    import java.awt.Point;
     
    public class Grid extends JPanel
    {
    	int rows;
    	int columns;
     
    	int xCo = -1;
    	int yCo = -1;
     
    	Point[][] points;
     
        public Grid(int r,int c) 
        {
        	rows = r;
        	columns = c;
        	points = new Point[rows][columns];
        	this.addMouseListener(new UserClick());
        }
     
        public void paintComponent(Graphics g)
        {
        	super.paintComponent(g);
        	for(int x=0;x<rows;x++)
        	{
        		for(int y=0;y<columns;y++)
        		{
        			points[x][y] = new Point(x*50,y*50);
        			if(xCo==x && yCo==y)
        				g.fillRect(x*50,y*50,50,50);
        			else
        				g.drawRect(x*50,y*50,50,50);
        		}
        	}
        	xCo = -1;
        	yCo = -1;
        }
     
        public class UserClick extends MouseAdapter
        {
        	public UserClick()
        	{
        		super();
        	}
        	public void mouseClicked(MouseEvent e)
        	{
        		Point p = e.getPoint();
        		System.out.println("The Cell at location "+(int)(p.getX()/50)+","+(int)(p.getY()/50)+" was pressed.");
        		xCo = (int)(p.getX()/50);
        		yCo = (int)(p.getY()/50);
        	}
        }
     
    }

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 820 Times in 763 Posts
    Blog Entries
    5

    Default Re: Grid GUI Library

    @Override
    public void mouseClicked(MouseEvent e){
        Grid.this.repaint();//
    }

  7. The Following User Says Thank You to copeg For This Useful Post:

    aussiemcgr (September 15th, 2010)

  8. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,619
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: Grid GUI Library

    Quote Originally Posted by copeg View Post
    @Override
    public void mouseClicked(MouseEvent e){
        Grid.this.repaint();//
    }
    Wow, you have no idea how helpful that will be down the line. Much appreciated.

  9. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 820 Times in 763 Posts
    Blog Entries
    5

    Default Re: Grid GUI Library

    Glad to be of help.

Similar Threads

  1. Powerpoint Library
    By aussiemcgr in forum Java Theory & Questions
    Replies: 0
    Last Post: July 22nd, 2010, 08:26 AM
  2. Shared library path
    By sateesh.b in forum Java Native Interface
    Replies: 3
    Last Post: May 13th, 2010, 11:12 PM
  3. library
    By b109 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 3rd, 2010, 05:32 AM
  4. creating a shared library
    By navinbecse in forum Threads
    Replies: 1
    Last Post: April 9th, 2010, 07:54 AM
  5. Replies: 1
    Last Post: October 7th, 2008, 07:35 AM