4 Replies - 207 Views - Last Post: 12 June 2014 - 12:51 PM Rate Topic: **--- 3 Votes

#1 incredibleX  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 201
  • Joined: 01-November 13

Problem with ListSelectionListener

Posted 12 June 2014 - 03:07 AM

    table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
		public void valueChanged(ListSelectionEvent e) {
			System.out.println("Value changed");
			if(Float.parseFloat(table.getModel().getValueAt(table.getSelectedRow(), 4).toString()) != 0.0){
					System.out.println(getTotal());
				totalAmount.setText(""+getTotal());
			}
		}
	});	


This is what I've tried so far.
What i want to do is to calculate all Total Amounts by adding all the rows (5th column only),as 5th column has total amount for 1 product, and set the text for a JTextField whenever the 5th column of any row changes its value.

But this isn't working my way. I have also tried keyListener to trigger that change at Enter key typed but that also don't work for me. I am stuck and I need a way out. Any help or suggestion will be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with ListSelectionListener

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,083
  • Joined: 05-April 11

Re: Problem with ListSelectionListener

Posted 12 June 2014 - 03:14 AM

How is it not working? What is it currently doing?
Was This Post Helpful? 0
  • +
  • -

#3 incredibleX  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 201
  • Joined: 01-November 13

Re: Problem with ListSelectionListener

Posted 12 June 2014 - 09:16 AM

value changed is printed it means event is triggering but the total is printed only when i first time click the row.
Which is not what i want.
My if condition in't right but what it should be then?
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,083
  • Joined: 05-April 11

Re: Problem with ListSelectionListener

Posted 12 June 2014 - 10:57 AM

The selection won't change if you click the same cell twice
Have you tried selecting another row and cell?
Was This Post Helpful? 0
  • +
  • -

#5 incredibleX  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 201
  • Joined: 01-November 13

Re: Problem with ListSelectionListener

Posted 12 June 2014 - 12:51 PM

I've attached my Table please have a look.
What i'm doing is that when i put some value in quantity and rate and get focus on the amount column quantity and rate is multiplied and amount is automatically filled up.
I've used custom model for this:
Here is my model:

model = new DefaultTableModel() {
			private static final long serialVersionUID = 1L;

			@Override
            public Class<?> getColumnClass(int columnIndex) {
                switch (columnIndex) {
                case 0:
                	return JComboBox.class;
                case 1:
                	return JComboBox.class;
                case 2:
                    return Integer.class;
                case 3:
                    return Float.class;
                case 4:
                    return Float.class;
                }
                return super.getColumnClass(columnIndex);
            }

            @Override
            public Object getValueAt(int row, int column) {
            	if(column == 0){}
                if (column == 4) {
                	Integer d = (Integer) getValueAt(row, 2);
                    Float i = (Float) getValueAt(row, 3);
                    if (i != null && d != null) {
                    	//totalAmount.setText(""+getTotal());
                        return i * d;
                    } else {
                        return 0;
                    }
                }
                return super.getValueAt(row, column);
            }

            @Override
            public boolean isCellEditable(int row, int column) {
                return column == 0 || column == 1 || column == 2 || column == 3;
            }

            @Override
            public void setValueAt(Object aValue, int row, int column) {
                super.setValueAt(aValue, row, column);
                fireTableCellUpdated(row, 4);
            }

            @Override
            public String getColumnName(int column) {
                switch (column) {
                case 0:
                    return "Product Code";
                case 1:
                    return "Product";
                case 2:
                    return "Quantity";
                case 3:
                    return "Rate";
                case 4:
                    return "Total Amount";
                }
                return super.getColumnName(column);
            }

            @Override
            public int getColumnCount() {
                return 5;
            }
            
           
        };
        model.addRow(new Vector<String>());
    }



Now as you see i have a text field down there which shows the total amount.
What i want is that whenever the amount is filled up it should also calculate the total from all the column and show it in the field automatically.

I've tried doing something (first code above) but i can't figure out how to do it?

Remember I've added functionality to it that whenever the row is double clicked a new empty row appears.

Please suggest me something with it.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1