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.
Printable View
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.
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.
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.
Should be quite feasible with just a GridLayout or GridBagLayout. A Visual Guide to Layout Managers
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?
Code java: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); } } }
Glad to be of help.