帮助我创建行来计算第 1 列中的一些值。 5
我使用下面的模型,但我未能创建一行来总结第五列中的数据,如果有人知道如何,请帮助我。
当数据从按钮触发时,我也需要增加行数以及最后一行的总数
提前谢谢您。
我
setModel ( new DefaultTableModel () {
private static final long serialVersionUID = 1L;
private final String[] columnNames = { "" , "" , "" , "" , "" , "" };
@Override
public Class<?> getColumnClass ( int columnIndex ) {
switch ( columnIndex ) {
case 3:
case 4:
case 5:
return Double.class;
}
return super.getColumnClass ( columnIndex );
}
@Override
public Object getValueAt ( int row ,
int column ) {
if ( column == 5 ) {
Double i = ( Double ) getValueAt ( row ,
3 );
Double d = ( Double ) getValueAt ( row ,
4 );
if ( i != null && d != null ) {
return i * d;
} else {
}
}
return super.getValueAt ( row ,
column );
}
@Override
public boolean isCellEditable ( int row ,
int column ) {
return column == 1 || column == 2 || column == 3 || column == 4;
}
@Override
public void setValueAt ( Object aValue ,
int row ,
int column ) {
super.setValueAt ( aValue ,
row ,
column );
fireTableCellUpdated ( row ,
5 );
}
@Override
public String getColumnName ( int column ) {
switch ( column ) {
case 0:
return "S/N";
case 1:
return "Product";
case 2:
return "UoM";
case 3:
return "Quantity";
case 4:
return "Price";
case 5:
return "Total";
}
return super.getColumnName ( column );
}
@Override
public int getColumnCount () {
return columnNames.length;
}
} );
setTable ( new JTable ( getModel () ) );
getTable ().
setFillsViewportHeight ( true );
getTable ().
addMouseListener ( new MouseAdapter () {
@Override
public void mouseClicked ( MouseEvent e ) {
if ( e.getClickCount () == 1 ) {
if ( getTable ().
rowAtPoint ( e.getPoint () ) < 0 ) {
getModel ().
addRow ( new Vector () );
model.setValueAt ( getLine () + 1 ,
getLine () ,
0 );
setLine ( getLine () + 1 );
}
}
}
} );
tcm = table.getColumnModel ();
tc1 = tcm.getColumn ( 0 );
tc2 = tcm.getColumn ( 2 );
tc1.setMaxWidth ( 100 );
tc2.setMaxWidth ( 100 );
getTable ().
setRowHeight ( getTable ().
getRowHeight () + 15 );
getTable ().
getTableHeader ().
setFont ( new Font ( "Arial" ,
Font.BOLD ,
18 ) );
table.setFont ( new Font ( "Arial" ,
Font.PLAIN ,
18 ) );
getTable ().
setGridColor ( new java.awt.Color ( 150 ,
150 ,
150 ) );
table.setForeground ( new java.awt.Color ( 0 ,
0 ,
0 ) );
table.setBackground ( Color.WHITE );
table.getTableHeader ().
setBackground ( new java.awt.Color ( 60 ,
130 ,
160 ) );
table.getTableHeader ().
setForeground ( new java.awt.Color ( 255 ,
255 ,
255 ) );
getTd ().
setBackground ( new java.awt.Color ( 60 ,
130 ,
160 ) );
getTd ().
setForeground ( Color.WHITE );
getTd ().
setFont ( new Font ( "Arial" ,
Font.PLAIN ,
20 ) );
getBtn1 ().
setBackground ( new java.awt.Color ( 60 ,
130 ,
160 ) );
getBtn1 ().
setFont ( new Font ( "Arial" ,
Font.BOLD ,
20 ) );
getBtn1 ().
setForeground ( new java.awt.Color ( 250 ,
250 ,
250 ) );
getBtn2 ().
setBackground ( new java.awt.Color ( 60 ,
130 ,
160 ) );
getBtn2 ().
setFont ( new Font ( "Arial" ,
Font.BOLD ,
20 ) );
getBtn2 ().
setForeground ( new java.awt.Color ( 250 ,
250 ,
250 ) );
getBtn4 ().
setBackground ( new java.awt.Color ( 60 ,
130 ,
160 ) );
getBtn4 ().
setFont ( new Font ( "Arial" ,
Font.BOLD ,
20 ) );
getBtn4 ().
setForeground ( new java.awt.Color ( 250 ,
250 ,
250 ) );
getBtn5 ().
setBackground ( new java.awt.Color ( 60 ,
130 ,
160 ) );
getBtn5 ().
setFont ( new Font ( "Arial" ,
Font.BOLD ,
20 ) );
getBtn5 ().
setForeground ( new java.awt.Color ( 250 ,
250 ,
250 ) );
getBtn4 ().
addActionListener ( ( ActionEvent e ) -> {
saveTable ();
} );
getBtn1 ().
addActionListener ( ( ActionEvent e ) -> {
loadTable ();
} );
getBtn2 ().
addActionListener ( ( ActionEvent e ) -> {
getModel ().
addRow ( new Vector () );
double total = 0;
model.setValueAt ( total ,
model.getRowCount () - 1 ,
5 );
model.setValueAt ( getLine () + 1 ,
getLine () ,
0 );
setLine ( getLine () + 1 );
} );
getBtn5 ().
addActionListener ( new ActionListener () {
@Override
public void actionPerformed ( ActionEvent e ) {
MessageFormat header = new MessageFormat ( "INVOICE" );
MessageFormat footer = new MessageFormat ( "-{0}-\n" + new Date () );
try {
table.print ( JTable.PrintMode.FIT_WIDTH ,
header ,
footer );
} catch ( Exception ae ) {
System.err.println ( "Error printing: " + ae.getMessage () );
}
}
} );
getJb ().
add ( getBtn5 () );
getJb ().
add ( getBtn1 () );
getJb ().
add ( getBtn4 () );
getJb ().
add ( getBtn2 () );
setScrollPane2 ( new JScrollPane ( getTable () ) );
frame.add ( getScrollPane2 () ,
BorderLayout.CENTER );
frame.add ( getTd () ,
BorderLayout.NORTH );
frame.add ( getJb () ,
BorderLayout.SOUTH );
frame.setDefaultCloseOperation ( EXIT_ON_CLOSE );
frame.pack ();
frame.setSize ( 1300 ,
900 );
frame.setLocationRelativeTo ( null );
frame.setVisible ( true );
The image can be viewed here
首先,您应该避免将汇总行作为表的最后一行,因为您将更改数据模型。如果您从模型中添加/删除行,这将避免移动行的需要。 话虽这么说,您可以轻松地将总计放入 JLabel 或其他组件中。 只需添加一个 TableModelListener 即可捕获模型中的更改并重新计算总数:
dataTable.getModel().addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
updateSummary();
}
});
private void updateSummary(){
Double sum = (double) 0;
for (int row = 0; row < dataTable.getRowCount(); row++){
int rowModel = dataTable.convertRowIndexToModel(row);
// Make sure the cast is possible here, or convert the Object appropiately to Double
Double value = (Double) tblResults.getModel().getValueAt(rowModel, 5);
sum += value;
}
// Format the number appropiately here if you need
lblSummary.setText(sum.toString());
}
即使您有 RowFilter,此方法也将起作用,因为它适用于所有可见行。