有什麼辦法可以強制JTable專注於整個行而不是單個單元格嗎?我不是在談論行選擇的亮點,只是關於我想在所關注的行中包含所有單元格的焦點邊界。JTable專注於整個行而不是單元格
UPDATE:
表不可編輯的細胞和刪除行的功能:
「:
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();
}
});
}
}
上選擇行 」聚焦邊境「焦點邊框「,沒有選擇行:
在Windows LAF「聚焦邊界」:
我想這個 「邊界集中」 到(可見時),包括該行的所有單元格。
下面是相同的代碼,但我已經添加的@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();
}
});
}
}
這增加了整個行周圍的邊框(但沒有左/右插圖包括所有細胞) 。這不是「焦點邊框/矩形」,它是僅出現在選定行周圍的邊框。當我刪除一行時,所選行被清除並且焦點邊框重新出現。
請注意,我不想隱藏對焦邊界(我知道如何做到這一點),我要保持它的功能一樣,但包括該行,而不是一個小區的所有細胞,當它變得可見。
整個行焦點的意圖是什麼?鍵盤輸入應該去哪裏?或者這只是一個顯示問題,你想要在整個行周圍顯示'FocusBorder'? – kgeorgiy
我只想在整行上顯示FocusBorder。這些單元格是不可編輯的。 – AndroidX
不需要。重點在於顯示開始鍵入時鍵盤輸入的位置。做你想要的東西違背慣例,而不是被設計成框架。唯一的選擇是在每個列的自定義單元格渲染器中自己繪製焦點邊框。在一般情況下,由於列可以由用戶重新排列,所以這很棘手。更簡單的解決方案是根本不使用JTable,而是使用帶有單個定製單元格渲染器的JList。 –