gpt4 book ai didi

java - 将 JComboBox 设置为表列不起作用

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

立即使用 MCVE 进行编辑:

我想将一个 JTable 单元格视为 JComboBox。我用 Oracle 教程尝试过,但不起作用。本教程没有说明如何更改表模型才能使其正常工作。所以这是代码:

表模型:

import javax.swing.*;
import javax.swing.table.AbstractTableModel;

public class TableModel extends AbstractTableModel {

@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return 2;
}

@Override
public int getRowCount() {
// TODO Auto-generated method stub
return 2;
}

@Override
public Object getValueAt(int row, int col) {
if (row == 0) {
return 2;
} else if (col == 1 ) {
return 1;
} else {
return null;
}
}

}

以及显示组件的 View :

import java.awt.BorderLayout;

import javax.swing.*;
import javax.swing.table.TableColumn;

public class View extends JFrame {
public View() {
super();
TableModel tableModel = new TableModel();
JTable testTable = new JTable(tableModel);

JScrollPane scrollpane = new JScrollPane(testTable);
// scrollpane.setOpaque(false);
// scrollpane.getViewport().setOpaque(false);
JPanel testTablePanel = new JPanel();
testTablePanel.setBorder(
BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Testkonfiguration"));
testTablePanel.setLayout(new BorderLayout());
testTablePanel.add(scrollpane);

TableColumn sportColumn = testTable.getColumnModel().getColumn(1);

JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Chasing toddlers");
comboBox.addItem("Speed reading");
comboBox.addItem("Teaching high school");
comboBox.addItem("None");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));

add(scrollpane);

pack();
setVisible(true);
}
public static void main(String[] args) {
new View();
}

}

我必须在表模型中更改哪些内容才能使其正常工作?

最佳答案

在您的示例中,您没有使 1 列可编辑。您首先需要执行此操作,例如在您的模型中:

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1;
}

而且你的模型不能很好地处理数据,实际上没有办法获取数据并且实际上是固定的,因此编辑器不可能产生任何效果。您需要重写 setValueAt 并让它更新模型的核心。

例如使用您的简单示例:

import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;

public class View extends JFrame {
public View() {
super();
TableModel tableModel = new TableModel();
JTable testTable = new JTable(tableModel);

JScrollPane scrollpane = new JScrollPane(testTable);
// scrollpane.setOpaque(false);
// scrollpane.getViewport().setOpaque(false);
JPanel testTablePanel = new JPanel();
testTablePanel.setBorder(BorderFactory
.createTitledBorder(BorderFactory.createEtchedBorder(), "Testkonfiguration"));
testTablePanel.setLayout(new BorderLayout());
testTablePanel.add(scrollpane);

TableColumn sportColumn = testTable.getColumnModel().getColumn(1);

JComboBox<String> comboBox = new JComboBox<>();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Chasing toddlers");
comboBox.addItem("Speed reading");
comboBox.addItem("Teaching high school");
comboBox.addItem("None");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));

setDefaultCloseOperation(EXIT_ON_CLOSE);
add(scrollpane);

pack();
setVisible(true);
}

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

}

class TableModel extends AbstractTableModel {
Object[][] innerModel = new Object[][]{{2, 1}, {2, null}};

public TableModel() {

}

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

@Override
public int getRowCount() {
return innerModel.length;
}

@Override
public Object getValueAt(int row, int col) {
return innerModel[row][col];
}

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
innerModel[rowIndex][columnIndex] = aValue;
fireTableCellUpdated(rowIndex, columnIndex);
}

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1;
}

}

关于java - 将 JComboBox 设置为表列不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46365096/

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