Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: TableModel problem Java

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default TableModel problem Java

    Im developing a program that has a JTable in it. The JTable should display a checkbox per row, the file name and the file path.
    The problem im having is Im new to java and im having a hard time understanding how to make custom table model. This is what i have so far. Excuse me for all the bad coding in it. Thank you
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package trialfiletablemodel;
     
    import java.awt.BorderLayout;
    import java.io.File;
    import java.util.LinkedList;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;
     
     
    /**
     * String fileName,filePath;
     * @author rosado
     */
     
    public class Main {
     
     
     
        public static void main(String[] args) {
        String fileName,filePath;
        File f = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial.txt");
        System.out.println(f.exists()); //checks if file exists
        JFrame frame = new JFrame("Table Test");
        TableData data = new TableData();
        fileName = data.getFileName(f);
        filePath = data.getFilePath(f);
     
        JTable table = new JTable(new FileTableModel());
        table.setValueAt(false, 0, 0);
        table.setValueAt(fileName, 0, 1);
        table.setValueAt(filePath, 0, 2);
     
     
        JScrollPane scrollpane = new JScrollPane(table);
        frame.getContentPane().add(scrollpane,BorderLayout.CENTER);
        frame.setSize(300,150);
        frame.setVisible(true);
     
        /*
        ArrayList <String> data = new ArrayList<String>();
        data.add(f.getAbsolutePath());
        data.add(f.getName());
        for(int x = 0; x<data.size();x++)
        {
            System.out.println(data.get(x));
        }
          */  
     
     
        }
    }
    class FileTableModel extends AbstractTableModel {
     
          public String[] columnNames = { "Selection", "File Name","File Path" };
     
          LinkedList <Object> tabledata = new LinkedList<Object>();
          public Class[]  columnTypes = {Boolean.class,String.class,String.class};
     
     
          //Constructor
           public FileTableModel(){
               super();
     
           }
     
      @Override
      public int getColumnCount() {
          return columnNames.length;
      }
        @Override
        public Class getColumnClass(int column){
             return (getValueAt(0, column).getClass());
        }
        @Override
      public String getColumnName(int column) {
        return columnNames[column];
      }
     
        @Override
      public int getRowCount() {
        return (tabledata.size());
      }
     
        @Override
      public Object getValueAt(int row, int col) {
            float index = (row/2);
     
            if ((index % 2) == 0)
                {
                    index =(int) index;
                }
            else 
                index = (int) index +1;
     
     
         return tabledata.get((int) index);
     }
     
        @Override
      public void setValueAt(Object value, int row, int column) {
       /*   TableData d = new TableData();
          Boolean fs = false;
          String fn = d.getFileName(f);
          String fp = d.getFilePath(f);
          tabledata.addLast(fs);
          tabledata.addLast(fn);
          tabledata.addLast(fp);
         */ 
     
       }
     
        @Override
      public boolean isCellEditable(int row, int column) {
            if(column == 0){
                return false;
            }
            else {
                return(true);
            }
      }
     
    }
     
    class TableData {
        private Boolean selection;
        private String fileName;
        private String filePath;
     
        public TableData()
        {
     
        }
        public TableData(Boolean sel, String name,String path)
        {
            selection = sel;
            fileName = name;
            filePath = path;
        }
     
        public Boolean getSelection()
        {
            return selection;
        }
        public String getFileName(File f)
        {
            fileName = f.getName();
            return fileName;
        }
        public String getFilePath(File f)
        {
            filePath = f.getAbsolutePath();
            return filePath;
        }
     
        public void setSelection(Boolean sel)
        {
            selection = sel;
        }
     
        public void setFileName(String name)
        {
            fileName = name;
        }
     
        public void setFilePath(String path)
        {
            filePath = path;
        }
    }
    Last edited by KevinWorkman; June 23rd, 2011 at 11:52 AM. Reason: added highlight tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: TableModel problem Java

    When posting code, make sure you use the highlight tags. I added them for you this time.

    What's the actual problem? Where are you stuck? Did you read through this: How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TableModel problem Java

    The problem is that when i open a new file it should create a new row on the table with the name and file path of the file and a checkbox. I tried running it but it doesnt display anything.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: TableModel problem Java

    I don't really understand the logic in your table model- why are you using a linked list instead of a 2-dimensional array or List?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TableModel problem Java

    This is just the tablemodel . The program really will let the user open up a file. It will load the file info to the JTable, open up and editor. The user can open more than one file at a time. So that why i used linked list. the checkbox will be to select which files to close with a jbutton.
    Thanks!!

  6. #6
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TableModel problem Java

    The table will just display the files currently open.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: TableModel problem Java

    ...I don't really know what that has to do with your problem.

    Hint: Take a look at what your getRowCount() method is returning.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. HTK files from Java problem.
    By ivanloes in forum Java Theory & Questions
    Replies: 0
    Last Post: December 17th, 2010, 06:49 PM
  2. java creator problem
    By vgenopoulos in forum Java Theory & Questions
    Replies: 5
    Last Post: July 7th, 2010, 09:58 AM
  3. Minesweeper java problem
    By bluez_exe in forum Paid Java Projects
    Replies: 1
    Last Post: March 3rd, 2010, 08:55 PM
  4. java. Text problem...
    By Ranger-Man in forum Java SE APIs
    Replies: 8
    Last Post: September 7th, 2009, 07:19 PM
  5. java problem
    By Mj Shine in forum Java Theory & Questions
    Replies: 1
    Last Post: August 14th, 2009, 12:24 AM