Results 1 to 9 of 9
  1. #1
    ThommyW is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default Problem with JTable - only some rows are displayed

    Hello,

    I have an annoying problem. I use a JTable and a DefaultTableModel but I also tried customized models created by "extends DefaultTableModel" (with and without super()) and "extends AbstractTableModel".
    I am giving an array to it (String[][] with 315 lines ) and there are only 28 of 315 entries represented.
    I did not change any default option despite of "setPreferedsize" and "setVisible" and "setOpaque".
    Of course I verified the array created as a result of reading an excel file: Even in the model - giving it to the super constructor - it has exactly the size of 315 lines.
    As I print all rows simultaneously in the output there is no error of any type loading the data (anyway there would be no data at all in my table if this was the case , as I experienced several times.)

    By debugging I cannot find any error: all data are as they should be, except for the representation of the data. As this happens also in the defaultTableModel I am getting a little bit desperate because I begin to doubt in Java...
    I might give the relevant parts of the code here but I don't think this will help:

    Java Code:
     
    provcedure init() of the panel class (extends JPanel):
    
    public void init(Dimension d){
    GridBagLayout gbl = new GridBagLayout();
    this.setLayout(gbl);int textgrösse = 20; int kbeginSpalte = 0;int kbeginZeile = 0;
    data = new String[][] {{" ", " "," "}} ; /// dummy for table to show
    jtal = new JTable(); //BasicTable();
    column_names = getHeadersFromExcelTable("");
    jtal.setModel(new BasicTableModel(data,column_names));/// tried also DefaultTableModel
    BasicTableRenderer renderer = new BasicTableRenderer(jtal);
    jtal.setDefaultRenderer(Object.class, renderer);
    jtal.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    
    //JList rowHeader = new JList(new RowHeaderListModel(data.length)); //I verified that this is not the problem by commenting it out
    //rowHeader.setFixedCellWidth(50);
    // rowHeader.setFixedCellHeight(jtal.getRowHeight()+ jtal.getRowMargin()+
    /// jtal.getIntercellSpacing().height);
    // rowHeader.setCellRenderer(new RowHeaderRenderer(jtal));
    
    JTableHeader colheader = jtal.getTableHeader(); //colheader.setBackground(Color.gray);
    jtal.setRowHeight(jtal.getRowHeight()+ jtal.getRowMargin()+ jtal.getIntercellSpacing().height); 
    colheader.setOpaque(true);
    
    Dimension dsc = new Dimension(800,500);
    jtal.setPreferredSize(dsc);JScrollPane scr1 = new JScrollPane();//ScrollPane anlegen
    // scr1.setRowHeaderView(rowHeader);
    scr1.setPreferredSize(new Dimension(810,510)); scr1.add(jtal); scr1.setViewportView(jtal);
    
    btnLaden = new BasicButton("Messdaten Laden", textgrösse);
    btnLaden.setVisible(true);
    btnLaden.addActionListener(new ActionListener() {
    private String messDateiPfad;
    
    @Override
    public void actionPerformed(ActionEvent e) {
    JFileChooser fc = new JFileChooser();
    File f = null;
      try { 
           f = new File(new File(".").getCanonicalPath());
           } catch (IOException ex) {
           } fc.setCurrentDirectory(f);int returnVal = 0;
              returnVal = fc.showOpenDialog(btnLaden);
           if (returnVal == JFileChooser.APPROVE_OPTION) {
            File thefile = fc.getSelectedFile();
            messDateiPfad = thefile.getPath();
            }
    column_names = getHeadersFromExcelTable(messDateiPfad);
    data = fillTable( messDateiPfad);
    //jtal=new BasicTable(data,column_names);
    jtal.setAutoResizeMode(4);
    BasicTableModel btm =new BasicTableModel(data,column_names);
    jtal.setModel(btm);
    SwingUtilities.updateComponentTreeUI(jtal);
    }
    });
    
    this.addComp(gbl, scr1, kbeginSpalte,kbeginZeile,4,5,0,0,GridBagConstraints.NONE, GridBagConstraints.NORTHWEST);
    this.addComp(gbl, btnLaden, kbeginSpalte,kbeginZeile+6,4,1,0,0,GridBagConstraints.NONE, GridBagConstraints.NORTHWEST);
    
    //// addComp is the procedure given in "Java ist auch eine Insel": Layout GridBagLayout
    }
    
    public String[][] fillTable(String messDateiPfad)  {
       String[][] result = {{" ", " ", " "}} ;
       ReadExcel rxl = new ReadExcel();
       rxl.setInputFile(messDateiPfad);
       try {
            data = rxl.read(); result = data;
             } catch (IOException ex) 
             { Sytem.out.PrintLn("Error")
             }
         return result; 
     }
    
    private String[] getHeadersFromExcelTable(String messDateiPfad) {
    String[] result = {"Aktiv ?", "Quelle", "Bezeichnung"} ;
    ReadExcel rxl = new ReadExcel();
       if (!(messDateiPfad.equalsIgnoreCase("")))   {
       rxl.setInputFile(messDateiPfad);
       try { 
        result = rxl.readHeaders();
       } catch (BiffException ex) { } 
       catch (IOException ex) { } 
       }
      return result;
      }
    }
    
    /// the returned String [] [] data returned exactly all 315 lines and 18 columns of the Excel file.
    /// I will not give the code here: API JXL 2.8.3
    
    
    public class BasicTableModel extends AbstractTableModel      {
       public static final long serialVersionUID = 1111111L;
       private Object [][] daten = null;
       private Object[] headerNames = null;
       //private Class columnClasses[] = {String.class, String.class, String.class,Object.class, Object.class};
    
       public BasicTableModel()  {
         headerNames = new Object[1];
         }
    
      public BasicTableModel(String[] column_names, int i)  {
         headerNames = column_names;
         for(int j = 0; j< column_names.length;j++)   {
         headerNames[j] = (column_names[j]);
         }
     }
    
    public BasicTableModel(String[][] data,String[] columnNames)    {
        headerNames = new Object[columnNames.length];
        for(int j = 0; j< columnNames.length;j++) {
        headerNames[j]= columnNames[j];
        }
    
         daten = new Object[data.length][];
         for(int i = 0; i< data.length;i++)  {
         daten[i]= new Object[data[i].length];
            for(int j = 0; j< data[i].length;j++) {
            daten[i][j] = data[i][j]; 
            }
          }
     }
    
    @Override
        public int getRowCount() {
        return this.daten.length;
        }
    
    @Override
         public int getColumnCount() {
         return headerNames.length;
         }
    
    @Override
        public Object getValueAt(int rowIndex, int columnIndex)  {
        return daten[rowIndex][columnIndex];
        }
    }
    I have 18 columns in my table. I verified the columncount by System.out.print in the function getRowCount() in the model. Everything ok. I deleted a number of columns or rows (only 28, all from 1 to 74 etc.) in the file : no change: 28 lines starting from the first line not deleted are given. At any point I could imagine the right number of rows of the String [][] (315) is indicated.

    If I just use DefaultTablemodel and JTable (so no problem which I introduced myself normally) I get the same result: 28 lines, although there is no change in type of data or number of columns with data in those lines.

    I use java jdk 1.6.20- Anyway all other jdk's (1.6.27, 1.7.0) are crashing when I try to install them....


    If I don't find a solution the entire project will die .....

    Thanks
    Last edited by ThommyW; 09-21-2011 at 09:57 PM. Reason: forgot tags

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Problem with JTable - only some rows are displayed

    Put your code in [ code ] tags, otherwise you lose any formatting, making things really difficult to read.

  3. #3
    ThommyW is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default Re: Problem with JTable - only some rows are displayed

    Quote Originally Posted by Tolls View Post
    Put your code in [ code ] tags, otherwise you lose any formatting, making things really difficult to read.
    Sorry, it is the first time since years that I am in a forum. Furthermore tags are hard to find here: there is no symbol anywhere to surround the code by click as usual, it is not clear how to write exactly the tags and there is no indication anywhere (big Letters ?, slash or back slash ?). Or Did I overlook something? Anyway I hope it is better now. I doubt that the code will help much: Last Debugging did not show any problem. Unfortunately to give still more code (eg. reading Excel files with jxl to get the array) would be to much. The big question is how to reproduce this problem without using my whole program....

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Problem with JTable - only some rows are displayed

    Moved from "Advanced Java"

    db

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Problem with JTable - only some rows are displayed

    Do you really write your code without indents? Please go through Code Conventions for the Java(TM) Programming Language: Contents

    db

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with JTable - only some rows are displayed

    Agree, you'll want to fix your posted code because right now it is pretty well un-readable.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default Re: Problem with JTable - only some rows are displayed

    there is no symbol anywhere to surround the code by click as usual, it is not clear how to write exactly the tags and there is no indication anywhere (big Letters ?, slash or back slash ?). Or Did I overlook something?
    I thought I was the only one with this problem. I reported this problem a couple of weeks ago: Missing Markup Buttons

    @Pete, @Darryl, maybe you guys can follow-up on this. I would hope you have more influence than I do.

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default Re: Problem with JTable - only some rows are displayed

    The only suggestion I have is don't use a 2D array for the table model data. Rarely do you know the size of the data in advance. Instead use a Vectors or Vectors. Then it doesn't matter how much data you have because the Vectors can grow.

Similar Threads

  1. Jtable rows
    By riddhishah28 in forum AWT / Swing
    Replies: 3
    Last Post: 02-18-2011, 06:24 AM
  2. Remove Jtable rows
    By anilkumar_vist in forum Advanced Java
    Replies: 2
    Last Post: 09-17-2010, 08:03 AM
  3. Listening rows in JTable ??
    By Stephen Douglas in forum New To Java
    Replies: 2
    Last Post: 04-10-2010, 04:45 PM
  4. Updating Displayed Table Rows
    By raycini in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 04-20-2009, 08:24 AM
  5. Deleting All rows in the JTable
    By surot in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 10:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •