Reputation: 105
I'm writing a program to play a game of cards. I already made the game work, and effectively play it, but now I have decided to add images of cards (right now it works but using the names of cards, like "As of Spades", instead of an icon to represent them).
In my program, I use JTable
s to organize the cards and for the selection of them in the various JDialog
s I put them in (one dialog for swapping the cards in hand, another to select card to discard, etc.)
What I tried, and I personally like how it'd work, its to make a JTable
with 8 columns and 1 row of cells for each card. And inside of each cell put a image of the card. Then I'd select a cell to select a card, or use a JButtonGroup
outside of the table.
DefaultTableModel dtModel = new DefaultTableModel(COL_NAMES, 0) {
@Override
public Class<?> getColumnClass(int column) {
if (getRowCount() > 0)
return getValueAt(0, column).getClass();
return super.getColumnClass(column);
}
};
//add the columns to the model:
if (dtModel.getColumnCount() == 0) {
for (int i = 0; i < COLS; i++) {
dtModel.addColumn(COL_NAMES[i]);
}
}
//add a row to the model:
if (dtModel.getRowCount() == 0) {
Object[] data = {new JLabel(), new JLabel(), new JLabel(), new JLabel(), new JLabel(), new JLabel(), new JLabel(), new JLabel()};
dtModel.addRow(data);
}
jTable1.setModel(dtModel);
//set the size of the table, but I think I got it wrong:
jScrollPane1.setSize(400, jScrollPane1.getColumnHeader().getHeight() + jTable1.getRowHeight());
//here is the image I'm using:
ImageIcon ii = new ImageIcon("C:\\Users\\DeRipper\\Pictures\\Naipes\\oros_1s.jpg");
//the loop where I set the image in all the cells. I scale the image into a smaller size:
for (int i = 0; i < COLS; i++)
jTable1.setValueAt(new ImageIcon(ii.getImage().getScaledInstance(50, 65, Image.SCALE_DEFAULT)), 0, i);
Where "C:\\Users\\DeRipper\\Pictures\\Naipes\\oros_1s.jpg"
is the path to the card file. I was first testing my code by putting the same card image for all the cells.
At first glance I got the desired result, the images were displayed correctly, but when clicking on them a couple of times the table would stop rendering them and instead showed the toString()
value of the images:
And then the images would not show on the table again. I just need the user to be able to click on the images and not disappear.
Well, thanks for reading.
DeR.
Upvotes: 0
Views: 449
Reputation: 324118
Object[] data = {new JLabel(), new JLabel(), new JLabel(), new JLabel(), new JLabel(), new JLabel(), new JLabel(), new JLabel()};
dtModel.addRow(data);
Don't add JLabel
s to the model. Instead you need to add an ImageIcon
.
Then the JTable
will use the Icon renderer to display the image.
but when clicking on them a couple of times the table would stop rendering them and instead showing the "toString()" value of the images:
If you are editing the cells, then the default editor will just save the toString()
representation of the object back to the TableModel. So you might want to override the isCellEditable(...)
method to turn editing off. Otherwise you would need a custom editor that knows how to edit and save an ImageIcon
.
Upvotes: 3