java 如何为 Jtable 中的行添加边框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1772764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you add a border to a row in a Jtable?
提问by Ryan Elkins
I have a Jtable and I want to highlight a row by adding a border to the row. I have extended a DefaultTableCellRendererand I figure the work needs to be done in the getTableCellRendererComponentmethod.
我有一个 Jtable,我想通过向行添加边框来突出显示一行。我已经扩展了 aDefaultTableCellRenderer并且我认为需要在getTableCellRendererComponent方法中完成工作。
I'm guessing that since there doesn't seem to be a concept of a row that I need to create a custom border for the individual cells in the row. Something like a left side, top, and bottom for the first cell, a top and bottom for all the inner cells, and a top, bottom, and right side for the last cell in the row. I'm having problems finding out how to go about actually executing the thought process. I'm not sure how to use the setBorder()method or if that's even the direction I need to take.
我猜是因为似乎没有我需要为行中的单个单元格创建自定义边框的行的概念。类似于第一个单元格的左侧、顶部和底部,所有内部单元格的顶部和底部,以及行中最后一个单元格的顶部、底部和右侧。我在找出如何实际执行思考过程时遇到问题。我不确定如何使用该setBorder()方法,或者这是否是我需要采取的方向。
采纳答案by broschb
You have the correct idea in mind, you will need to set the border on the label in the cellrenderer depending on where it is in the table(i.e. edge, center etc).
您有正确的想法,您需要根据它在表格中的位置(即边缘、中心等)在 cellrenderer 中设置标签上的边框。
Take a look at matteborder. You can specify which areas to draw a border along w/ width and color.
看看matteborder。您可以指定沿宽度和颜色绘制边框的区域。
回答by camickr
I would not create a custom renderer for this. Yes it will work if all your data is of the same type. But what happens when you start to mix Strings, with Dates and Integers and Booleans which all use different renderers? Then you would need to create 4 custom renderers.
我不会为此创建自定义渲染器。是的,如果您的所有数据都属于同一类型,它将起作用。但是当你开始将字符串、日期、整数和布尔值混合使用时会发生什么,它们都使用不同的渲染器?然后您需要创建 4 个自定义渲染器。
The better approach is to override the prepareRenderer(...) method JTable so you can add the code in one place. Here is an example to get you started. In reality you would want to use a CompoundBorder that contains a MatteBorder for the top/bottom and and EmptyBorder for the left/right and you would create a single instance of the Border.
更好的方法是覆盖 prepareRenderer(...) 方法 JTable,以便您可以在一处添加代码。这是一个帮助您入门的示例。实际上,您可能希望使用包含用于顶部/底部的 MatteBorder 和用于左侧/右侧的 EmptyBorder 的 CompoundBorder,并且您将创建 Border 的单个实例。
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;
import javax.swing.border.*;
public class TablePrepareRenderer extends JFrame
{
JTable table;
public TablePrepareRenderer()
{
Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"};
Object[][] data =
{
{"Buy", "IBM", new Double(1000), new Double(80.5), Boolean.TRUE},
{"Sell", "MicroSoft", new Double(2000), new Double(6.25), Boolean.TRUE},
{"RSell", "Apple", new Double(3000), new Double(7.35), Boolean.TRUE},
{"Buy", "Nortel", new Double(4000), new Double(20), Boolean.TRUE}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
table = new JTable( model )
{
// Returning the Class of each column will allow different
// renderers to be used based on Class
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}
public Component prepareRenderer(
TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);
JComponent jc = (JComponent)c;
// Color row based on a cell value
// Alternate row color
if (!isRowSelected(row))
c.setBackground(row % 2 == 0 ? getBackground() : Color.LIGHT_GRAY);
else
jc.setBorder(new MatteBorder(1, 0, 1, 0, Color.RED) );
// Use bold font on selected row
return c;
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.changeSelection(0, 0, false, false);
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
}
public static void main(String[] args)
{
TablePrepareRenderer frame = new TablePrepareRenderer();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
回答by Katu
I agree with > camickr the best way to go is to override the prepareRendere method. The following code will create a border for a row with a selected cell:
我同意 > camickr 最好的方法是覆盖 prepareRendere 方法。以下代码将为具有选定单元格的行创建边框:
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
JComponent jc = (JComponent)c;
if (isRowSelected(row)){
int top = (row > 0 && isRowSelected(row-1))?1:2;
int left = column == 0?2:0;
int bottom = (row < getRowCount()-1 && isRowSelected(row + 1))?1:2;
int right = column == getColumnCount()-1?2:0;
jc.setBorder(BorderFactory.createMatteBorder(top, left, bottom, right, this.getSelectionBackground()));
}
else
jc.setBorder(null);
return c;
}

