jTable中两列数据的乘法 [英] multiplication of two columns data in jTable
问题描述
我有一个小型应用程序,如下所示应用程序界面
i have a small application as follows GUI OF APP
问题是我将从表的第1,2列中提取数据并将数据相乘并将答案打印到第3列中,但一切正常,但是我没有得到最后一行的结果,并抛出异常遵循工作错误
the problem is i will be taking the data from the table columns 1,2 and multiply both the data and print the answer into the 3rd column everything works fine but i am not getting the last row result and throwing me the exception as follows working error
完整代码如下
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
public class mU extends JFrame {
private JPanel contentPane;
private JTable table;
private JTextField textField;
Double l2;
Double l4;
Double d,d2;
int hu;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mU frame = new mU();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public mU() {
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception w1){
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 486, 297);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNumberOfMachines = new JLabel("Number of Machines");
lblNumberOfMachines.setBounds(10, 11, 120, 14);
contentPane.add(lblNumberOfMachines);
JSpinner spinner = new JSpinner();
spinner.setBounds(140, 8, 48, 20);
contentPane.add(spinner);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(208, 11, 252, 239);
contentPane.add(scrollPane);
table = new JTable();
table.setRowSelectionAllowed(false);
table.setModel(new DefaultTableModel(
new Object[][] {
},
new String[] {
"Machine", "N(1)", "N(0)", "Qan"
}
));
scrollPane.setViewportView(table);
DefaultTableModel model = (DefaultTableModel) table.getModel();
table.setModel(model);
JLabel lblSum = new JLabel("Sum");
lblSum.setBounds(10, 117, 46, 14);
contentPane.add(lblSum);
textField = new JTextField();
textField.setBounds(39, 114, 105, 20);
contentPane.add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("Generate");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
table.setModel(model);
int hu=(Integer) spinner.getValue();
for(int i=0;i<hu;i++) {
model.addRow(new Object[] {"M"+(i+1),"0","0",""});
}
}
});
btnNewButton.setBounds(10, 36, 77, 23);
contentPane.add(btnNewButton);
JButton btnClear = new JButton("CLEAR");
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.setRowCount(0);
}
});
btnClear.setBounds(89, 36, 65, 23);
contentPane.add(btnClear);
JButton btnInitilize = new JButton("N");
btnInitilize.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i=0; i<=model.getColumnCount(); i++)
{
Double d= Double.parseDouble((String) model.getValueAt(i,1));
Double d2= Double.parseDouble((String) model.getValueAt(i,2));
Double d3=d*d2;
model.setValueAt(d3,i,3);
}
}
});
btnInitilize.setBounds(10, 63, 46, 23);
contentPane.add(btnInitilize);
}
}
有人可以帮我吗
推荐答案
我没有在第三列中得到最后一行的值.
I am not getting the last row value in third column.
如果您在最后一个单元格的编辑器有机会提交更改之前单击 N 按钮 ,则会发生这种情况.当失去焦点时,您可以终止编辑器,如下所示:
This can happen if you click the N button before the last cell's editor has had a chance to commit the change. You can terminate the editor when focus is lost like this:
table.putClientProperty("terminateEditOnFocusLost", true);
或者,尝试使用这些示例中使用的方法.
这篇关于jTable中两列数据的乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!