[英]Get selected row of JTable when itemStateChanged in JComboBox
我試圖在單擊combox時獲取當前行數據。 我的問題是,如果我嘗試獲取有關單擊組合框的詳細信息,則檢索到的數據是錯誤的。
這是在集合中填充無效數據。 請按照下面提到的確切步驟進行復制。
請運行代碼以復制問題,因為該問題僅在初始選擇期間有效,而在之后沒有。
注意:請僅在第二列上單擊臟污
Step 1: Click on Second Column of Row 1
Step 2: Select- Item 1
Step 3: Click on Second Column of Row 2
Step 4: Select- Item 2
Step 5: Click on Second Column of Row 3
Step 6: Select- Item 3
WORKS Fine till here :)
Step 7 : Click on Second column of Row 1 and do not change an selection leave it as it is (Just click on the combobox twice)
Step 8 : Click on Second column of Row 2, DO NO CHANGES
Step 9 : Click on Second column of Row 3, DO NO CHANGES
Step 10: NOW randomly click on any of the second columns of rows(1,2,3) and see the output datamap. It really wierd why the event is
下面是示例代碼:
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Vector;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class TestJCombo extends JFrame {
public TestJCombo() {
initialize();
}
JTable jTable;
JComboBox comboBox;
Map<Integer, String> dataMap;
private void initialize() {
setSize(300, 300);
setLayout(new FlowLayout(FlowLayout.LEFT));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTextField field = new JTextField();
field.setSize(50000, 25);
field.setText(" ");
jTable = new JTable();
comboBox = new JComboBox();
comboBox.setEditable(true);
comboBox.addItem("item 1");
comboBox.addItem("item 2");
comboBox.addItem("item 3");
comboBox.setEditable(false);
dataMap = new LinkedHashMap();
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
Object selected = comboBox.getSelectedItem();
int selectedRow = jTable.getSelectedRow();
selectedRow = jTable.convertRowIndexToModel(selectedRow);
if (selectedRow != -1) {
String user = (String) jTable.getValueAt(selectedRow, 0);
String data = "Row: " + (selectedRow + 1) + " :::: " + user + " , "
+ comboBox.getSelectedItem();
dataMap.put(selectedRow + 1, "[" + user + " - " + comboBox.getSelectedItem() + "]");
if (selected != null) {
field.setText(data);
}
System.out.println("Current data map:: " + dataMap);
}
}
}
});
jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTable.setRowHeight(30);
jTable.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
DefaultTableModel myTableMdl = new DefaultTableModel();
myTableMdl.addColumn("User");
myTableMdl.addColumn("Role");
jTable.setModel(myTableMdl);
jTable.getColumn("Role").setCellEditor(new DefaultCellEditor(comboBox));
Vector tableData;
for (int i = 1; i <= 7; i++) {
tableData = new Vector();
tableData.add("User " + i);
myTableMdl.addRow(tableData);
}
getContentPane().add(jTable);
getContentPane().add(field);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestJCombo().setVisible(true);
}
});
}
}
問題是您在錯誤的組件上使用了錯誤的偵聽器。 您不應將偵聽器添加到組合框。 使用編輯器的目的是編輯數據並更新TableModel
。 您不應引用組合框進行進一步處理。
因此,您應該向TableModelListener
添加一個TableModel
。 然后,只要更改第2列中的值,就會生成TableModelEvent
。 TableModelEvent
將包含已更改的單元格的行/列信息。
您可以簽出: JTable-> TableModeListener以獲取使用TableModelListener的基本示例。 在您的情況下,請檢查第二列是否已更改,然后更新地圖。
還要注意,您錯誤地使用了convertRowIndexToModel()方法:
selectedRow = jTable.convertRowIndexToModel(selectedRow);
...
String user = (String) jTable.getValueAt(selectedRow, 0);
首先,如果對表視圖進行排序或過濾,則只需要擔心覆蓋行索引。 在提供的代碼中,您也不執行任何操作,因此無需轉換索引。
但是,如果您曾經對視圖進行過某種篩選,則需要將視圖行轉換為模型行,然后必須從模型而不是視圖訪問數據。 因此,代碼將是:
String user = (String) jTable.getModel().getValueAt(selectedRow, 0);
問題是您要創建一個JComboBox
,然后將其放在列中的所有單元格上。 您正在引用JComboBox
,而不是為每個單元格創建一個新的。 這就是為什么當您按照提到的10個步驟進行操作時,獲得的結果非常愚蠢和毫無意義。
這是解決方法,為列1(“角色”列)中的所有單元格創建一個自定義TableCellEditor
。 目標是為每個單元創建一個全新的JComboBox
。 這可以通過將JTable
的聲明更改為jtable = new JTable(){...};
重寫getCellEditor(...){...}
方法。 另外, private JComboBox createComboBox() {...}
是全新JComboBox
創建所需要的。
現在,我們將取出addItemListener
並將其替換為addActionListener
。 這是必需的,因為如果用戶選擇已選擇的項目,則不會調用ItemListener
的itemStateChanged
。 我們需要略微復制兩次鼠標單擊,一次單擊顯示下拉列表,第二次單擊選擇項目(如果不需要,可以跳過此步驟)。
我還編輯了您的代碼,以使其更具可讀性和效率。
這是MVCE:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.table.TableCellEditor;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Vector;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class TestJCombo extends JFrame {
JTable jTable;
//JComboBox comboBox;//not needed, replaced by the createComboBox() method.
Map<Integer, String> dataMap;
final JTextField FIELD = new JTextField(25);//must put this globally.
//Since it is final, it should be all in upper case
//It is a good practice to put the global variables on top and constructor(s) below.
public TestJCombo() {
initialize();
}
private void initialize() {
//use pack(); instead setSize(...); I used it at the end of this method.
//setSize(300, 300);
setLayout(new FlowLayout(FlowLayout.LEFT));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
//both lines not needed, it has been taken care of when FIELD was declared.
//FIELD.setSize(50000, 25);
//FIELD.setText(" "
// + " ");
//Must create a seperate TableCellEditor for each cell in the table.
jTable = new JTable() {
public TableCellEditor getCellEditor(int row, int column) {
int modelColumn = convertColumnIndexToModel(column);
//if the cell lies in the second column, create a custom cell editor.
if (modelColumn == 1) {
return (TableCellEditor) new DefaultCellEditor(createComboBox());
} else {
return super.getCellEditor(row, column);
}
}
};
//comboBox = new JComboBox();
//comboBox.setEditable(true);
//comboBox.addItem("item 1");
//comboBox.addItem("item 2");
//comboBox.addItem("item 3");
//comboBox.setEditable(false);
dataMap = new LinkedHashMap();
jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTable.setRowHeight(30);
jTable.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
DefaultTableModel myTableMdl = new DefaultTableModel();
myTableMdl.addColumn("User");
myTableMdl.addColumn("Role");
jTable.setModel(myTableMdl);
//we have our own custom CellEditor, this is not needed
//jTable.getColumn("Role").setCellEditor(new DefaultCellEditor(comboBox));
Vector tableData;
for (int i = 1; i <= 7; i++) {
tableData = new Vector();
tableData.add("User " + i);
myTableMdl.addRow(tableData);
}
//It is better practice to add everything to a
//panel and then add that panel to the frame.
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(jTable);
panel.add(Box.createRigidArea(new Dimension(0, 10)));//add some space
panel.add(FIELD);
getContentPane().add(panel, BorderLayout.CENTER);
pack();//should use this.
}
private JComboBox createComboBox() {
JComboBox comboBox = new JComboBox();
comboBox.setEditable(true);
comboBox.addItem("item 1");
comboBox.addItem("item 2");
comboBox.addItem("item 3");
comboBox.setEditable(false);
//Add an ActionListener so that it would also detect if the user selects
//the same item. The itemStateChanged in the ItemListener will not be
//invoked if the user selects the item that is already selected.
comboBox.addActionListener(new ActionListener() {
//To update the result everytime the user selects an item
//(regardless if it was selected or not), we need to "mock"
//a two-click operation. The first click will show the
//drop-down list to select from and the second will
//allow the user to select the desired choice to be selected.
boolean doubleClick = false;
@Override
public void actionPerformed(ActionEvent ae) {
if (doubleClick) {
int selectedRow = jTable.getSelectedRow();
selectedRow = jTable.convertRowIndexToModel(selectedRow);
Object selected = comboBox.getSelectedItem();
if (selectedRow != -1 && selected != null) {
String user = (String) jTable.getValueAt(selectedRow, 0);
String data = "Row: " + (selectedRow + 1) + " :::: " + user + " , "
+ comboBox.getSelectedItem();
dataMap.put(selectedRow + 1, "[" + user + " - "
+ comboBox.getSelectedItem() + "]");
FIELD.setText(data);
System.out.println("Current data map:: " + dataMap);
}
doubleClick = false;
}
doubleClick = true;
}
});
// comboBox.addItemListener(new ItemListener() {
//
// public void itemStateChanged(ItemEvent e) {
// if (e.getStateChange() == ItemEvent.SELECTED) {
//
// Object selected = comboBox.getSelectedItem();
//
// int selectedRow = jTable.getSelectedRow();
// selectedRow = jTable.convertRowIndexToModel(selectedRow);
// if (selectedRow != -1) {
// String user = (String) jTable.getValueAt(selectedRow, 0);
// String data = "Row: " + (selectedRow + 1) + " :::: " + user + " , "
// + comboBox.getSelectedItem();
// dataMap.put(selectedRow + 1, "[" + user + " - "
// + comboBox.getSelectedItem() + "]");
// if (selected != null) {
// FIELD.setText(data);
// }
// System.out.println("Current data map:: " + dataMap);
// }
// }
//
// }
// });
return comboBox;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestJCombo().setVisible(true);
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.