uzzi7862's Profile
Reputation: 0
Apprentice
- Group:
- Members
- Active Posts:
- 17 (0.04 per day)
- Joined:
- 28-February 12
- Profile Views:
- 287
- Last Active:
Sep 30 2012 06:45 AM- Currently:
- Offline
Previous Fields
- Dream Kudos:
- 0
Posts I've Made
-
In Topic: database content on a table in java with a column for checkboxes
Posted 20 Sep 2012
pbl, on 19 September 2012 - 03:50 AM, said:You will need a CellRenderer for that column
A Boolean toString() method returns "true" or "false"
hello. i was wondering if you could perhaps help me with the cell renderer for the column and the boolean to string method since i cannot seem to get it right in my program -
In Topic: database content on a table in java with a column for checkboxes
Posted 19 Sep 2012
hey thank you.
but when i run it for some odd reason it just writes the word false in the block instead of actually displaying it. any reason y that could happen since the code you gave me is fine.
-
In Topic: database content on a table in java with a column for checkboxes
Posted 18 Sep 2012
pbl, on 18 September 2012 - 01:57 PM, said:A lot complicated for nothing
Take that one
http://www.dreaminco...snippet6437.htm
just make sure to add a column to the number of columns and an array of boolean for your first column
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
i know i must be irritating you but i am new to this and was wondering if you could tell me where exactly to put the boolean array (and how to create it)in the model im proving which is implemented from the snap you gave me the link to.and can you tell me if i added the column correctly
public class Check extends JFrame { Connection con; Statement st; ResultSet rs; ResultSetMetaData metaData; // Vector columnNames = new Vector(); // Vector data = new Vector(); private static final int CHECK_COL = 3; public Check() { connect(); a(); } public void connect(){ try{ String driver="sun.jdbc.odbc.JdbcOdbcDriver"; Class.forName(driver); //database used String db="jdbc:odbc:td"; con=DriverManager.getConnection(db); st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); //sql query String sql="select * from Table1 "; rs=st.executeQuery(sql); }catch(Exception ex){} } public void a(){ JFrame f = new JFrame(); f.setSize(800, 600); TableModelFromRS tmfr= new TableModelFromRS(rs); JTable table = new JTable(tmfr); f.add(table); f.setVisible(true); } public class TableModelFromRS extends AbstractTableModel { private static final long serialVersionUID = 1L; // the Column names based on the DB private String[] columnName; // ArrayList to hold each row composed of an array of Object private ArrayList<Object[]> al; TableModelFromRS(ResultSet rs) { try { // meta data to get info on the database ResultSetMetaData metaData = rs.getMetaData(); // number of columns present in the ResultSet int nbCol = metaData.getColumnCount(); columnName = new String[nbCol+1]; //creating boolean array Boolean[] array = new Boolean[nbCol]; Arrays.fill(array, Boolean.FALSE); // Load the Array of column names for (int i = 0; i < nbCol; ++i) { columnName[i] = metaData.getColumnLabel(i + 1); // +1 SQL columns start at 1 } // Now get the rows al = new ArrayList<Object[]>(); // while there are still rows in the ResultSet while (rs.next()) { Object[] row = new Object[nbCol]; // retreive each column for (int i = 0; i < nbCol; i++) { row[i] = rs.getObject(i+1); // +1 SQL columns start at 1 } // insert into arrayList al.add(row); } } catch (Exception e) { System.out.println("Error building TableModel: " + e); e.printStackTrace(); } } // the number of rows is the number of entries in the ArrayList public int getRowCount() { return al.size(); } // the number of columms is the size of the the Array of column name public int getColumnCount() { return columnName.length; } // we retreive the array of Object[] at that index in the ArrayList // then the object at the required column public Object getValueAt(int rowIndex, int columnIndex) { return al.get(rowIndex)[columnIndex]; } // the column name is in the String[] array of column name public String getColumnName(int columnIndex) { return columnName[columnIndex]; } } public static void main(String args[])throws SQLException { new Check(); } } -
In Topic: database content on a table in java with a column for checkboxes
Posted 18 Sep 2012
hello i have this code but cant seem to get it right could you perhaps help me with finding my error
public class Check extends JFrame { Connection con; Statement st; ResultSet rs; ResultSetMetaData metaData; // Vector columnNames = new Vector(); // Vector data = new Vector(); private static final int CHECK_COL = 3; public Check()throws SQLException { connect(); a(); } public void connect(){ try{ String driver="sun.jdbc.odbc.JdbcOdbcDriver"; Class.forName(driver); //database used String db="jdbc:odbc:td"; con=DriverManager.getConnection(db); st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); //sql query String sql="select * from Table1 "; rs=st.executeQuery(sql); }catch(Exception ex){} } public void a() throws SQLException{ setTitle("MARKING OF TARGET HABITATION"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); metaData=rs.getMetaData(); int columns = metaData.getColumnCount(); String[] columnNames= new String[columns+1]; int num=0; for (int i = 1; i <= columns; i++) { columnNames[num]=metaData.getColumnName(i); num=num+1; } rs.last(); int numrows=rs.getRow(); int numcols=rs.getMetaData().getColumnCount(); rs.first(); Object[][] data=new Object[numcols][numrows]; while(rs.next()){ for(int c=1;c<numcols;c++){ for(int r=1;r<numrows;r++){ data[c][r]=rs.getObject(c); } } } DefaultTableModel dtm = new DefaultTableModel(data, columnNames) { @Override public Class getColumnClass(int col) { return getValueAt(0, col).getClass(); } @Override public boolean isCellEditable(int rowIndex, int colIndex) { return (colIndex == CHECK_COL); } }; final JTable table = new JTable(dtm); JScrollPane scrollPane = new JScrollPane(table); JButton button = new JButton("check"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for (int row = 0; row < table.getRowCount(); row++) { Boolean b = ((Boolean) table.getValueAt(row, CHECK_COL)); if (b.booleanValue()) { System.out.print("row " + row + " is " + b + ": "); for (int col = 0; col < table.getColumnCount(); col++) { System.out.print(table.getValueAt(row, col) + " "); } System.out.println(); } } } }); JPanel buttonpanel = new JPanel(); buttonpanel.add(button); add(scrollPane, BorderLayout.CENTER); add(buttonpanel, BorderLayout.SOUTH); pack(); setLocationByPlatform(true); setVisible(true); } public static void main(String args[])throws SQLException { new Check(); } } -
In Topic: database content on a table in java with a column for checkboxes
Posted 18 Sep 2012
how can i do it such that it is not hard coded i want it to come straight from database
My Information
- Member Title:
- New D.I.C Head
- Age:
- Age Unknown
- Birthday:
- Birthday Unknown
- Gender:
Contact Information
- E-mail:
- Click here to e-mail me
Friends
uzzi7862 hasn't added any friends yet.
|
|


Find Topics
Find Posts
View Reputation Given
|
Comments
uzzi7862 has no profile comments yet. Why not say hello?