gpt4 book ai didi

java - 在选择时以细线十字图案突出显示相关细胞

转载 作者:行者123 更新时间:2023-12-02 11:14:40 25 4
gpt4 key购买 nike

我想向 JXTable 添加细线交叉突出显示。但我可以突出显示当前行或当前列;永远不会两者兼而有之。

我现在有的是红色部分,黄色部分是我要添加的。

enter image description here

低于 SSCCE 1 .

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

import org.jdesktop.swingx.JXTable;

public class Application
{

public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
JFrame frame = new JFrame();
JXTable table = new JXTable(new CustomTableModel());

boolean highlightRow = false;
table.setRowSelectionAllowed(highlightRow);
table.setColumnSelectionAllowed(!highlightRow);

table.setDefaultRenderer(Object.class, new CustomTableCellRenderer());

frame.add(new JScrollPane(table));
frame.setVisible(true);
frame.pack();
}
});
}

public static class CustomTableCellRenderer extends DefaultTableCellRenderer
{

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (row == table.getSelectedRow()) { component.setBackground(Color.YELLOW); }
if (column == table.getSelectedColumn()) { component.setBackground(Color.RED); }
return component;
}
}

static class CustomTableModel extends AbstractTableModel
{
private final List<Object[]> data = new ArrayList<>();

public CustomTableModel()
{
data.add(new Object[] {1, 2, 3, "A", "Collection", "of", "Random", "Strings", 9, 10});
data.add(new Object[] {1, 2, 3, "A", "Collection", "of", "Random", "Strings", 9, 10});
data.add(new Object[] {1, 2, 3, "A", "Collection", "of", "Random", "Strings", 9, 10});
data.add(new Object[] {1, 2, 3, "A", "Collection", "of", "Random", "Strings", 9, 10});
}

@Override
public int getRowCount() { return data.size(); }

@Override
public int getColumnCount() { return 10; }

@Override
public Object getValueAt(int rowIndex, int columnIndex) { return data.get(rowIndex)[columnIndex]; }
}

}

更新

使用 @camickr 提供的代码和建议会产生以下结果:

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

import org.jdesktop.swingx.JXTable;

public class Application
{

public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
JFrame frame = new JFrame();

DefaultTableModel model = new DefaultTableModel(5, 10);
JXTable table = new JXTable(model)
{
@Override
public Component prepareRenderer(
TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);

if (column == getSelectedColumn()) { c.setBackground(Color.RED); }
else if (row == getSelectedRow()) { c.setBackground(Color.YELLOW); }
else c.setBackground(getBackground());

return c;
}
};

frame.add(new JScrollPane(table));
frame.setVisible(true);
frame.pack();
}
});
}

}

但不幸的是我现在遇到了工件问题。类似于下图(取自仅红色版本)。

enter image description here

<小时/>

我在 Windows 10.0.16299 Build 16299 上使用 Java 1.8.0_74。JXTable 来自

        <dependency>
<groupId>org.swinglabs.swingx</groupId>
<artifactId>swingx-all</artifactId>
<version>1.6.5-1</version>
</dependency>

最佳答案

它似乎在使用 JTable 时有效(尽管如果您希望列具有优先级,则需要更改 if 语句的顺序)。因此,JXTable 似乎做了一些额外的渲染,从而导致了问题。

所以这是一种基于 Table Row Rendering 的替代方法这似乎适用于 JXTable:

JXTable table = new JXTable(new CustomTableModel())
{
public Component prepareRenderer(
TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);

if (column == getSelectedColumn()) { c.setBackground(Color.RED); }
else if (row == getSelectedRow()) { c.setBackground(Color.YELLOW); }
else c.setBackground(getBackground());

return c;
}
};

上面的代码替换了自定义渲染器:

//table.setDefaultRenderer(Object.class, new CustomTableCellRenderer());

注意:在我的建议的“MCVE”中,您删除了:

boolean highlightRow = false;
table.setRowSelectionAllowed(highlightRow);
table.setColumnSelectionAllowed(!highlightRow);
为什么?我只给了你上面2个改动。

这就是为什么您需要发布适当的“MCVE”,以便我们可以确保您正确实现了该建议。

关于java - 在选择时以细线十字图案突出显示相关细胞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50375113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com