我试图让我的表在单击一个单元格时选择整行(这可以通过关闭列选择来完成),但是,我不希望您单击的特定单元格周围的额外粗边框突出显示.我希望这很容易,但显然它涉及渲染器所以我做了很多研究,我能得到的最接近的是:
JTable contactTable = new JTable(tableModel);
contactTable.setCellSelectionEnabled(true);
contactTable.setColumnSelectionAllowed(false);
contactTable.setRowSelectionAllowed(false);
contactTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// This renderer extends a component. It is used each time a
// cell must be displayed.
class MyTableCellRenderer extends JLabel implements TableCellRenderer {
// This method is called each time a cell in a column
// using this renderer needs to be rendered.
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
// 'value' is value contained in the cell located at
// …Run Code Online (Sandbox Code Playgroud) 我的GUI中有一个JTable组件,它显示psuedocode了一个算法.我想通过更改特定单元格的背景然后更改下面的单元格等来突出显示当前的执行行.
现在我的代码改变了JTable中所有单元格的背景,如下图所示:

我用来存档这个当前状态的代码如下:
class CustomRenderer extends DefaultTableCellRenderer
{
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
JLabel d = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if((row == 0) && (column == 0))
d.setBackground(new java.awt.Color(255, 72, 72));
return d;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我调用jTable2.setDefaultRenderer(String.class, new CustomRenderer());我的构造函数.
我认为:
如何修复我的代码,以便只有单元格(0,0)被着色?
我想为jTable中的特定行着色..我使用此代码为列做了,
private class CustomCellRenderer extends DefaultTableCellRenderer {
/* (non-Javadoc)
* @see
javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
*/
@Override
public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) {
Component rendererComp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,row, column);
//Set foreground color
// rendererComp.setForeground(Color.red);
//Set background color
rendererComp .setBackground(Color.pink);
return rendererComp ;
}
}
Run Code Online (Sandbox Code Playgroud)
我用上面的代码调用,
jTable1.getColumnModel().getColumn(3).setCellRenderer(new CustomCellRenderer());
Run Code Online (Sandbox Code Playgroud)
但是我想对jTable中的行做同样的事情.在行的情况下没有getColumnModel()或getColumn().那么这样做的替代方法是什么?我是通过使用Java Swing在Netbeans中完成的.
我对 Java 和编程本身比较陌生,所以请原谅我的问题。我正在尝试做的是以下内容:
我正在制作一个簿记程序。在显示收入/结果的列上,我希望当用户输入负数(例如 -1.150 欧元)时,数字变为红色(或任何颜色,但大多数簿记程序使用红色)。仅该列上的特定单元格。我还没有开始使用代码,所以我不能在这里输入。我也不需要它右对齐,因为我已经这样做了。
附注。抱歉,如果此帖子/问题已经存在,我进行了搜索,但没有找到对我有多大帮助的内容。
我有一个JTable并在JTable和其他属性中设置图片作为背景我使用此代码.
tblMainView= new JTable(dtModel){
public Component prepareRenderer(TableCellRenderer renderer, int row,
int column)
{
Component c = super.prepareRenderer( renderer, row, column);
// We want renderer component to be transparent so background image
// is visible
if( c instanceof JComponent )
((JComponent)c).setOpaque(false);
return c;
}
ImageIcon image = new ImageIcon( "images/watermark.png" );
public void paint( Graphics g )
{
// First draw the background image - tiled
Dimension d = getSize();
for( int x = 0; x < d.width; x += image.getIconWidth() …Run Code Online (Sandbox Code Playgroud) 我有一个JTable并添加了排序.现在,JTable有5列,日期字段中的第2列转换为DD/MM/YYYY,并显示在单元格的JTextField中.
当我将它排序为字符串并且我将日期混淆时,如何更改该特定列的排序行为?
例如.按照ASC顺序排序后,我得到了这个:
01/02/2012
01/03/2011
01/04/2011
01/05/2011
01/06/2011
01/07/2011
01/08/2011
01/09/2011
01/10/2011
01/12/2011
Run Code Online (Sandbox Code Playgroud)
这是错的,我应该得到结果
01/03/2011
01/04/2011
01/05/2011
01/06/2011
01/07/2011
01/08/2011
01/09/2011
01/10/2011
01/12/2011
01/02/2012
Run Code Online (Sandbox Code Playgroud)
我的代码现在看起来像这样排序
List<SortKey> sortKeys = new ArrayList<SortKey>();
sortKeys.add(new SortKey(2, SortOrder.ASCENDING));
table.getRowSorter().setSortKeys(sortKeys);
Run Code Online (Sandbox Code Playgroud)
我应该仅针对该特定列更改哪些内容?