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 4 of 4

Thread: Checkbox doesnt change when click on JTable

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

    Default Checkbox doesnt change when click on JTable

    im building a custom Table for an application and everything is working except that the checkbox doesnt change when you click the mouse on top of it. Im new at java and really dont know what im doing wrong,
    Thank you in advance

    /*
     * 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) {
            File f = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial.txt");
            File g = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial1.txt");
            File h = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial2.txt");
            File i = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial3.txt");
            File j = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial4.txt");
     
            System.out.println(f.exists()); //checks if file exists
            JFrame frame = new JFrame("Table Test");
     
            FileTableModel data = new FileTableModel();
            JTable table = new JTable();
            table.setModel(data);
     
            data.addData(f);
            data.addData(g);
            data.addData(h);
            data.addData(i);
            data.addData(j);
            System.out.println(data);
            System.out.println("Data Added");
     
     
     
     
            JScrollPane scrollpane = new JScrollPane(table);
            frame.getContentPane().add(scrollpane,BorderLayout.CENTER);
            frame.setSize(300,150);
            frame.setVisible(true);
     
     
     
     
        }
    }
    class FileTableModel extends AbstractTableModel {
     
     
          private String[] columnNames = { "Selection", "File Name","File Path" };
          FileData rowData;
     
     
          FileTableModel()
          {
              rowData = new FileData();
          }
     
     
       @Override
        public Class getColumnClass(int column)
        {
            return getValueAt(0, column).getClass();
        }
     
        @Override
             public int getColumnCount() {
                 return columnNames.length;
             }
     
        @Override
             public int getRowCount() {
                 return rowData.getListSize();
             }
     
        @Override
             public String getColumnName(int col) {
                 return columnNames[col];
             }
     
        @Override
             public Object getValueAt(int row, int col) {
                if (col == 0)
                   {
                     return rowData.getSelection();
     
                   }
                if (col ==1)
                    {
                      return rowData.getFileName(row);
                    }
                if (col == 2)
                    {
                       return rowData.getFilePath(row);
                    }
                else return "Invalid Row or Column";
     
             }
     
        @Override
             public boolean isCellEditable(int row, int col)
             {
                   if (col == 0)
                       return true;
                   else
                       return false;
     
             }
        @Override
             public void setValueAt(Object value, int row, int col)
             {
     
             }
         public void addData(File f)
       {
         rowData.addData(f);
     
       }
     
     public void removeData(int row)
        {
         rowData.removeData(row);
        }
     
     
    }
     
     
     
     
     
    class FileData {
     
       private LinkedList <File> FileList = new LinkedList <File>();
       private Boolean selection = false;
     
       FileData()
        {
     
        }
     
        public int getListSize()
        {
            return FileList.size();
        }
     
        public String getFileName(int row)
        {
            return FileList.get(row).getName();
        }
        public String getFilePath(int index)
        {
     
            return FileList.get(index).getAbsolutePath();
        }
     
        public Boolean getSelection()
        {
            return selection;
        }
     
     
     public void addData(File f)
       {
         FileList.add(f);
     
       }
     
     public void removeData(int row)
        {
         FileList.remove(row);
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Checkbox doesnt change when click on JTable

    Please edit your code and wrap it in code tags. The currently posted code is very hard to read.
    See: BB Code List - Java Forums

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

    Default Re: Checkbox doesnt change when click on JTable

    Sorry About that! here it is
    /*
    * 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) {
    File f = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial.txt");
    File g = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial1.txt");
    File h = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial2.txt");
    File i = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial3.txt");
    File j = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial4.txt");
     
    System.out.println(f.exists()); //checks if file exists
    JFrame frame = new JFrame("Table Test");
     
    FileTableModel data = new FileTableModel();
    JTable table = new JTable();
    table.setModel(data);
     
    data.addData(f);
    data.addData(g);
    data.addData(h);
    data.addData(i);
    data.addData(j);
    System.out.println(data);
    System.out.println("Data Added");
     
     
     
     
    JScrollPane scrollpane = new JScrollPane(table);
    frame.getContentPane().add(scrollpane,BorderLayout .CENTER);
    frame.setSize(300,150);
    frame.setVisible(true);
     
     
     
     
    }
    }
    class FileTableModel extends AbstractTableModel {
     
     
    private String[] columnNames = { "Selection", "File Name","File Path" };
    FileData rowData;
     
     
    FileTableModel()
    {
    rowData = new FileData();
    }
     
     
    @Override
    public Class getColumnClass(int column)
    {
    return getValueAt(0, column).getClass();
    }
     
    @Override
    public int getColumnCount() {
    return columnNames.length;
    }
     
    @Override
    public int getRowCount() {
    return rowData.getListSize();
    }
     
    @Override
    public String getColumnName(int col) {
    return columnNames[col];
    }
     
    @Override
    public Object getValueAt(int row, int col) {
    if (col == 0)
    {
    return rowData.getSelection();
     
    }
    if (col ==1)
    {
    return rowData.getFileName(row);
    }
    if (col == 2)
    {
    return rowData.getFilePath(row);
    }
    else return "Invalid Row or Column";
     
    }
     
    @Override
    public boolean isCellEditable(int row, int col)
    {
    if (col == 0)
    return true;
    else
    return false;
     
    }
    @Override
    public void setValueAt(Object value, int row, int col)
    {
     
    }
    public void addData(File f)
    {
    rowData.addData(f);
     
    }
     
    public void removeData(int row)
    {
    rowData.removeData(row);
    }
     
     
    }
     
     
     
     
     
    class FileData {
     
    private LinkedList <File> FileList = new LinkedList <File>();
    private Boolean selection = false;
     
    FileData()
    {
     
    }
     
    public int getListSize()
    {
    return FileList.size();
    }
     
    public String getFileName(int row)
    {
    return FileList.get(row).getName();
    }
    public String getFilePath(int index)
    {
     
    return FileList.get(index).getAbsolutePath();
    }
     
    public Boolean getSelection()
    {
    return selection;
    }
     
     
    public void addData(File f)
    {
    FileList.add(f);
     
    }
     
    public void removeData(int row)
    {
    FileList.remove(row);
    }
    }

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Checkbox doesnt change when click on JTable

    The setValueAt method if your TableModel does nothing...implement this method to change the value that needs changing using the parameters and setting the appropriate value in your model

Similar Threads

  1. change JTable column name, gui does not update at once
    By renars in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 23rd, 2011, 10:39 AM
  2. [SOLVED] Help with the CheckBox button in JTable
    By Yamato in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 6th, 2010, 09:22 PM
  3. i can run my program but the math doesnt come otu
    By pairenoid in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 7th, 2010, 01:39 PM
  4. Checkbox - Change Font Color
    By wakebrdr77 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 09:57 AM
  5. how to delete record based on checkbox selected checkbox
    By -_- in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: December 15th, 2009, 08:26 PM