[英]Icon in JTable Cell Causing Misrendering
I'm trying to use a custom renderer to set the background of individual cells to a given image. 我正在尝试使用自定义渲染器将单个单元格的背景设置为给定图像。 In this case, I'm giving a chessboard wooden squares.
在这种情况下,我给一个棋盘上的木制方块。
Here's what it looks like before: 这是以前的样子:
Here's what it looks like afterwards: 如下所示:
EDIT : With a little experimentation, it seems that every square is given the wood icon. 编辑 :经过一点试验,似乎每个正方形都被赋予了木质图标。 Pieces can still be moved, resulting in this: (put it in the comments because I can't post more than 2 links)
片段仍然可以移动,导致以下结果:(将其放入评论中,因为我发布的链接不能超过2个)
In my code, I've merely replaced the setBackground(darkSquare)
with setIcon(wood)
. 在我的代码中,我仅用
setIcon(wood)
替换了setBackground(darkSquare)
setIcon(wood)
。
@SuppressWarnings("serial")
public class BoardCellRenderer extends DefaultTableCellRenderer {
private ArrayList<Coordinate> possibleMoves = new ArrayList<Coordinate>();
private ImageIcon wood = new ImageIcon("resources/images/light_square.png");
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setHorizontalAlignment(SwingConstants.CENTER);
Color darkSquare = new Color(125, 125, 125);
//pattern for the chessboard look
if(row % 2 == 0 && column % 2 == 1)
setBackground(darkSquare);
//setIcon(wood);
else if(row % 2 == 1 && column % 2 == 0)
setBackground(darkSquare);
//setIcon(wood);
else
setBackground(Color.WHITE);
for(Coordinate move : possibleMoves)
if(column == move.getX() && row == move.getY()){
setBackground(new Color(255, 51, 51, 50));
System.out.println("Highlighting (" + row + ", " + column + ")");
}
if(hasFocus){
setBorder(new MatteBorder(2, 2, 2, 2, Color.RED));
System.out.println("hasFocus [array]: " + row + ", " + column);
System.out.println("hasFocus [coordinate]: " + column + ", " + row);
}
if(isSelected)
setBorder(new MatteBorder(2, 2, 2, 2, Color.BLUE));
return this;
}
public void setPossibleMoves(ArrayList<Coordinate> possibleMoves){
this.possibleMoves = possibleMoves;
}
}
it seems that every square is given the wood icon.
似乎每个正方形都带有木质图标。
The renderer remembers its last state. 渲染器记住其最后状态。
So maybe something like: 所以也许是这样的:
// set the default values
setBackground(Color.WHITE);
setIcon(null);
if(row % 2 == 0 && column % 2 == 1)
//setBackground(darkSquare);
setIcon(wood);
else if(row % 2 == 1 && column % 2 == 0)
//setBackground(darkSquare);
setIcon(wood);
Of course you will still have a problem when you want to display a chess piece on top of the wood as you will want to render two icons, one for the background and one for the chess piece. 当然,当您要在木头上显示国际象棋时仍然会有问题,因为您将要渲染两个图标,一个用于背景,一个用于国际象棋。
I'm not sure if a JTable is the best component to use for this. 我不确定JTable是否是为此最佳的组件。 It might be easier to use a JPanel with a grid of either JPanel/JLabel to represent the wood square.
使用带有JPanel / JLabel网格的JPanel表示木正方形可能会更容易。 Then you add a JLabel with an Icon of the chess piece to the square.
然后,将带有国际象棋图标的JLabel添加到广场。 This posting of a simple Chess Board might give you some ideas.
张贴一个简单的国际象棋棋盘可能会给您一些想法。
Edit: 编辑:
Would that be why the cell looks like it's printing ...?
那就是为什么单元格看起来像是在打印...?
Probably. 大概。 The default for a JLabel is to display the icon followed by the text.
JLabel的默认设置是显示图标,然后显示文本。
You can change this behaviour by using: 您可以使用以下方式更改此行为:
setHorizontalTextPosition(JLabel.CENTER);
setVerticalTextPosition(JLabel.CENTER);
in the constructor of your renderer. 在渲染器的构造函数中。 Then the text will be centered over the icon.
然后,文本将居中于图标上方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.