gpt4 book ai didi

java - JXTable 中非常不寻常的单元格突出显示

转载 作者:行者123 更新时间:2023-11-30 09:00:58 24 4
gpt4 key购买 nike

我设置了一个 JXTable 以用某种颜色绘制选定的单元格。但是,只要选定的单元格位于顶行,就好像表格中的所有单元格都被绘制了一样。

谁能帮我理解为什么会出现这个问题,以及如何解决这个问题?

下面是演示此问题的最小工作示例。

附加信息:每次调用 Paint() 时都会重新应用 DefaultTableCellRenderer,因为它是我在完整程序中使用的 CustomTableCellRenderer 的占位符。有趣的是,如果我删除 DefaultTableCellRenderer 行,我的问题就解决了。为什么是这样?当然,如果未指定渲染器,则仍然使用默认值??

import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.ColorHighlighter;
import org.jdesktop.swingx.decorator.ComponentAdapter;
import org.jdesktop.swingx.decorator.HighlightPredicate;

public class MainClass {
public static void main(String[] args) {
JFrame frame = new JFrame();
CustomTableModel tableModel = new CustomTableModel();
JXXTable table = new JXXTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
SelectionListener listener = new SelectionListener(table);
table.getSelectionModel().addListSelectionListener(listener);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scrollPane);
frame.setVisible(true);
frame.pack();
}
}

class JXXTable extends JXTable {
public JXXTable(CustomTableModel model){ super(model); }
public void Paint(){
this.setDefaultRenderer(Object.class, new DefaultTableCellRenderer());
this.setHighlighters();
HighlightPredicate predicate = new HighlightPredicate() {
public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {
return adapter.hasFocus();
}
};
this.addHighlighter(new ColorHighlighter(predicate, null, null, new Color(115,164,209), Color.WHITE));
}
}

class SelectionListener implements ListSelectionListener {
JXXTable table;
public SelectionListener(JXXTable table) { this.table = table; }
public void valueChanged(ListSelectionEvent e) { table.Paint(); }
}

class CustomTableModel extends AbstractTableModel {
ArrayList<Object[]> al;

public CustomTableModel() {
al = new ArrayList<Object[]>();
Object[] row = {1,2,3,"A","Collection","of","Random","Strings",9,10}; al.add(row);
Object[] row2 = {11,12,13,"Another","Collection","Of","Random","Strings",19,20}; al.add(row2);
Object[] row3 = {11,12,13,"Another","Collection","Of","Random","Strings",19,20}; al.add(row3);
Object[] row4 = {11,12,13,"It","just","gets","more","random...",19,20}; al.add(row4);
}

public int getRowCount() { return al.size(); }
public int getColumnCount() { return 10; }
public Object getValueAt(int rowIndex, int columnIndex) { return al.get(rowIndex)[columnIndex]; }
}

最佳答案

我“认为”部分问题与每次选择更改时添加另一个荧光笔有关。

相反,考虑仅在初始化表格时才添加高亮...

话虽如此,也没有必要扩展 JXTable,您实际上并没有向该类添加任何新功能。相反,您可以创建某种工厂来配置您想要的表

enter image description here

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.ColorHighlighter;
import org.jdesktop.swingx.decorator.HighlightPredicate;

public class MainClass {

public static void main(String[] args) {
new MainClass();
}

public MainClass() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
CustomTableModel tableModel = new CustomTableModel();
JXTable table = new JXTable(tableModel);
table.setDefaultRenderer(Object.class, new CustomTableCellRenderer());
HighlightPredicate predicate = new HighlightPredicate() {
@Override
public boolean isHighlighted(Component cmpnt, org.jdesktop.swingx.decorator.ComponentAdapter ca) {
System.out.println(ca.getComponent());
return ca.hasFocus();
}
};
table.addHighlighter(new ColorHighlighter(predicate, null, null, new Color(115, 164, 209), Color.WHITE));

JScrollPane scrollPane = new JScrollPane(table);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scrollPane);
frame.setVisible(true);
frame.pack();
}
});
}

class CustomTableCellRenderer extends DefaultTableCellRenderer {

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
return (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
}

class CustomTableModel extends AbstractTableModel {

ArrayList<Object[]> al;

public CustomTableModel() {
al = new ArrayList<Object[]>();
Object[] row = {1, 2, 3, "A", "Collection", "of", "Random", "Strings", 9, 10};
al.add(row);
Object[] row2 = {11, 12, 13, "Another", "Collection", "Of", "Random", "Strings", 19, 20};
al.add(row2);
Object[] row3 = {11, 12, 13, "Another", "Collection", "Of", "Random", "Strings", 19, 20};
al.add(row3);
Object[] row4 = {11, 12, 13, "It", "just", "gets", "more", "random...", 19, 20};
al.add(row4);
}

public int getRowCount() {
return al.size();
}

public int getColumnCount() {
return 10;
}

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

关于java - JXTable 中非常不寻常的单元格突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26569532/

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