13 Replies - 715 Views - Last Post: 15 August 2017 - 09:19 PM Rate Topic: -----

#1 wseng92  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 268
  • Joined: 23-September 15

Tabulate data to JTable

Posted 13 August 2017 - 09:17 AM

I have posted a question here an hour ago but haven't get any replies. So I decided to post here one more time.

I using JTable to tabular data. When JSpinner is clicked, it will get the item, quantity and the price and displayed to JTable.

 JSpinner[] numspinner;
       private JTable table;
       DefaultTableModel dtm;
       Double[] price;
 
       public void stateChanged(ChangeEvent e) { // JSpinner clicked  
       for (int i = 0; i < ELEMENTS; i++) {
            int quantity = (int) ((JSpinner) e.getSource()).getValue();
            if (numspinner[i] == e.getSource()) {
                dtm.addRow(new Object[] { foodLabel[i].getText(),quantity, price[i]*quantity});
            }
       }
    

The (JSpinner) e.getSource()).getValue() is used to get the value from the JSpinner. What I want to achieve is I want the value change instead of adding more rows. How to achieve this ?

Example

Salad 3 15

Instead of
Salad 1 5
Salad 2 10
Salad 3 15

Here the image http://imgur.com/a/cwFvF

This post has been edited by wseng92: 14 August 2017 - 08:52 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Tabulate data to JTable

#2 IOExceptional  Icon User is offline

  • D.I.C Head

Reputation: 25
  • View blog
  • Posts: 122
  • Joined: 15-September 16

Re: Tabulate data to JTable

Posted 13 August 2017 - 09:46 AM

Have you taken a look at DefaultTableModel#setValueAt? It could be what you need.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3417
  • View blog
  • Posts: 15,412
  • Joined: 20-September 08

Re: Tabulate data to JTable

Posted 14 August 2017 - 08:38 AM

I think you ought to display the price too. I'd probably make a Map<JSpinner, SomeTableRowObject> (where the value depends on the way you define your table rows) so you don't have to iterate everything. Let's assume the row type is Object[]. You could then do something like

    public void stateChanged(ChangeEvent e) { // JSpinner clicked  

        JSpinner s = (JSpinner) e.getSource();
        int quantity = (int) s.getValue();
        Object[] rowChanged = rowMap.get(s);
        // (Assuming column 1 shows price and column 4 shows cost)
        rowChanged[3] = rowChanged[1] * quantity;
        dtm.fireTableDataChanged();
    }


Was This Post Helpful? 0
  • +
  • -

#4 wseng92  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 268
  • Joined: 23-September 15

Re: Tabulate data to JTable

Posted 14 August 2017 - 08:46 AM

View PostIOExceptional, on 13 August 2017 - 04:46 PM, said:

Have you taken a look at DefaultTableModel#setValueAt? It could be what you need.

I tried using setValueAt, but it always change the above row.

Example I want to change the second row column, but it change the first row column instead.

public void stateChanged(ChangeEvent e) {
		for (int i = 0; i < ELEMENTS; i++) {
			int quantity = (int) ((JSpinner) e.getSource()).getValue();
			if (numspinner[i] == e.getSource()) {
				if (quantity == 1) {
					dtm.addRow(new Object[] { foodLabel[i].getText(), quantity, price[i] * quantity });
				} else {
					dtm.setValueAt(quantity, i, 1);
				}
			}
		}
	}

This post has been edited by wseng92: 14 August 2017 - 08:59 AM

Was This Post Helpful? 0
  • +
  • -

#5 wseng92  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 268
  • Joined: 23-September 15

Re: Tabulate data to JTable

Posted 14 August 2017 - 08:56 AM

View Postg00se, on 14 August 2017 - 03:38 PM, said:

I think you ought to display the price too.

The price will only displayed to JTable once the JSpinner clicked. I replace
dtm.setValueAt(quantity, i, 1);
with
 dtm.fireTableDataChanged();
, when the JSpinner changed, the quantity column remains unchanged.
Was This Post Helpful? 0
  • +
  • -

#6 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 643
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Tabulate data to JTable

Posted 14 August 2017 - 09:53 AM

Quote

want to change the second row column, but it change the first row column instead.

Do the two indexes have different bases? 0 vs 1?
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3417
  • View blog
  • Posts: 15,412
  • Joined: 20-September 08

Re: Tabulate data to JTable

Posted 14 August 2017 - 11:11 AM

Quote

The price will only displayed to JTable once the JSpinner clicked.

You misunderstand my meaning. The price is constant and is the same if zero items are required. What you are referring to is the amount payable
Was This Post Helpful? 0
  • +
  • -

#8 wseng92  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 268
  • Joined: 23-September 15

Re: Tabulate data to JTable

Posted 15 August 2017 - 01:40 AM

View PostNormR, on 14 August 2017 - 04:53 PM, said:

Do the two indexes have different bases? 0 vs 1?
Which two are you referring to ?

View Postg00se, on 14 August 2017 - 06:11 PM, said:

You misunderstand my meaning. The price is constant and is the same if zero items are required. What you are referring to is the amount payable
What is rowMap?

This post has been edited by wseng92: 15 August 2017 - 01:40 AM

Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3417
  • View blog
  • Posts: 15,412
  • Joined: 20-September 08

Re: Tabulate data to JTable

Posted 15 August 2017 - 03:04 AM

Quote

What is rowMap?

Quote

I'd probably make a Map<JSpinner, SomeTableRowObject>
(See THIS )
Was This Post Helpful? 0
  • +
  • -

#10 wseng92  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 268
  • Joined: 23-September 15

Re: Tabulate data to JTable

Posted 15 August 2017 - 08:41 AM

View Postg00se, on 14 August 2017 - 03:38 PM, said:

I'd probably make a Map<JSpinner, SomeTableRowObject> (where the value depends on the way you define your table rows) so you don't have to iterate everything. Let's assume the row type is Object[].

How/Where should I apply that ? Thanks

This post has been edited by wseng92: 15 August 2017 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3417
  • View blog
  • Posts: 15,412
  • Joined: 20-September 08

Re: Tabulate data to JTable

Posted 15 August 2017 - 09:16 AM

Well i don't know - somewhere else applicable in your code ;)
Was This Post Helpful? 0
  • +
  • -

#12 wseng92  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 268
  • Joined: 23-September 15

Re: Tabulate data to JTable

Posted 15 August 2017 - 09:44 AM

Can you post your example here ?
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3417
  • View blog
  • Posts: 15,412
  • Joined: 20-September 08

Re: Tabulate data to JTable

Posted 15 August 2017 - 10:37 AM

Sorry - i don't have the time unless it's a micro-paid assignment
Was This Post Helpful? 0
  • +
  • -

#14 wseng92  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 268
  • Joined: 23-September 15

Re: Tabulate data to JTable

Posted 15 August 2017 - 09:19 PM

View Postg00se, on 15 August 2017 - 05:37 PM, said:

Sorry - i don't have the time unless it's a micro-paid assignment
OK...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1