Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2005

Tip: Looking for answers? Try searching our database.

Jtable problem

Thread view: 
spark - 04 Feb 2005 10:08 GMT
Hi

i`m creating my table in that way:
mtm = new DefaultTableModel(dane,nazwyKolumn);
tablica = new JTable(mtm);
i`m getting data from database to global class variables dane and
nazwyKolumn
everything is ok, table shows data but when i`m trying to delete row from it
it doesn`t work
i delete row in database
put new data in variables dane and nazwyKolumn from database
and execute mtm.fireTableDataChanged() and it does nothing.
spark
Andrew McDonagh - 04 Feb 2005 10:11 GMT
> Hi
>
[quoted text clipped - 9 lines]
> and execute mtm.fireTableDataChanged() and it does nothing.
> spark

are you calling mtm.fireTableDataChanged() explicitly yourself, or are
you calling removeRow(...), which in rurn calls fireTableDataChanged(..);
spark - 04 Feb 2005 10:46 GMT
explicitly mtm.fireTableDataChanged()
this is my Listener for button usun
a do this action when i push it :
usun.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          if (ktoryRow!=-1)
          {
            try
            {
            s.executeQuery("delete from plyty where idplyty ="+ktoryRow+"
update plyty set idplyty=idplyty-1 where idplyty>"
            +(ktoryRow-1));
            dodawanie();
            mtm.fireTableDataChanged();
            }
            catch(Exception eg)
            {
              System.out.println(eg.getMessage());
            }

          }

        }
      });

and this is my function dodawanie(), which i call when i want to get new
data from database
public void dodawanie()
   {
     try
      {

        ResultSet r8 = s.executeQuery("select count(*) from plyty");
         while (r8.next())
         {
           liczbaPlyt = r8.getInt(1);
         }

        dane = new String[liczbaPlyt][4];

        ResultSet r1 = s.executeQuery("select idplyty,nazwa,opis,dostepna
from plyty");
        int n1 = 0;
        while(r1.next())
        {
          dane[n1][0] = temp.toString(r1.getInt(1));

          dane[n1][1] = r1.getString(2);

          dane[n1][2] = r1.getString(3);

          dane[n1][3] = temp.toString(r1.getInt(4));

          n1++;
        }
     /*   for (int i=0;i<10;i++)
          for(int j=0;j<4;j++)
            System.out.print(" "+dane[i][j]); */
      }
      catch(Exception e)
      {
        System.out.println(e.getMessage());
      }

      //mtm.fireTableDataChanged();
   }
jonck@vanderkogel.net - 04 Feb 2005 10:51 GMT
In the API docs, look up fireTableRowsDeleted, this does what you want.

Regards, Jonck
Andrew McDonagh - 05 Feb 2005 11:34 GMT
> explicitly mtm.fireTableDataChanged()
> this is my Listener for button usun
[quoted text clipped - 64 lines]
>        //mtm.fireTableDataChanged();
>     }

Thats the problem.  You are using a DefaultTableModel which stores the
data the JTable is displaying.  You are then changing some other model
and asking the DefaultTableModel to inform others it has changed - when
in fact it hasn't. Your other model has.
spark - 06 Feb 2005 11:56 GMT
hmm i dont know if i understood you well
u wrote me that i`m changing some other model.Which other model?? I wrote
another class, this time i extends AbstractTableModel and it looks like this
and the problem is the same:
public class MojaTable extends AbstractTableModel
 {
   Object[][] values = null;
   String[] columnNames = null;
   private int columns = 0;
   private int rows = 0;
   private Integer temp;

   public MojaTable(){
     pobierzDane();
   }
   public void pobierzDane()
   {
     try
      {
        ResultSet r1 = s.executeQuery("select idplyty,nazwa,opis,dostepna
from plyty");
        ResultSetMetaData rsmd = r1.getMetaData();
        columns = rsmd.getColumnCount();
        columnNames = new String[columns];
        for (int i=0;i<columns;i++)
        columnNames[i] = rsmd.getColumnName(i+1);
        ResultSet r2 = s.executeQuery("select count(*) from plyty");
        r2.next();
        rows = r2.getInt(1);
        int n1 = 0;
        values = new Object[columns][rows];
        ResultSet r1a = s.executeQuery("select idplyty,nazwa,opis,dostepna
from plyty");
        while(r1a.next())
        {
          values[0][n1] = temp.toString(r1a.getInt(1));
          values[1][n1] = r1a.getString(2);
          values[2][n1] = r1a.getString(3);
          values[3][n1] = temp.toString(r1a.getInt(4));
          n1++;
        }

        for (int i=0;i<rows;i++)
          for(int j=0;j<columns;j++)
            {
              System.out.print(" "+values[j][i]);
              if(j==3)
                System.out.println("");
            }
      }
      catch(Exception e)
      {
        System.out.println("funkcja pobierz"+e.getMessage());
      }
      this.fireTableDataChanged();
   }
    /**
   * @see javax.swing.table.TableModel#getRowCount()
   */
   public int getRowCount() {
   return rows;
   }

   /**
   * @see javax.swing.table.TableModel#getColumnCount()
   */
   public int getColumnCount() {
   return columns;
   }
   /**
   * @see javax.swing.table.AbstractTableModel#getColumnName(int)
   */
   public String getColumnName(int column) {
   return columnNames[column];
   }

   /**
   * @see javax.swing.table.TableModel#getValueAt(int, int)
   */
   public Object getValueAt(int rowIndex, int columnIndex) {
   return values[columnIndex][rowIndex];
   }
 }

i create object
MojaTable mtm = new MojaTable();
JTable tablica = new JTable(mtm);

and in function usun if i delete row i call function mtm.pobierzDane()
it isn`t the same model i`m changing??what should i do to change the right
one??
thanks for being patient and help


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.