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

Thread: own Tablemodel with checkbox

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default own Tablemodel with checkbox

    Can anyone tell my why my code isn't working as intended?
    i want to create a table with a check box at the end for deleting several rows easily.
    I'm working with Netbeans and it seems that the way i'm creating my table doesnt play along with the boolean datatype i'm trying to use.

    Any tips would be highly appreciated.
    Thanks in advance

    public class UserTab {
     
        private String name;
        private String pw;
        boolean loeschen=false;
     
        public UserTab(String n, String p)
        {
            name=n;
            pw=p;
            //loeschen=false;
        }
     
        UserTab() {
            name="";
            pw="";
            //loeschen=false;
        }
     
        public String getName()
        {
            return name;
        }
        public String getPw()
        {
            return pw;
        }
     
        public boolean delete()
        {
            return loeschen;
        }
     
        void setName(String string) {
            name=string;
        }
     
        void setPw(String string) {
            pw=string;
        }
     
        void setLoeschen(boolean b) {
            loeschen=b;
        }
     
     
    }

    public class UserDatenmodell extends AbstractTableModel{
     
        private Vector<UserTab> user;
     
        public UserDatenmodell(Vector<UserTab> u)
        {
            this.user=u;
        }
     
        public int getRowCount() {
            return this.user.size();
        }
     
        public int getColumnCount() {
            return 3;
        }
     
        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Object erg = null;
            UserTab ut=user.get(rowIndex);
            switch(columnIndex)
            {
                case 0: erg=ut.getName(); break;
                case 1: erg=ut.getPw(); break;
                case 2: erg=ut.delete(); break;
     
            }
            return erg;
     
        }
     
        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex)
        {
            if(columnIndex == 0)
                return false;
            else
              return true;
        }
     
        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex)
        {
            UserTab k = user.get(rowIndex);
     
            switch(columnIndex)
            {
                case 0: k.setName((String) aValue);
                        break;
     
                case 1: k.setPw((String) aValue);
                        break;
     
                case 2: boolean b = Boolean.valueOf(aValue.toString());
                        k.setLoeschen(b);
                        break;
     
            }
        }
     
        @Override
        public Class<?> getColumnClass(int columnIndex)
        {
            Class<?> erg = String.class;
     
            if(columnIndex == 2)
                erg=boolean.class;
     
     
     
     
            return erg;
        }
    }

    public class UserScreen extends javax.swing.JFrame {
     
        Vector<UserTab> v=new Vector<UserTab>();
     
        /** Creates new form UserScreen */
        public UserScreen() {
            initComponents();
            UserDatenmodell ud = new UserDatenmodell(v);
            jTable1.setModel(ud);
            v.add(new UserTab());
            jTable1.updateUI();
     
     
        }
     
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
     
            zurueck = new javax.swing.JButton();
            jScrollPane1 = new javax.swing.JScrollPane();
            jTable1 = new javax.swing.JTable();
            update_button = new javax.swing.JButton();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            zurueck.setText("Zurück");
            zurueck.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    zurueckActionPerformed(evt);
                }
            });
     
            jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
     
                },
                new String [] {
     
                }
            ));
            jScrollPane1.setViewportView(jTable1);
     
            update_button.setText("Update");
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(update_button)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 248, Short.MAX_VALUE)
                    .addComponent(zurueck)
                    .addContainerGap())
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(zurueck)
                        .addComponent(update_button))
                    .addContainerGap())
            );
     
            pack();
        }// </editor-fold>
     
        private void zurueckActionPerformed(java.awt.event.ActionEvent evt) {
            LoggedIn li=new LoggedIn();
            li.setVisible(true);
            this.dispose();
        }
     
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new UserScreen().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTable jTable1;
        private javax.swing.JButton update_button;
        private javax.swing.JButton zurueck;
        // End of variables declaration
     
    }


  2. #2
    Member
    Join Date
    Dec 2011
    Posts
    50
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: own Tablemodel with checkbox

    Because you weren't using your custom model. It should be:

    jTable1.setModel(new UserDatenmodell());

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: own Tablemodel with checkbox

    thanks for your post but i basically did that by setting the model to my vector.
    it does work perfectly if i don't use a boolean type, but i want a checkbox next to every row of my table.

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: own Tablemodel with checkbox

    anyone any idea why it doesn't work? :/

    here is the exception thrown:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at javax.swing.JTable.prepareRenderer(JTable.java:5720)
            at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2072)
            at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1974)
            at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1770)
            at javax.swing.plaf.ComponentUI.update(ComponentUI.java:143)
            at javax.swing.JComponent.paintComponent(JComponent.java:752)
            at javax.swing.JComponent.paint(JComponent.java:1029)
            at javax.swing.JComponent.paintChildren(JComponent.java:862)
            at javax.swing.JComponent.paint(JComponent.java:1038)
            at javax.swing.JViewport.paint(JViewport.java:747)
            at javax.swing.JComponent.paintChildren(JComponent.java:862)
            at javax.swing.JComponent.paint(JComponent.java:1038)
            at javax.swing.JComponent.paintChildren(JComponent.java:862)
            at javax.swing.JComponent.paint(JComponent.java:1038)
            at javax.swing.JComponent.paintChildren(JComponent.java:862)
            at javax.swing.JComponent.paint(JComponent.java:1038)
            at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
            at javax.swing.JComponent.paintChildren(JComponent.java:862)
            at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131)
            at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1479)
            at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1410)
            at javax.swing.RepaintManager.paint(RepaintManager.java:1224)
            at javax.swing.JComponent.paint(JComponent.java:1015)
            at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
            at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
            at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
            at java.awt.Container.paint(Container.java:1780)
            at java.awt.Window.paint(Window.java:3375)
            at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
            at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
            at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
            at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
            at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
            at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:642)
            at java.awt.EventQueue.access$000(EventQueue.java:85)
            at java.awt.EventQueue$1.run(EventQueue.java:603)
            at java.awt.EventQueue$1.run(EventQueue.java:601)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:612)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at javax.swing.JTable.prepareRenderer(JTable.java:5720)
            at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2072)
            at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1974)
            at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1770)
            at javax.swing.plaf.ComponentUI.update(ComponentUI.java:143)
            at javax.swing.JComponent.paintComponent(JComponent.java:752)
            at javax.swing.JComponent.paint(JComponent.java:1029)
            at javax.swing.JComponent.paintChildren(JComponent.java:862)
            at javax.swing.JComponent.paint(JComponent.java:1038)
            at javax.swing.JViewport.paint(JViewport.java:747)
            at javax.swing.JComponent.paintChildren(JComponent.java:862)
            at javax.swing.JComponent.paint(JComponent.java:1038)
            at javax.swing.JComponent.paintChildren(JComponent.java:862)
            at javax.swing.JComponent.paint(JComponent.java:1038)
            at javax.swing.JComponent.paintChildren(JComponent.java:862)
            at javax.swing.JComponent.paint(JComponent.java:1038)
            at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
            at javax.swing.JComponent.paintChildren(JComponent.java:862)
            at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131)
            at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1479)
            at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1410)
            at javax.swing.RepaintManager.paint(RepaintManager.java:1224)
            at javax.swing.JComponent.paint(JComponent.java:1015)
            at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
            at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
            at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
            at java.awt.Container.paint(Container.java:1780)
            at java.awt.Window.paint(Window.java:3375)
            at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
            at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
            at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
            at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
            at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
            at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:642)
            at java.awt.EventQueue.access$000(EventQueue.java:85)
            at java.awt.EventQueue$1.run(EventQueue.java:603)
            at java.awt.EventQueue$1.run(EventQueue.java:601)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:612)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Last edited by Voldimore; February 21st, 2012 at 06:29 AM.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: own Tablemodel with checkbox

    can no one tell me how to solve this issue?

Similar Threads

  1. See if checkbox is checked
    By kney in forum Java Theory & Questions
    Replies: 9
    Last Post: December 7th, 2011, 07:23 AM
  2. Updating TableModel if changing data on DataBase(MySQL)
    By Farrukhjon in forum AWT / Java Swing
    Replies: 2
    Last Post: September 13th, 2011, 08:59 AM
  3. TableModel problem Java
    By GodspeedPR in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 23rd, 2011, 12:30 PM
  4. checkbox problem in jsp
    By kundan_101 in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: February 17th, 2011, 08:12 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, 09:26 PM