469,631 Members | 1,203 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

changing values in two columns in a table

oll3i
679 512MB
i want to change the values in two columns
one colum is a combobox and the secons column is editable too
i want to get the value of that second column and the value of combobox
and sent that to another application
i have setValueAt(Object value,int row, int col) but it works only for combobox
when i enter some data in the second editable column the value i entered disappears

rows are a list data = new ArrayList<OrderClientProducent>();

thank you
Expand|Select|Wrap|Line Numbers
  1.  
  2. import javax.swing.DefaultCellEditor;
  3. import javax.swing.JComboBox;
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.JScrollPane;
  7. import javax.swing.JTable;
  8. import javax.swing.event.TableModelEvent;
  9. import javax.swing.event.TableModelListener;
  10. import javax.swing.table.AbstractTableModel;
  11. import javax.swing.table.DefaultTableCellRenderer;
  12. import javax.swing.table.TableCellRenderer;
  13. import javax.swing.table.TableColumn;
  14.  
  15. import com.db4o.Db4o;
  16. import com.db4o.ObjectContainer;
  17. import com.db4o.ObjectSet;
  18.  
  19. import javax.naming.*;
  20. import javax.jms.*;
  21.  
  22. import java.awt.BorderLayout;
  23. import java.awt.Component;
  24. import java.awt.Dimension;
  25. import java.awt.GridLayout;
  26.  
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. /** 
  30.  * TableRenderDemo is just like TableDemo, except that it
  31.  * explicitly initializes column sizes and it uses a combo box
  32.  * as an editor for the Sport column.
  33.  */
  34. public class TableClientProducer extends JPanel implements TableModelListener{
  35.     private boolean DEBUG = false;
  36.     private JPanel thePanel;
  37.     List<OrderClientProducent> data;
  38.     private MyTableModel model;
  39.     JComboBox comboBox;
  40.     JTable table;
  41.  
  42.  
  43.  
  44.  
  45.     Context context = null;
  46.     ConnectionFactory factory = null;
  47.     Connection connection = null;
  48.     String factoryName = "ConnectionFactory";
  49.     String destName = "queue1";
  50.     Destination dest = null;
  51.  
  52.     Session session = null;
  53.     MessageProducer sender = null;
  54.     String text = "Message ";
  55.  
  56.  
  57.  
  58.  
  59.     public TableClientProducer() {
  60.         data = new ArrayList<OrderClientProducent>();
  61.         //super(new GridLayout(1,0));
  62.         thePanel = new JPanel(new BorderLayout());
  63.         model = new MyTableModel();
  64.  
  65.  
  66.         table = new JTable(new MyTableModel());
  67.         table.setPreferredScrollableViewportSize(new Dimension(800, 70));
  68.         table.setFillsViewportHeight(true);
  69.         table.getModel().addTableModelListener(this);
  70.         //Create the scroll pane and add the table to it.
  71.         JScrollPane scrollPane = new JScrollPane(table);
  72.  
  73.         //Set up column sizes.
  74.         ////initColumnSizes(table);
  75.  
  76.         //Fiddle with the Sport column's cell editors/renderers.
  77.         setUpStatusColumn(table, table.getColumnModel().getColumn(6));
  78.  
  79.         //Add the scroll pane to this panel.
  80.         thePanel.add(scrollPane, BorderLayout.CENTER);
  81.     }
  82.     public JPanel getPanel() {
  83.         return thePanel;
  84.     }
  85.  
  86.     public void addData(OrderClientProducent ocp){
  87.         data.add(ocp);
  88.         int size = data.size();     
  89.         model.fireTableRowsInserted(size, size);
  90.     }
  91.     public void tableChanged(TableModelEvent e) {
  92.         int row = e.getFirstRow();
  93.         int column = e.getColumn();
  94.         MyTableModel model = (MyTableModel)e.getSource();
  95.         String columnName = model.getColumnName(column);
  96.         Object data = model.getValueAt(row, column);
  97.  
  98.         // Do something with the data...
  99.  
  100.  
  101.  
  102.  
  103.  
  104.     }
  105.     public void setComoboxSelectedIndex(String value) {
  106.         if(value.equals("Nie potwierdzono"))  
  107.              comboBox.setSelectedIndex(0); 
  108.         else if (value.equals("Potwierdzono"))
  109.             comboBox.setSelectedIndex(1); 
  110.         else if (value.equals("Dostarczono"))
  111.             comboBox.setSelectedIndex(2); 
  112.         else if (value.equals("Nie dostarczono"))
  113.             comboBox.setSelectedIndex(3);
  114.         else if (value.equals("Zamkniete"))
  115.             comboBox.setSelectedIndex(4);
  116.         else comboBox.setSelectedIndex(5);
  117.    }
  118.     public int GetIndexRowSelected(){return table.getSelectedRow();} 
  119.     /*
  120.      * This method picks good column sizes.
  121.      * If all column heads are wider than the column's cells'
  122.      * contents, then you can just use column.sizeWidthToFit().
  123.      */
  124.  
  125.     public void clearRows(){
  126.         System.out.println(data.size());
  127.         //theTableModel.fireTableRowsDeleted(0,theRows.size()-1);
  128.         //for(int i=0;i<theRows.size();i++){
  129.             ///theRows.remove(i);}
  130.         int numRows = data.size()-1;
  131.         for (int i=numRows;i>=0;i--) {
  132.             data.remove(i);
  133.             model.fireTableRowsDeleted(i,i);
  134.         }    
  135.      }
  136.     public void setUpStatusColumn(JTable table,
  137.                                  TableColumn statusColumn) {
  138.         //Set up the editor for the sport cells.
  139.         comboBox = new JComboBox();
  140.         comboBox.addItem("Nie potwierdzono");
  141.         comboBox.addItem("Potwierdzono");
  142.         comboBox.addItem("Dostarczono");
  143.         comboBox.addItem("Nie dostarczono");
  144.         comboBox.addItem("Zamkniete");
  145.         comboBox.addItem("Anulowane");
  146.  
  147.  
  148.  
  149.         statusColumn.setCellEditor(new DefaultCellEditor(comboBox));
  150.  
  151.         //Set up tool tips for the sport cells.
  152.         DefaultTableCellRenderer renderer =
  153.                 new DefaultTableCellRenderer();
  154.         renderer.setToolTipText("Kliknij aby ukazal sie combo box");
  155.         statusColumn.setCellRenderer(renderer);
  156.     }
  157.  
  158.  
  159.     public int getID(int row){
  160.         OrderClientProducent orderclientproducer = (OrderClientProducent) data.get(row);
  161.         return orderclientproducer.getID();}
  162.  
  163.  
  164.       public String getStatus(int row){
  165.           OrderClientProducent orderclientproducer = (OrderClientProducent) data.get(row);
  166.         return orderclientproducer.getStatus();}
  167.  
  168.       public String getIlosc(int row){
  169.           OrderClientProducent orderclientproducer = (OrderClientProducent) data.get(row);
  170.           return orderclientproducer.getQuantity();
  171.       }
  172.  
  173.       public int getNumerZamowienia(int row){
  174.           OrderClientProducent orderclientproducer = (OrderClientProducent) data.get(row);
  175.             return orderclientproducer.getOrderNumber();  
  176.  
  177.       }
  178.  
  179.       public String getDateSent(int row){
  180.           OrderClientProducent orderclientproducer = (OrderClientProducent) data.get(row);
  181.           return orderclientproducer.getDateSent(); 
  182.       }
  183.      public String getDateDeadline(int row)    {
  184.  
  185.          OrderClientProducent orderclientproducer = (OrderClientProducent) data.get(row);
  186.  
  187.          return orderclientproducer.getDateReceived();
  188.      }    
  189.  
  190.  
  191. class MyTableModel extends AbstractTableModel {
  192.         String[] columnNames = {"ID","Nazwa klienta","Ilosc",
  193.                 "Numer Zamowienia","Data orzymania zamowienia","Termin dostawy","Status"};
  194.  
  195.  
  196.  
  197.         public int getColumnCount() {
  198.             return columnNames.length;
  199.         }
  200.  
  201.         public int getRowCount() {
  202.             return data.size();
  203.         }
  204.  
  205.         public String getColumnName(int col) {
  206.             return columnNames[col];
  207.         }
  208.  
  209.         public Object getValueAt(int row, int col) {
  210.             OrderClientProducent orderclientproducer = data.get(row);
  211.             if(col==0)return orderclientproducer.getID();
  212.             else if (col==1) return orderclientproducer.getClientName();
  213.             else if (col==2) return orderclientproducer.getQuantity();
  214.             else if(col==3) return orderclientproducer.getOrderNumber();
  215.             else if(col==4) return orderclientproducer.getDateSent();
  216.             else if(col==5){  
  217.  
  218.                 return orderclientproducer.getDateReceived();
  219.  
  220.  
  221.  
  222.             }
  223.             else return orderclientproducer.getStatus();
  224.         }
  225.  
  226.  
  227.         /*
  228.          * JTable uses this method to determine the default renderer/
  229.          * editor for each cell.  If we didn't implement this method,
  230.          * then the last column would contain text ("true"/"false"),
  231.          * rather than a check box.
  232.          */
  233.         public Class getColumnClass(int c) {
  234.             return getValueAt(0, c).getClass();
  235.         }
  236.  
  237.         /*
  238.          * Don't need to implement this method unless your table's
  239.          * editable.
  240.          */
  241.         public boolean isCellEditable(int row, int col) {
  242.  
  243.             if (col == 5 || col==6 ) {
  244.                 return true;
  245.             } else {
  246.                 return false;
  247.             }
  248.         }
  249.         public void setValueAt(Object value,int row, int col) {
  250.  
  251.             OrderClientProducent orderclientproducer = data.get(row);
  252.             int id=orderclientproducer.getID();
  253.             int n=orderclientproducer.getOrderNumber();
  254.             String q=orderclientproducer.getQuantity();
  255.             String d_s=orderclientproducer.getDateSent();
  256.  
  257.             String d_r=getDateDeadline(row);
  258.             String s=orderclientproducer.getStatus();
  259.             ObjectContainer db = Db4o.openFile("ordersProducer.yap"); 
  260.             try{
  261.                 ObjectSet result = db.get(new OrderClientProducent(id,"Client",q,n,d_s,d_r,s));    
  262.                 OrderClientProducent ocp = (OrderClientProducent)result.next();
  263.                 ocp.setStatus(value);
  264.                 db.set(ocp);
  265.             }
  266.             finally {db.close();}
  267.             orderclientproducer.setStatus(value);       
  268.             fireTableCellUpdated(row, col);  
  269.  
  270.  
  271.  
  272.  
  273.         try {
  274.               // create the JNDI initial context.
  275.               context = new InitialContext();
  276.  
  277.               // look up the ConnectionFactory
  278.               factory = (ConnectionFactory) context.lookup(factoryName);
  279.  
  280.               // look up the Destination
  281.               dest = (Destination) context.lookup(destName);
  282.  
  283.               // create the connection
  284.               connection = factory.createConnection();
  285.  
  286.               // create the session
  287.               session = connection.createSession(
  288.                   false, Session.AUTO_ACKNOWLEDGE);
  289.  
  290.               // create the sender
  291.               sender = session.createProducer(dest);
  292.  
  293.               // start the connection, to enable message sends
  294.               connection.start();
  295.  
  296.   System.out.println("Sender sending to client!");
  297.  
  298.   TextMessage message = session.createTextMessage();
  299.   message.setText(id+":"+q+":"+n+":"+d_r);
  300.   sender.send(message);
  301.   System.out.println("Sent: " + message.getText());
  302.  
  303.  
  304.  
  305.  
  306.  
  307.           } catch (JMSException exception) {
  308.               exception.printStackTrace();
  309.           } catch (NamingException exception) {
  310.               exception.printStackTrace();
  311.           } catch (NumberFormatException exception){
  312.                //the string could not be converted to Int.
  313.                exception.printStackTrace();
  314.                System.out.println("Nie wybrano zadnego wiersza!");
  315.  
  316.  
  317.           } finally {
  318.               // close the context
  319.               if (context != null) {
  320.                   try {
  321.                       context.close();
  322.                   } catch (NamingException exception) {
  323.                       exception.printStackTrace();
  324.                   }
  325.               }
  326.  
  327.               // close the connection
  328.               if (connection != null) {
  329.                   try {
  330.                       connection.close();
  331.                   } catch (JMSException exception) {
  332.                       exception.printStackTrace();
  333.                   }
  334.               }
  335.           }//finally 
  336.  
  337.  
  338.  
  339.         }}}
  340.  
Jun 18 '07 #1
7 3394
blazedaces
284 100+
First of all, please don't post a lot of code for people to fish through. Not only is this against the posting guidelines, but think about it, how many people will see all that code and really want to read through it to find your problem. Help people help you.

You could have at least showed where it's "supposed" to be setting the value.

Is it right here?:

Expand|Select|Wrap|Line Numbers
  1.             orderclientproducer.setStatus(value);       
  2.             fireTableCellUpdated(row, col);  
  3.  
If so, when you do "fireTableCellUpdated(row, col);" I don't see you're passing any "value" to it. Could that be your problem? If not, show exactly where you think you're changing both table values.

Thank you and good luck.
Hope this was helpful,
-blazed
Jun 19 '07 #2
oll3i
679 512MB
normally i wd do it data[row][col]=value but my data is a list of OrderClientProducent and in that OrderClientProducent i need to change the dateReceived and status if somebody enteres the date received or changes the combobox
Jun 19 '07 #3
oll3i
679 512MB
whne i try to change the value in one of these columns i get

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException
at com.db4o.foundation.Iterable4Adaptor.next(Unknown Source)
at com.db4o.internal.query.result.StatefulQueryResult .next(Unknown Sourc
e)
at com.db4o.internal.query.ObjectSetFacade.next(Unkno wn Source)
at TableClientProducer$MyTableModel.setValueAt(tablec lientproducer.java:
278)
at javax.swing.JTable.setValueAt(JTable.java:2676)
at javax.swing.JTable.editingStopped(JTable.java:4675 )
at javax.swing.AbstractCellEditor.fireEditingStopped( AbstractCellEditor.
java:125)
at javax.swing.DefaultCellEditor$EditorDelegate.stopC ellEditing(DefaultC
ellEditor.java:330)
at javax.swing.DefaultCellEditor$3.stopCellEditing(De faultCellEditor.jav
a:140)
at javax.swing.DefaultCellEditor.stopCellEditing(Defa ultCellEditor.java:
215)
at javax.swing.DefaultCellEditor$EditorDelegate.actio nPerformed(DefaultC
ellEditor.java:347)
at javax.swing.JComboBox.fireActionEvent(JComboBox.ja va:1242)
at javax.swing.JComboBox.setSelectedItem(JComboBox.ja va:569)
at javax.swing.JComboBox.setSelectedIndex(JComboBox.j ava:605)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mou seReleased(BasicCom
boPopup.java:814)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEven tMulticaster.java:2
73)
at java.awt.Component.processMouseEvent(Component.jav a:6038)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3260)
at javax.swing.plaf.basic.BasicComboPopup$1.processMo useEvent(BasicCombo
Popup.java:480)
at java.awt.Component.processEvent(Component.java:580 3)
at java.awt.Container.processEvent(Container.java:205 8)
at java.awt.Component.dispatchEventImpl(Component.jav a:4410)
at java.awt.Container.dispatchEventImpl(Container.jav a:2116)
at java.awt.Component.dispatchEvent(Component.java:42 40)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4322
)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3986)

at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3916)
at java.awt.Container.dispatchEventImpl(Container.jav a:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429 )
at java.awt.Component.dispatchEvent(Component.java:42 40)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThre
ad.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.
java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThre
ad.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:168)

at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:160)

at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:121)
Jun 19 '07 #4
blazedaces
284 100+
whne i try to change the value in one of these columns i get
Expand|Select|Wrap|Line Numbers
  1. TableClientProducer$MyTableModel.setValueAt(tableclientproducer.java:
  2. 278)
As far as I can tell this is the only line you should worry about because it's the one you have control over. Line 278 is blank in the code above, it could be the line before, which happens sometimes, but more likely, I have to ask is the code you posted exactly how it looks and was at the time that error was produced? If not redo it and post the line this error code says it's at.

Also, your table changed event, what is it supposed to do? It doesn't seem to change anything and you still don't seem to pass value somehow so how would the event know what value to change that row,col to? Perhaps make an instance variable and make it equal to value?

Good luck,
-blazed

Edit: I didn't read the "// do something with the data" part... So, can I ask what that part of the code looks like? It would help...

-blazed
Jun 19 '07 #5
oll3i
679 512MB
on line 278 i have
OrderClientProducent ocp = (OrderClientProducent)result.next();
i get an object from db4o to set its status and delievery deadline
Jun 19 '07 #6
blazedaces
284 100+
on line 278 i have
OrderClientProducent ocp = (OrderClientProducent)result.next();
i get an object from db4o to set its status and delievery deadline
Could the problem be that it's not casting correctly? I don't know, result is an OrderClientProducent object already, why do you need to cast it? I might be confused.

Try printing out result.next()'s contents before you do this, see if something is wrong with result...

Just trying to figure out where the problem lies.

Unfortunately, I'm not an expert and am not familiar with db4o, so I won't be of much help with specifics there...

-blazed
Jun 19 '07 #7
oll3i
679 512MB
i made it :) it changes the values in both columns it was db4o throwing the exception stilll have to solve the second part with db4o
thank YOU
Jun 19 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Jon Brunson | last post: by
1 post views Thread by Amos | last post: by
13 posts views Thread by scorpion53061 | last post: by
1 post views Thread by Tim Cowan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.