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 }