gpt4 book ai didi

java - 具有相同自定义模型的两个 JXTables - 如何使单元格在一个表中可编辑但在第二个表中不可编辑?

转载 作者:行者123 更新时间:2023-11-30 06:27:15 26 4
gpt4 key购买 nike

我有一个自定义表模型,我想在一个 JXTable 中编辑其数据,但在第二个 JXTable 中仅供查看。这可以在没有两个独立模型的情况下完成吗?有什么方法可以覆盖仅供查看的表的 model.isCellEditable 吗?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
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 SSCCE extends JPanel {

private JSplitPane splitPane;
private JXTable viewTable, editTable;

private class CustomModel extends AbstractTableModel {

public static final int SPORT_COL = 0;
public static final int EQUIPMENT_COL = 1;
private final String[] COLUMN_NAMES = {
"Sport",
"Equipment"
};
private Map<String, String> sports;
private List<String> set;

public CustomModel() {
sports = new TreeMap<String, String>();
sports.put("Rugby", "Headguard");
sports.put("Hurling", "Sliotar");
sports.put("Tennis", "Racket");
set = new ArrayList<String>(sports.keySet());
}

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

public int getColumnCount() {
return COLUMN_NAMES.length;
}

@Override
public String getColumnName(int columnIndex) {
return COLUMN_NAMES[columnIndex];
}

public Object getValueAt(int rowIndex, int columnIndex) {
String sport = set.get(rowIndex);
switch (columnIndex) {
case SPORT_COL:
return sport;
case EQUIPMENT_COL:
return sports.get(sport);
}
return null;
}

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (columnIndex == EQUIPMENT_COL) {
if (aValue != null) {
String sport = (String) getValueAt(rowIndex, SPORT_COL);
String equip = (String) aValue;
sports.put(sport, equip);
fireTableDataChanged();
}
}
}

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == EQUIPMENT_COL;
}
}
private CustomModel model;

public SSCCE() {
super();

model = new CustomModel();
viewTable = new JXTable(model);
editTable = new JXTable(model);
editTable.addHighlighter(new ColorHighlighter(new HighlightPredicate() {
@Override
public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {
return adapter.isEditable();
}
}, Color.GREEN.brighter(), Color.BLACK));

JPanel panelLeft = new JPanel(new BorderLayout(0, 10));
panelLeft.add(new JLabel("Editable"), BorderLayout.NORTH);
panelLeft.add(new JScrollPane(editTable));
JPanel panelRight = new JPanel(new BorderLayout(0, 10));
panelRight.add(new JLabel("How to make non-editable?"), BorderLayout.NORTH);
panelRight.add(new JScrollPane(viewTable));

splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panelLeft, panelRight);
add(splitPane);
}

public static void main(String args[]) {
JFrame frame = new JFrame("One Model | Two Views");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SSCCE(), BorderLayout.CENTER);
frame.setSize(360, 240);
frame.setVisible(true);
}
}

最佳答案

SwingX 支持在 View 层配置可编辑性。无需触摸您的模型,使其保持可编辑状态:

viewTable = new JXTable(model);
viewTable.setEditable(false);
editTable = new JXTable(model);

关于java - 具有相同自定义模型的两个 JXTables - 如何使单元格在一个表中可编辑但在第二个表中不可编辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13493431/

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