I have to make table with two buttons, one for adding a new row, and one that gets values from selected cells and then summing that values. I resolved adding a new row, but can somebody help me with this summing of selected cells values. Here is the code :
It may work, but it is not done correctly. You should never need to use the updateUI() method for something like this.
The AbstractTableModel provides the fireTableRowsInserted(...) method for this purpose.
In fact there is no need for you to create a custom TableModel because the DefaultTableModel already extend the AbstractTableModel and implements an addRow(...) method.
but can somebody help me with this summing of selected cells values
Read the JTable API for the method that returns the selected rows. You then create a loop an use the getValueAt(...) method to get the values for each selected row and add them together.
protectedvoid bt_sumCells_actionPerformed(ActionEvent e) {
double sum = 0;
//check if there is selected row
if (table.getSelectedRow() != -1) {
TableColumnModel colModel = table.getColumnModel();
//iterate throw table columns
for (int i = 0; i < colModel.getColumnCount(); i++) {
//check cell value type.
if (table.getModel().getValueAt(table.getSelectedRow(), i).getClass() == Double.class) {//if double then sum
//parse double
double temp=Double.parseDouble(table.getModel().getValueAt(table.getSelectedRow(),i).toString());
//sum
sum+=temp;
//check cell value type.
} elseif (table.getModel().getValueAt(table.getSelectedRow(),i).getClass() == Integer.class) {//if int then sum
//parse int
int temp=Integer.parseInt(table.getModel().getValueAt(table.getSelectedRow(),i).toString());
//sum
sum+=temp;
}else{//else other type (String, Date etc.) do not parse them
System.err.println("Can no include this value in sum: "+table.getModel().getValueAt(table.getSelectedRow(),
i).toString());
}
}
}else{//there is no selected row show massage
JOptionPane.showMessageDialog(this,"There is no selected cells in the table","info",JOptionPane.INFORMATION_MESSAGE);
}
//finnaly use SUM .
System.err.println("Final Sum: "+sum);
}
Ok, I added your code, but it doesn't work. When I click on button it only display in text area "Final Sum: 0.0".
And this single interval selection also doesn't work, do you know how to solve that, must it be called from MyTableModel maybe ?
publicclass Table extends JFrame {
JTextArea area;
public Table(){
this.setTitle("Excel Mocni");
this.setLayout(new GridBagLayout());
final JTable table=new JTable(new MyTableModel());
JScrollPane skrol=new JScrollPane(table);
table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
table.setFillsViewportHeight(true);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 4;
gbc.gridwidth = 1;
gbc.weightx = 0.6;
gbc.weighty = 0.6;
add(skrol, gbc);
final JButton sabiranje = new JButton("Summing");
sabiranje.addActionListener(new ActionListener() {
@Override
publicvoid actionPerformed(ActionEvent e) {
double sum = 0;
// check if there is selected row
if (table.getSelectedRow() != -1) {
TableColumnModel colModel = table.getColumnModel();
// iterate throw table columns
for (int i = 0; i < colModel.getColumnCount(); i++) {
// check cell value type.
if (table.getModel().getValueAt(table.getSelectedRow(),i).getClass() == Double.class) {// if double
// then sum
// parse double
double temp = Double.parseDouble(table.getModel().getValueAt(table.getSelectedRow(), i).toString());
// sum
sum += temp;
// check cell value type.
} elseif (table.getModel().getValueAt(table.getSelectedRow(), i).getClass() == Integer.class) {
// parse int
int temp = Integer.parseInt(table.getModel().getValueAt(table.getSelectedRow(), i).toString());
sum += temp;
} else {
area.setText("Nesto ne radi");
}
}
} else {
area.setText("Can no include some value in sum");
}
// finnaly use SUM .
area.setText("Final Sum: "+sum);
}
});
GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.fill = GridBagConstraints.HORIZONTAL;
// gbc1.anchor = GridBagConstraints.NORTH;
gbc1.ipady = 1;
gbc1.ipadx = 1;
gbc1.gridx = 1;
gbc1.gridy = 0;
gbc1.gridheight = 1;
gbc1.gridwidth = 1;
gbc1.insets = new Insets(1, 0, 1, 0);
gbc1.weightx = 0.6;
gbc1.weighty = 0.6;
add(sabiranje, gbc1);
final JButton oduzimanje = new JButton("SubTraction");
oduzimanje.addActionListener(new ActionListener() {
@Override
publicvoid actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
GridBagConstraints gbc2 = new GridBagConstraints();
gbc2.fill = GridBagConstraints.HORIZONTAL;
gbc2.anchor = GridBagConstraints.NORTH;
gbc2.gridx = 1;
gbc2.gridy = 1;
gbc2.gridheight = 1;
gbc2.gridwidth = 1;
gbc2.insets = new Insets(1, 0, 1, 0);
gbc2.weightx = 0.6;
gbc2.weighty = 0.6;
add(oduzimanje, gbc2);
final JButton mnozenje = new JButton("Mnozi");
mnozenje.addActionListener(new ActionListener() {
@Override
publicvoid actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
GridBagConstraints gbc6 = new GridBagConstraints();
gbc6.fill = GridBagConstraints.HORIZONTAL;
gbc6.gridx = 1;
gbc6.gridy = 2;
gbc6.gridheight = 1;
gbc6.gridwidth = 1;
gbc6.insets = new Insets(1, 0, 1, 0);
gbc6.weightx = 0.6;
gbc6.weighty = 0.6;
add(mnozenje, gbc6);
final JButton red = new JButton("Dodaj red");
red.addActionListener(new ActionListener() {
@Override
publicvoid actionPerformed(ActionEvent e) {
MyTableModel.dodajRed();
table.updateUI();
}
});
GridBagConstraints gbc3 = new GridBagConstraints();
gbc3.fill = GridBagConstraints.HORIZONTAL;
gbc3.anchor = GridBagConstraints.NORTH;
gbc3.gridx = 1;
gbc3.gridy = 3;
gbc3.gridheight = 1;
gbc3.gridwidth = 1;
gbc3.weightx = 0.6;
gbc3.weighty = 0.6;
gbc3.insets = new Insets(1, 0, 1, 0);
add(red, gbc3);
JLabel label = new JLabel("Rezultat: ");
GridBagConstraints gbc5 = new GridBagConstraints();
gbc5.gridx = 0;
gbc5.gridy = 4;
gbc5.gridwidth = 1;
add(label, gbc5);
area = new JTextArea();
area.setAutoscrolls(true);
area.setEditable(false);
area.setRows(5);
area.setBorder(BorderFactory.createLineBorder(Color.BLACK));
GridBagConstraints gbc4 = new GridBagConstraints();
gbc4.fill = GridBagConstraints.BOTH;
gbc4.gridx = 0;
gbc4.gridy = 5;
gbc4.gridwidth = 1;
add(area, gbc4);
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicstaticclass MyTableModel extends AbstractTableModel {
String[] columns = { "1.", "2.", "3.", "4.", "5.", "6.", "7." };
static ArrayList<ArrayList<String>> rows;
public MyTableModel(){
rows = new ArrayList<ArrayList<String>>();
for (int i=0; i<10; i++){
ArrayList<String> red = new ArrayList<String>();
red.add("0");
red.add("0");
red.add("0");
red.add("0");
red.add("0");
red.add("0");
red.add("0");
rows.add(red);
}
}
publicint getColumnCount(){
return columns.length;
}
publicint getRowCount(){
return rows.size();
}
public Object getValueAt(int rowIndex, int columnIndex){
return rows.get(rowIndex).get(columnIndex);
}
publicboolean isCellEditable(int rowIndex, int columnIndex){
returntrue;
}
publicvoid setValueAt(Object value, int rowIndex, int columnIndex){
rows.get(rowIndex).set(columnIndex, (String) value);
}
publicstaticvoid dodajRed(){
ArrayList<String> red=new ArrayList<String>();
red.add("0");
red.add("0");
red.add("0");
red.add("0");
red.add("0");
red.add("0");
red.add("0");
MyTableModel.rows.add(red);
}
}
publicstaticvoid main(String[] args) {
new Table();
}
}
Ok, that's for multi row selected, but there is "table.setCellSelectionEnabled(true);", so I need to sum only sellected cell's values, like here: http://img265.imageshack.us/img265/4729/37979180.png