gpt4 book ai didi

java - JTable 关注整行而不是单元格

转载 作者:行者123 更新时间:2023-12-01 09:17:24 26 4
gpt4 key购买 nike

有没有办法强制 JTable 关注整行而不是单个单元格?我不是在谈论行选择突出显示,只是在谈论焦点边框,我希望包括焦点行上的所有单元格。

更新:

具有不可编辑单元格和删除行功能的表格:

public class TableTest {

private static void createAndShowGUI() {

int rows = 20;
int cols = 2;
String[] headers = { "Column 1", "Column 2" };
String[][] data = new String[rows][cols];

for (int j = 0; j < rows; j++)
for (int k = 0; k < cols; k++)
data[j][k] = "item " + (j * cols + k + 1);

JTable table = new JTable();

DefaultTableModel tableModel = new DefaultTableModel(data, headers) {
// Disable editing of the cells
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
table.setModel(tableModel);
table.setShowGrid(false);
table.setIntercellSpacing(new Dimension(0,0));

// key binding to remove rows
InputMap inputMap = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
ActionMap actionMap = table.getActionMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "REMOVE");
actionMap.put("REMOVE", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
int[] rows = table.getSelectedRows();
for (int i = rows.length - 1; i >= 0; i--) {
tableModel.removeRow(rows[i]);
}
}
});

JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(table));
f.setSize(500, 500);
f.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {}
createAndShowGUI();
}
});
}
}

选定行上的“焦点边框”:

enter image description here

“焦点边框”,未选择任何行:

enter image description here

Windows LAF 中的“焦点边框”:

enter image description here

我希望这个“焦点边框”包含该行的所有单元格(当可见时)。

下面是相同的代码,但我添加了 @camickr 的部分代码:

public class TableTest {

private static void createAndShowGUI() {

int rows = 20;
int cols = 2;
String[] headers = { "Column 1", "Column 2" };
String[][] data = new String[rows][cols];

for (int j = 0; j < rows; j++)
for (int k = 0; k < cols; k++)
data[j][k] = "item " + (j * cols + k + 1);


// ADDED CODE
JTable table = new JTable() {

private Border outside = new MatteBorder(1, 0, 1, 0, Color.BLACK);
private Border inside = new EmptyBorder(0, 1, 0, 1);
private Border border = new CompoundBorder(outside, inside);

@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
JComponent jc = (JComponent) c;
// Add a border to the selected row
if (isRowSelected(row)) jc.setBorder(border);
return c;
}
};


DefaultTableModel tableModel = new DefaultTableModel(data, headers) {
// Disable editing of the cells
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
table.setModel(tableModel);
table.setShowGrid(false);
table.setIntercellSpacing(new Dimension(0,0));

// key binding to remove rows
InputMap inputMap = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
ActionMap actionMap = table.getActionMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "REMOVE");
actionMap.put("REMOVE", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
int[] rows = table.getSelectedRows();
for (int i = rows.length - 1; i >= 0; i--) {
tableModel.removeRow(rows[i]);
}
}
});

JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(table));
f.setSize(500, 500);
f.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {}
createAndShowGUI();
}
});
}
}

这会在整行周围添加边框(但没有左/右插入来包含所有单元格)。这不是“焦点边框/矩形”,它是仅出现在选定行周围的边框。当我删除一行时,所选行将被清除,并且焦点边框会重新出现。

请注意,我不想想要隐藏焦点边框(我知道如何做到这一点),我想保持它的功能,但要包含该行的所有单元格而不是一个单元格当它变得可见时。

最佳答案

I just want to display FocusBorder around the entire row.

看看Table Row Rendering

它展示了一种在行上而不是单个单元格上放置边框的方法。

关于java - JTable 关注整行而不是单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40444820/

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