changing values in two columns in a table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    #1

    changing values in two columns in a table

    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(Obje ct 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<Order ClientProducent >();

    thank you
    [code=java]

    import javax.swing.Def aultCellEditor;
    import javax.swing.JCo mboBox;
    import javax.swing.JFr ame;
    import javax.swing.JPa nel;
    import javax.swing.JSc rollPane;
    import javax.swing.JTa ble;
    import javax.swing.eve nt.TableModelEv ent;
    import javax.swing.eve nt.TableModelLi stener;
    import javax.swing.tab le.AbstractTabl eModel;
    import javax.swing.tab le.DefaultTable CellRenderer;
    import javax.swing.tab le.TableCellRen derer;
    import javax.swing.tab le.TableColumn;

    import com.db4o.Db4o;
    import com.db4o.Object Container;
    import com.db4o.Object Set;

    import javax.naming.*;
    import javax.jms.*;

    import java.awt.Border Layout;
    import java.awt.Compon ent;
    import java.awt.Dimens ion;
    import java.awt.GridLa yout;

    import java.util.Array List;
    import java.util.List;
    /**
    * TableRenderDemo is just like TableDemo, except that it
    * explicitly initializes column sizes and it uses a combo box
    * as an editor for the Sport column.
    */
    public class TableClientProd ucer extends JPanel implements TableModelListe ner{
    private boolean DEBUG = false;
    private JPanel thePanel;
    List<OrderClien tProducent> data;
    private MyTableModel model;
    JComboBox comboBox;
    JTable table;




    Context context = null;
    ConnectionFacto ry factory = null;
    Connection connection = null;
    String factoryName = "ConnectionFact ory";
    String destName = "queue1";
    Destination dest = null;

    Session session = null;
    MessageProducer sender = null;
    String text = "Message ";




    public TableClientProd ucer() {
    data = new ArrayList<Order ClientProducent >();
    //super(new GridLayout(1,0) );
    thePanel = new JPanel(new BorderLayout()) ;
    model = new MyTableModel();


    table = new JTable(new MyTableModel()) ;
    table.setPrefer redScrollableVi ewportSize(new Dimension(800, 70));
    table.setFillsV iewportHeight(t rue);
    table.getModel( ).addTableModel Listener(this);
    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(tab le);

    //Set up column sizes.
    ////initColumnSizes (table);

    //Fiddle with the Sport column's cell editors/renderers.
    setUpStatusColu mn(table, table.getColumn Model().getColu mn(6));

    //Add the scroll pane to this panel.
    thePanel.add(sc rollPane, BorderLayout.CE NTER);
    }
    public JPanel getPanel() {
    return thePanel;
    }

    public void addData(OrderCl ientProducent ocp){
    data.add(ocp);
    int size = data.size();
    model.fireTable RowsInserted(si ze, size);
    }
    public void tableChanged(Ta bleModelEvent e) {
    int row = e.getFirstRow() ;
    int column = e.getColumn();
    MyTableModel model = (MyTableModel)e .getSource();
    String columnName = model.getColumn Name(column);
    Object data = model.getValueA t(row, column);

    // Do something with the data...





    }
    public void setComoboxSelec tedIndex(String value) {
    if(value.equals ("Nie potwierdzono"))
    comboBox.setSel ectedIndex(0);
    else if (value.equals(" Potwierdzono"))
    comboBox.setSel ectedIndex(1);
    else if (value.equals(" Dostarczono"))
    comboBox.setSel ectedIndex(2);
    else if (value.equals(" Nie dostarczono"))
    comboBox.setSel ectedIndex(3);
    else if (value.equals(" Zamkniete"))
    comboBox.setSel ectedIndex(4);
    else comboBox.setSel ectedIndex(5);
    }
    public int GetIndexRowSele cted(){return table.getSelect edRow();}
    /*
    * This method picks good column sizes.
    * If all column heads are wider than the column's cells'
    * contents, then you can just use column.sizeWidt hToFit().
    */

    public void clearRows(){
    System.out.prin tln(data.size() );
    //theTableModel.f ireTableRowsDel eted(0,theRows. size()-1);
    //for(int i=0;i<theRows.s ize();i++){
    ///theRows.remove( i);}
    int numRows = data.size()-1;
    for (int i=numRows;i>=0; i--) {
    data.remove(i);
    model.fireTable RowsDeleted(i,i );
    }
    }
    public void setUpStatusColu mn(JTable table,
    TableColumn statusColumn) {
    //Set up the editor for the sport cells.
    comboBox = new JComboBox();
    comboBox.addIte m("Nie potwierdzono");
    comboBox.addIte m("Potwierdzono ");
    comboBox.addIte m("Dostarczono" );
    comboBox.addIte m("Nie dostarczono");
    comboBox.addIte m("Zamkniete" );
    comboBox.addIte m("Anulowane" );



    statusColumn.se tCellEditor(new DefaultCellEdit or(comboBox));

    //Set up tool tips for the sport cells.
    DefaultTableCel lRenderer renderer =
    new DefaultTableCel lRenderer();
    renderer.setToo lTipText("Klikn ij aby ukazal sie combo box");
    statusColumn.se tCellRenderer(r enderer);
    }


    public int getID(int row){
    OrderClientProd ucent orderclientprod ucer = (OrderClientPro ducent) data.get(row);
    return orderclientprod ucer.getID();}


    public String getStatus(int row){
    OrderClientProd ucent orderclientprod ucer = (OrderClientPro ducent) data.get(row);
    return orderclientprod ucer.getStatus( );}

    public String getIlosc(int row){
    OrderClientProd ucent orderclientprod ucer = (OrderClientPro ducent) data.get(row);
    return orderclientprod ucer.getQuantit y();
    }

    public int getNumerZamowie nia(int row){
    OrderClientProd ucent orderclientprod ucer = (OrderClientPro ducent) data.get(row);
    return orderclientprod ucer.getOrderNu mber();

    }

    public String getDateSent(int row){
    OrderClientProd ucent orderclientprod ucer = (OrderClientPro ducent) data.get(row);
    return orderclientprod ucer.getDateSen t();
    }
    public String getDateDeadline (int row) {

    OrderClientProd ucent orderclientprod ucer = (OrderClientPro ducent) data.get(row);

    return orderclientprod ucer.getDateRec eived();
    }


    class MyTableModel extends AbstractTableMo del {
    String[] columnNames = {"ID","Nazwa klienta","Ilosc ",
    "Numer Zamowienia","Da ta orzymania zamowienia","Te rmin dostawy","Statu s"};



    public int getColumnCount( ) {
    return columnNames.len gth;
    }

    public int getRowCount() {
    return data.size();
    }

    public String getColumnName(i nt col) {
    return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
    OrderClientProd ucent orderclientprod ucer = data.get(row);
    if(col==0)retur n orderclientprod ucer.getID();
    else if (col==1) return orderclientprod ucer.getClientN ame();
    else if (col==2) return orderclientprod ucer.getQuantit y();
    else if(col==3) return orderclientprod ucer.getOrderNu mber();
    else if(col==4) return orderclientprod ucer.getDateSen t();
    else if(col==5){

    return orderclientprod ucer.getDateRec eived();



    }
    else return orderclientprod ucer.getStatus( );
    }


    /*
    * JTable uses this method to determine the default renderer/
    * editor for each cell. If we didn't implement this method,
    * then the last column would contain text ("true"/"false"),
    * rather than a check box.
    */
    public Class getColumnClass( int c) {
    return getValueAt(0, c).getClass();
    }

    /*
    * Don't need to implement this method unless your table's
    * editable.
    */
    public boolean isCellEditable( int row, int col) {

    if (col == 5 || col==6 ) {
    return true;
    } else {
    return false;
    }
    }
    public void setValueAt(Obje ct value,int row, int col) {

    OrderClientProd ucent orderclientprod ucer = data.get(row);
    int id=orderclientp roducer.getID() ;
    int n=orderclientpr oducer.getOrder Number();
    String q=orderclientpr oducer.getQuant ity();
    String d_s=orderclient producer.getDat eSent();

    String d_r=getDateDead line(row);
    String s=orderclientpr oducer.getStatu s();
    ObjectContainer db = Db4o.openFile(" ordersProducer. yap");
    try{
    ObjectSet result = db.get(new OrderClientProd ucent(id,"Clien t",q,n,d_s,d_r, s));
    OrderClientProd ucent ocp = (OrderClientPro ducent)result.n ext();
    ocp.setStatus(v alue);
    db.set(ocp);
    }
    finally {db.close();}
    orderclientprod ucer.setStatus( value);
    fireTableCellUp dated(row, col);




    try {
    // create the JNDI initial context.
    context = new InitialContext( );

    // look up the ConnectionFacto ry
    factory = (ConnectionFact ory) context.lookup( factoryName);

    // look up the Destination
    dest = (Destination) context.lookup( destName);

    // create the connection
    connection = factory.createC onnection();

    // create the session
    session = connection.crea teSession(
    false, Session.AUTO_AC KNOWLEDGE);

    // create the sender
    sender = session.createP roducer(dest);

    // start the connection, to enable message sends
    connection.star t();

    System.out.prin tln("Sender sending to client!");

    TextMessage message = session.createT extMessage();
    message.setText (id+":"+q+":"+n +":"+d_r);
    sender.send(mes sage);
    System.out.prin tln("Sent: " + message.getText ());





    } catch (JMSException exception) {
    exception.print StackTrace();
    } catch (NamingExceptio n exception) {
    exception.print StackTrace();
    } catch (NumberFormatEx ception exception){
    //the string could not be converted to Int.
    exception.print StackTrace();
    System.out.prin tln("Nie wybrano zadnego wiersza!");


    } finally {
    // close the context
    if (context != null) {
    try {
    context.close() ;
    } catch (NamingExceptio n exception) {
    exception.print StackTrace();
    }
    }

    // close the connection
    if (connection != null) {
    try {
    connection.clos e();
    } catch (JMSException exception) {
    exception.print StackTrace();
    }
    }
    }//finally



    }}}
    [/code]
  • blazedaces
    Contributor
    • May 2007
    • 284

    #2
    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?:

    Originally posted by oll3i
    Code:
            	orderclientproducer.setStatus(value);       
                fireTableCellUpdated(row, col);
    If so, when you do "fireTableCellU pdated(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

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      normally i wd do it data[row][col]=value but my data is a list of OrderClientProd ucent and in that OrderClientProd ucent i need to change the dateReceived and status if somebody enteres the date received or changes the combobox

      Comment

      • oll3i
        Contributor
        • Mar 2007
        • 679

        #4
        whne i try to change the value in one of these columns i get

        Exception in thread "AWT-EventQueue-0" java.lang.Illeg alStateExceptio n
        at com.db4o.founda tion.Iterable4A daptor.next(Unk nown Source)
        at com.db4o.intern al.query.result .StatefulQueryR esult.next(Unkn own Sourc
        e)
        at com.db4o.intern al.query.Object SetFacade.next( Unknown Source)
        at TableClientProd ucer$MyTableMod el.setValueAt(t ableclientprodu cer.java:
        278)
        at javax.swing.JTa ble.setValueAt( JTable.java:267 6)
        at javax.swing.JTa ble.editingStop ped(JTable.java :4675)
        at javax.swing.Abs tractCellEditor .fireEditingSto pped(AbstractCe llEditor.
        java:125)
        at javax.swing.Def aultCellEditor$ EditorDelegate. stopCellEditing (DefaultC
        ellEditor.java: 330)
        at javax.swing.Def aultCellEditor$ 3.stopCellEditi ng(DefaultCellE ditor.jav
        a:140)
        at javax.swing.Def aultCellEditor. stopCellEditing (DefaultCellEdi tor.java:
        215)
        at javax.swing.Def aultCellEditor$ EditorDelegate. actionPerformed (DefaultC
        ellEditor.java: 347)
        at javax.swing.JCo mboBox.fireActi onEvent(JComboB ox.java:1242)
        at javax.swing.JCo mboBox.setSelec tedItem(JComboB ox.java:569)
        at javax.swing.JCo mboBox.setSelec tedIndex(JCombo Box.java:605)
        at javax.swing.pla f.basic.BasicCo mboPopup$Handle r.mouseReleased (BasicCom
        boPopup.java:81 4)
        at java.awt.AWTEve ntMulticaster.m ouseReleased(AW TEventMulticast er.java:2
        73)
        at java.awt.Compon ent.processMous eEvent(Componen t.java:6038)
        at javax.swing.JCo mponent.process MouseEvent(JCom ponent.java:326 0)
        at javax.swing.pla f.basic.BasicCo mboPopup$1.proc essMouseEvent(B asicCombo
        Popup.java:480)
        at java.awt.Compon ent.processEven t(Component.jav a:5803)
        at java.awt.Contai ner.processEven t(Container.jav a:2058)
        at java.awt.Compon ent.dispatchEve ntImpl(Componen t.java:4410)
        at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:2116)
        at java.awt.Compon ent.dispatchEve nt(Component.ja va:4240)
        at java.awt.Lightw eightDispatcher .retargetMouseE vent(Container. java:4322
        )
        at java.awt.Lightw eightDispatcher .processMouseEv ent(Container.j ava:3986)

        at java.awt.Lightw eightDispatcher .dispatchEvent( Container.java: 3916)
        at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:2102)
        at java.awt.Window .dispatchEventI mpl(Window.java :2429)
        at java.awt.Compon ent.dispatchEve nt(Component.ja va:4240)
        at java.awt.EventQ ueue.dispatchEv ent(EventQueue. java:599)
        at java.awt.EventD ispatchThread.p umpOneEventForF ilters(EventDis patchThre
        ad.java:273)
        at java.awt.EventD ispatchThread.p umpEventsForFil ter(EventDispat chThread.
        java:183)
        at java.awt.EventD ispatchThread.p umpEventsForHie rarchy(EventDis patchThre
        ad.java:173)
        at java.awt.EventD ispatchThread.p umpEvents(Event DispatchThread. java:168)

        at java.awt.EventD ispatchThread.p umpEvents(Event DispatchThread. java:160)

        at java.awt.EventD ispatchThread.r un(EventDispatc hThread.java:12 1)

        Comment

        • blazedaces
          Contributor
          • May 2007
          • 284

          #5
          Originally posted by oll3i
          whne i try to change the value in one of these columns i get
          Code:
          TableClientProducer$MyTableModel.setValueAt(tableclientproducer.java:
          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

          Comment

          • oll3i
            Contributor
            • Mar 2007
            • 679

            #6
            on line 278 i have
            OrderClientProd ucent ocp = (OrderClientPro ducent)result.n ext();
            i get an object from db4o to set its status and delievery deadline

            Comment

            • blazedaces
              Contributor
              • May 2007
              • 284

              #7
              Originally posted by oll3i
              on line 278 i have
              OrderClientProd ucent ocp = (OrderClientPro ducent)result.n ext();
              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 OrderClientProd ucent 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

              Comment

              • oll3i
                Contributor
                • Mar 2007
                • 679

                #8
                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

                Comment

                Working...