問題描述
我有這個 JTable
有一個 DefaultTableModel
作為它的模型.在桌子上,我有幾個擺動組件,JComboBox
和 JCheckBox
,通過 DefaultCellEditor
和 DefaultCellRenderer
在特定列上設置.TableModelListener
已添加到表中以捕獲可編輯列的更改.其余列將顯示所選組件的詳細信息,即商品代碼 -> 商品價格、商品數量、商品分類等.
I have this JTable
having a DefaultTableModel
as its model.
On the table I have several swing component, JComboBox
and JCheckBox
, set on a particular column via DefaultCellEditor
and DefaultCellRenderer
.
The TableModelListener
was added to the table to capture changes on editable columns.
The rest of the columns will display details of the selected component, i.e. item code -> item price, item count, item classification,etc.
我有這個問題,如果 JComboBox(itemCode)
的 selectedItem 發生變化,其他 JComboBox(itemClassification)
的項目也會發生變化.但隨著其他 JComboBox 的變化,我需要在同一張桌子上顯示商品價格.此更改重新觸發 valueChanged
方法,該方法使 valueChanged
無限循環.
I have this problem wherein if the selectedItem of the JComboBox(itemCode)
changes, the items of the other JComboBox(itemClassification)
changes. But together with the change of the other JComboBox I need to display the item price on the same table. This change refires the valueChanged
method which makes an infinite loop of valueChanged
.
我怎樣才能擺脫無限循環的東西?
How can I get rid of the infinite loop thing?
推薦答案
一種方法是檢查更新事件以查看該事件是針對哪一列并忽略自動更新的列:
One way is to check the update event to see what column the event is for and ignore columns that are automatically updated:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class TableProcessing extends JPanel implements TableModelListener
{
public TableProcessing()
{
String[] columnNames = {"Item", "Quantity", "Price", "Cost"};
Object[][] data =
{
{"Bread", new Integer(1), new Double(1.11), new Double(1.11)},
{"Milk", new Integer(1), new Double(2.22), new Double(2.22)},
{"Tea", new Integer(1), new Double(3.33), new Double(3.33)},
{"Cofee", new Integer(1), new Double(4.44), new Double(4.44)}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames)
{
// Returning the Class of each column will allow different
// renderers to be used based on Class
@Override
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}
// The Cost is not editable
@Override
public boolean isCellEditable(int row, int column)
{
return (column == 3) ? false : true;
}
};
model.addTableModelListener( this );
JTable table = new JTable( model );
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );
add( scrollPane );
String[] items = { "Bread", "Milk", "Tea", "Coffee" };
JComboBox<String> editor = new JComboBox<String>( items );
DefaultCellEditor dce = new DefaultCellEditor( editor );
table.getColumnModel().getColumn(0).setCellEditor(dce);
}
/*
* The cost is recalculated whenever the quantity or price is changed
*/
public void tableChanged(TableModelEvent e)
{
if (e.getType() == TableModelEvent.UPDATE)
{
int row = e.getFirstRow();
int column = e.getColumn();
if (column == 1 || column == 2)
{
TableModel model = (TableModel)e.getSource();
int quantity = ((Integer)model.getValueAt(row, 1)).intValue();
double price = ((Double)model.getValueAt(row, 2)).doubleValue();
Double value = new Double(quantity * price);
model.setValueAt(value, row, 3);
}
}
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Table Model Listener");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TableProcessing());
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args) throws Exception
{
EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
這篇關于JTable ->表模式監聽器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!