import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import net.java.dev.designgridlayout.DesignGridLayout;
import java.io.*;
import net.java.dev.designgridlayout.Tag;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.*;
import java.sql.*;
import javax.swing.table.TableColumn;
class table1 {
JFrame JF;
Container C;
JPanel JP;
JLabel creditLabel;
JComboBox credit;
String[] Credit = { "Vasan Phalke", "Pansare", "Anil Kg", "Suresh",
"Total Credit", "" };
String[] Names = { "Name", "Qty", "Rate/ Kg", "Total Amt." };
JTable table;
DefaultTableModel model;
JScrollPane scrollPane;
public table1() {
JF = new JFrame();
JP = new JPanel();
C = JF.getContentPane();
C.setLayout(new BorderLayout());
JF.pack();
JF.setLocationRelativeTo(null);
JF.setVisible(true);
DesignGridLayout layout = new DesignGridLayout(JP);
creditLabel = new JLabel("Credit");
credit = new JComboBox<String>(Credit);
model = new DefaultTableModel(Names, 6) {
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
return String.class;
case 1:
return Integer.class;
case 2:
case 3:
return Double.class;
}
return super.getColumnClass(columnIndex);
}
public Object getValueAt(int row, int column) {
if (column == 3) {
Integer i = (Integer) getValueAt(row, 1);
Double d = (Double) getValueAt(row, 2);
if (i != null && d != null) {
return i * d;
} else {
return 0 * 0;
}
}
return super.getValueAt(row, column);
}
public boolean isCellEditable(int arg0, int arg1) {
return true;
}
public void setValueAt(Object aValue, int row, int column) {
super.setValueAt(aValue, row, column);
fireTableCellUpdated(row, 3);
}
};
table = new JTable(model);
{
TableColumn nameColumn = table.getColumnModel().getColumn(0);
nameColumn.setCellEditor(new DefaultCellEditor(credit));
}
scrollPane = new JScrollPane(table);
layout.row().grid(creditLabel);
layout.emptyRow();
layout.row().grid().add(scrollPane);
C.add(JP);
}
public static void main(String args[]) {
new table1();
}
}
我想要Total Amt的总和。列出现在单元格上(5,4)。我尝试了一些代码,但是失败了,请在下面添加显示附加图像的代码。从过去的5个小时开始,我就被困在这个桌子上。提前谢谢。
答案 0 :(得分:4)
包含此approach的示例的简化变体如下所示。请注意以下几点:
虽然JTable
不是电子表格,但 可以动态更新单元格,以帮助理解。使用TableModelListener
更新相关的视图组件。
TableModel
应该为至少个可能的单元格触发事件; fireTableDataChanged()
用于在这个小型固定尺寸模型上获得权宜之计。
始终pack()
封闭的Window
并将setVisible()
设为最后。
应在event dispatch thread上仅构建和操作Swing GUI对象。
使用通用编码约定。
代码,经过测试:
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
public class Table1 {
JFrame frame;
JComboBox credit;
String[] rowNames = {
"Vasan Phalke", "Pansare", "Anil Kg", "Suresh", "Total Credit", ""};
String[] colNames = {"Name", "Qty", "Rate/ Kg", "Total Amt."};
JTable table;
DefaultTableModel model;
JScrollPane scrollPane;
public Table1() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
credit = new JComboBox<>(rowNames);
model = new DefaultTableModel(colNames, 6) {
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
return String.class;
case 1:
return Integer.class;
case 2:
return Double.class;
case 3:
return Double.class;
}
return super.getColumnClass(columnIndex);
}
@Override
public Object getValueAt(int row, int col) {
if (col == 3 & row == 5) {
double sum = 0;
for (int i = 0; i < 5; i++) {
sum += ((Double) getValueAt(i, 3)).doubleValue();
}
return sum;
}
if (col == 3 & row != 5) {
Integer i = (Integer) getValueAt(row, 1);
Double d = (Double) getValueAt(row, 2);
if (i != null && d != null) {
return i * d;
} else {
return 0d;
}
}
return super.getValueAt(row, col);
}
@Override
public void setValueAt(Object aValue, int row, int col) {
super.setValueAt(aValue, row, col);
fireTableDataChanged();
}
@Override
public boolean isCellEditable(int row, int col) {
return col != 3;
}
};
table = new JTable(model);
TableColumn nameColumn = table.getColumnModel().getColumn(0);
nameColumn.setCellEditor(new DefaultCellEditor(credit));
scrollPane = new JScrollPane(table);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Table1();
}
});
}
}