gpt4 book ai didi

java - 在 Jtable 中添加 JCombobox 并在 Swing java 中获取行和列

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:47:03 29 4
gpt4 key购买 nike

我有一个 Jtable,我在其中添加了这样的 JComobox。

TableColumn sportColumn = jTable1.getColumnModel().getColumn(2);
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));

并且我已经像这样添加了一个 jtable 的鼠标事件。

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     
// TODO add your handling code here:
try {
int row = jTable1.rowAtPoint(evt.getPoint());
int col = jTable1.columnAtPoint(evt.getPoint());

System.out.println("Row" + row + "Column" + col);

} catch (Exception e) {
e.printStackTrace();
}

}

我正在正确处理行和列。 enter image description here

但是当我单击我添加了 Jcombobox 的单元格时,它并没有给出该行和列的位置。我仍然在组合框单击事件中调用了表的 clickevent,但它在屏幕截图中给出了所有时间的第 0 行甚至第 0 列。

enter image description here

那么我该如何解决它才能拥有该行和该列?

最佳答案

在我看来,您有三个基本选项...

你可以...

CellEditorListener 附加到 DefaultCellEditor 并监视 editingStopped 事件...

editor.addCellEditorListener(new CellEditorListener() {
@Override
public void editingStopped(ChangeEvent e) {
System.out.println("Editing stopped");
TableCellEditor editor = (TableCellEditor) e.getSource();
Object value = editor.getCellEditorValue();
System.out.println("Selected value = " + value);
}

@Override
public void editingCanceled(ChangeEvent e) {
}
});

问题在于,无法真正确定已编辑的单元格。即使引用了 JTable,当您收到此事件时,该表也不再处于编辑模式...所以您所知道的只是编辑器的值...

你可以...

TableModelListener 附加到 TableModel...

model.addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
int type = e.getType();
switch (type) {
case TableModelEvent.UPDATE:
if (e.getFirstRow() - e.getLastRow() == 0) {
TableModel model = (TableModel) e.getSource();
int row = e.getFirstRow();
int col = e.getColumn();
System.out.println("Update " + row + "x" + col + " = " + model.getValueAt(row, col));
}
break;
}
}
});

这会让您知道单元格的值何时发生变化(以及许多其他事情,但这是我们感兴趣的),但它不会告诉您的是它发生变化的原因。

这种方法很好,因为它提供了查找值和单元格所需的所有信息。

你可以...

只需覆盖 TableModelsetValueAt 方法并提供您自己的某种通知...

虽然这有点复制 TableModelListener 的功能,但您可以设计事件信息,以便它提供更多相关信息,因为 TableModelEvent 涵盖了很多用户案例...

示例

下面是一个例子 TableRenderDemo并修改为演示前两个概念

import java.awt.BorderLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableModel;

/**
* TableRenderDemo is just like TableDemo, except that it explicitly initializes
* column sizes and it uses a combo box as an editor for the Sport column.
*/
public class TableRenderDemo extends JPanel {

public TableRenderDemo() {
super(new BorderLayout());

JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);

//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);

//Set up column sizes.
initColumnSizes(table);

//Fiddle with the Sport column's cell editors/renderers.
setUpSportColumn(table, table.getColumnModel().getColumn(2));

//Add the scroll pane to this panel.
add(scrollPane);
}

/*
* This method picks good column sizes.
* If all column heads are wider than the column's cells'
* contents, then you can just use column.sizeWidthToFit().
*/
private void initColumnSizes(JTable table) {
MyTableModel model = (MyTableModel) table.getModel();
model.addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
int type = e.getType();
switch (type) {
case TableModelEvent.UPDATE:
if (e.getFirstRow() - e.getLastRow() == 0) {
TableModel model = (TableModel) e.getSource();
int row = e.getFirstRow();
int col = e.getColumn();
System.out.println("Update " + row + "x" + col + " = " + model.getValueAt(row, col));
}
break;
}
}
});
TableColumn column = null;
Component comp = null;
int headerWidth = 0;
int cellWidth = 0;
Object[] longValues = model.longValues;
TableCellRenderer headerRenderer
= table.getTableHeader().getDefaultRenderer();

for (int i = 0; i < 5; i++) {
column = table.getColumnModel().getColumn(i);

comp = headerRenderer.getTableCellRendererComponent(
null, column.getHeaderValue(),
false, false, 0, 0);
headerWidth = comp.getPreferredSize().width;

comp = table.getDefaultRenderer(model.getColumnClass(i)).
getTableCellRendererComponent(
table, longValues[i],
false, false, 0, i);
cellWidth = comp.getPreferredSize().width;

column.setPreferredWidth(Math.max(headerWidth, cellWidth));
}
}

public void setUpSportColumn(final JTable table,
TableColumn sportColumn) {
//Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Knitting");
comboBox.addItem("Speed reading");
comboBox.addItem("Pool");
comboBox.addItem("None of the above");
TableCellEditor editor = new DefaultCellEditor(comboBox);
editor.addCellEditorListener(new CellEditorListener() {

@Override
public void editingStopped(ChangeEvent e) {
System.out.println("Editing stopped");
TableCellEditor editor = (TableCellEditor) e.getSource();
Object value = editor.getCellEditorValue();
System.out.println("Selected value = " + value);
}

@Override
public void editingCanceled(ChangeEvent e) {
}
});
sportColumn.setCellEditor(editor);

//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer
= new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
sportColumn.setCellRenderer(renderer);
}

class MyTableModel extends AbstractTableModel {

private String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
private Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};

public final Object[] longValues = {"Jane", "Kathy",
"None of the above",
new Integer(20), Boolean.TRUE};

@Override
public int getColumnCount() {
return columnNames.length;
}

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

@Override
public String getColumnName(int col) {
return columnNames[col];
}

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

/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
@Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}

/*
* Don't need to implement this method unless your table's
* editable.
*/
@Override
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}

/*
* Don't need to implement this method unless your table's
* data can change.
*/
@Override
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}

/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TableRenderDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
TableRenderDemo newContentPane = new TableRenderDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

关于java - 在 Jtable 中添加 JCombobox 并在 Swing java 中获取行和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23421952/

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