Which is better?
Hi,
Here is my question:
Scenario:
I am trying to get a two dimensional array of x,y numbers to show on a grid in the GUI. You should also be able to layer different pictures on top of the different cells in the grid. The grid also changes colour in the sells depending on certain events. The cells in the grid always go in the same order that the x, y numbers are. (1, 1) is 1, 1 on the grid, etc.
Problem
So now I am trying to figure out what to use to represent the cells in the grid. Should they be JPanels, or should they be JLabels? JPanels can change colour without having to use pictures, but JLables may be easier to sort and keep track of.
So please somebody tell me. I've tried for hours with JPanels but with no success.
Regards,
Platinum
[820 byte] By [
Platinuma] at [2007-9-29 17:00:38]

My first thought would be to use a JTable. You can use a cell renderer to put a picture on or change the color. You could use simple a custom table model to deal with a 2D int array.
Here's a simple example of the modelimport java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class Test extends JFrame {
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
int[][] data = {{1,2,3,4,5},{6,7,8,9,10},{11,22,33,44,55}};
JTable jt = new JTable(new MyModel(data));
content.add(new JScrollPane(jt), BorderLayout.CENTER);
setSize(300,300);
show();
}
public static void main(String args[]) { new Test(); }
}
class MyModel extends AbstractTableModel {
int[][] data;
public MyModel(int[][] data) {
this.data=data;
}
public Object getValueAt(int row, int col) {
return new Integer(data[row][col]);
}
public void setValueAt(Object value, int row, int col) {
if (value instanceof Integer) {
data[row][col]=((Integer)value).intValue();
}
}
public int getColumnCount() { return data[0].length; }
public int getRowCount() { return data.length; }
}
Hi,Thanks very much for your reply."Oh drat these computers, they're so naughty and so complex. I could pinch them." ;)Regards,Platinum
Just a question,Can you place pictures in the cells, and can you fill the background with colour? Please show me how this might be done.
Use a [url http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#renderer]cell renderer[/url]. The renderer extends JLabel so you can put an image on it or set the background (don't forget setOpaque(true)). It is used as a 'rubber stamp' to draw each cell in the table.
Hi,
I'm new to GUI's and Swing in particular. I'm having trouble piecing this together. Could you show me with code how you would change a certain cell's colour (referring to it by its coordinates), its picture depending on certain circumstances, and make row headers as well?
That's a pretty steep list of needs, and I'm a bit lost.
Thank you for your help so far.
Platinum
8HKxXYUpjrsmzKwCGP3Ln10MFmuqpOoPhyTcsN3tvNNmBNEitBuBQbErNUvpHbCRdczWM9WmjJNeV,tI
2KwwVGd2OTJ6Y14HrzIYRlykEzIbiuwmkqKyRAPctvpCVbJyicqnOyXAwguseqAiOFaQeFWGSLSauSpH
UQepEQhbS,Lr7nMb3HbWRmB8gKGDX8hJgOPdo,3NRWY2zUw1CJVoC9@dJzitByeQ9,3jwRDUK1N4HAGi
FHasCp41tRei5US@fYQfevQ6OsvpFfCMr12,lNpMcGnkxJIxSCw9K5U@IEvSlhnrTlW5lhxIGbrMBrVj
cjqLtZDMsYAc0jAsMAGfsfcSpToHBIYt0udzWeUcapbuZnubt0U1vr@MDzpvVCu6Stb4n9VuaIBhR0MO
HwOA4vlJPP8rymKO45nfjCeDnB4o7xBWeBB4NxNEzpVRDSMCGXVshwUoiOAQiCGMAvg6A@LX4DfMt8sS
y3DTq6pt5fJTA2EwHVuWhUgL,w5zqUjzUSHy87UL8fucIoBBGxpgT1N0c3eWe52H31FMaB8FZrLrJ6p9
pm3jbHSB8Gs4mu7XCEv2Np1PlGLWS8Kt6RZcB2paQf@G9nycu8cr3Qj@,XV4mNxXtFa6btVt@72Kwcii
xwcF4rW6uQuTcrE64,4nLpa@O@utfzL@E7ge8BsMPz3BOAtpBM9eJfwJaJBPjVpTTZytSNWeA9wsJbID
iHXht6hSaBr1O3Ui4HXlhnfH36bF2Ncqd2wwZ,VooDtSF8zJs4,HGXYztrl2,HejYVW@u@q0oIoerZsi
SJX,ks1BLyT79jcJbYIPXbclYthqNhsxE0q4VYgwMmAkAp7,sUbxQj75Iu6MRsPi@KY63FJIECL92yY7
7XjZSCi1ohpAzAKeOQHpvo,3g8NP6OijmNrGYFcdwGYX0Mp9Vm9kgLHEXYdCd1DwkPNqq8lCoTXEid6W
BLlq9P7bOYXVBBOXy8QqWVbYBTO6CvOBBzcF9Uv2qFTLd@2Iqrl4hRxJgG@HgoqFV51xhkcBmPLioPeJ
gu5q,gI4VdDGuqRC81LvTg0yFgFn3qTzwCFNwTpnlum@ulXbEqoT@CqUH1zyE1LWgVddYM6LfuwL1JZW
ae9kmdqqVpvkSVuE8a
You need to do some work. Here is a [url http://forum.java.sun.com/thread.jsp?forum=54&thread=406096&message=1778521#1778521]hint[/url]
Ok,Thanks for the hint tooI have been doing a mountain of work, but unfortunately I came to a sticking point in my last post. I will not bother you any more as after another hour I figured it out...
Read this section from the Swing tutorial in "Using Tables":
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
JDK1.4 comes with a Swing demo that shows how to use JTables with the features you require:
jdk1.4.1\demo\jfc\SwingSet2>java -jar SwingSet2.jar
Search the forums for examples.
> I will not bother you any more...
That's not the point. It takes effort, time and energy to learn to program. If you just are given the answer each time you don't know something, you won't learn how to figure out the problems you have with the tools available (tutorials, API's and search engines). Learning how to use these will make the time you spend waiting for an answer on the forum seem intolerable. You will also learn the logic behind what you are doing better and be able to extrapolate what you know into things you don't understand yet. It makes programming a lot simpler.
By the way, that gobbledegook I posted was exactly what you asked for. All you have to do is unmunge it.
FYI, as soon as you posted the code for the simple JTable I immediately searched the API on it, and searched in Google for info relating to it. I also tried my hardest to understand what you had written and the API, but I got sorta lost in the technical language surrounding them both... I've never been good on that side of things, even in music. Took me four years of music to keep the information that a minim is always two beats in my head (I always played the music fine though). I also looked at the tutorials, but wherever I looked there was not explicit info on whether you could use the Renderer for images or not.
Even a friend who was helping me then was stumped...
I'm sorry I sounded lazy back there, also I realise that it takes years to become good at anything. I was not really asking for an answer, more a code snippet that described the implementation of a picture or a row header rather than full-blown code, so I could look at it, understand it, and apply my knowledge to the program.
Soon after you posted that hint post I was called out, so I had no time to read the hint...
Anyway, enough excuses. What's the French phrase for "he excuses accuses"?
Regards,
Platinum
sorry, "he who excuses accuses"
> By the way, that gobbledegook I posted was exactly> what you asked for. All you have to do is unmunge it.And you reckon he'll figure out how to do that?
lol, I'm not a total write-off. I unmunged it alright, neat code bbritta :)
in all fairness, the JTable is probably one of the toughest swing components to understand. some might argue that JTree is a contender ;) either way, if you can get either of these 2 down, you will have a pretty decent grasp of swing. they make extensive use of the MVC pattern, the event model, etc. and these are recurring themes in swing.
> in all fairness, the JTable is probably one of the
> toughest swing components to understand. some might
> argue that JTree is a contender ;) either way, if you
> can get either of these 2 down, you will have a pretty
> decent grasp of swing. they make extensive use of the
> MVC pattern, the event model, etc. and these are
> recurring themes in swing.
And?
Hi,
I have worked with one JPanel, that contains a square for every cell. (so not a JPanel for every cell)
For the paintComponent method, you could use code like:
private void drawFill(Graphics2D g) {
int m = dataModel.getRowCount();
int n = dataModel.getColumnCount();
double dh = getHeight() / (double)m;
double dw = getWidth() / (double)n;
for (int i=0; i< m; i++){
double y = i * dh;
for (int j=0; j< n; j++ ) {
double x = j * dw;
g.setPaint(getPaint(i,j)); // getPaint depends on the value of the array
g.fillRect((int)x, (int)y, (int)(dw)+1, (int)(dh)+1);
}
}
}
My data is contained in the dataModel with m rows and n columns. The
dataModel (it has methods like: getValueAt(i,j), setValueAt(i,j), ...) will fire an event if a value changes, so that the panel can repaint itself.
I hope this helps. (I do not know what you mean precisely with "layer different pictures on top of the different cells")
Hi guys,
Thanks for all those responses. Just looking at the table reminds me of a list... ARRRGGHH
As I have been doing Java for about a year, but concentrating on stuff other than Swing or the AWT, I have had no practical experience in either. Looking at the table code was a bit overwhelming at first but I think I'm getting the hang of it (thanks to all).
RE: layers
If you've ever used Adobe Photoshop, you'll know what I mean. It uses a background, and allows you to layer gradients, anything over the top of the original background.
What I mean by layering pictures over a cell was the same principle. I have already made the white background in my pictures (gifs) transparent, so all I need now is to put the cells of the table in my GUI, and make some code that allows me to put pictures on the cells whenever certain events occur. Sound simple, but it's not :)
And as for "Qui s'excuse, s'accuse" (thanks YATArchivist), my mom uses that expression all the time, but not on me necessarily...
Thanks again for all your help
Regards,
Platinum
