1

Hello i am new in using JTable of java, I have a multiple rresult from my database which is stored in resultset. My problem is i am using cellrendered in jtable and i dont have any way but to store resultset in String[][] or Object[][].

String[] columnNames = {"Employee ID", "Firstname","Lastname", "Position"};
String[][] database;//This is where i need to put the result set
DefaultTableModel model = new DefaultTableModel(database,columnNames){
    public Class getColumnClass(int column){
        return getValueAt(1, column).getClass();
    }
};

This is the function to create table

private JComponent createData(DefaultTableModel model)
{
        JTable table = new JTable( model )
        {
            public Component prepareRenderer(TableCellRenderer renderer, int row, int     column)
            {
                Component c = super.prepareRenderer(renderer, row, column);
                if (!isRowSelected(row))
                {
                    c.setBackground(getBackground());
                   int modelRow = convertRowIndexToModel(row);
                String type = (String)getModel().getValueAt(modelRow, 1);
                if ("Nick".equals(type)) c.setBackground(Color.GREEN);
                if ("Leth".equals(type)) c.setBackground(Color.YELLOW);`
            }

            return c;
        }
    };

    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    table.changeSelection(0, 0, false, false);
    table.setAutoCreateRowSorter(true);
    JScrollPane scrollPane = new JScrollPane( table );
    scrollPane.setBounds(252, 11, 404, 402);
    return scrollPane;
}

This is where i call function to create table getContentPane().add(createData(model)); and the String[][] database variable is the way to insert data from database. This is the way that i got in google, so far i dont have any idea in cellrendered thats why i think i need to stored result set in String[][] for me to accomplish my target. This is where i fetch the data from database

public void loadList(){
    String sql = "SELECT emp_id,lname,fname,positional_status from tblEmployee";
    try{
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        while (rs.next()){
          //what goes here to populate my data fetch from database and put in
          // String[][] database?
        }
    }catch(Exception err){
        JOptionPane.showMessageDialog(null, err);
    }

}

Any suggestion will be appreciated.

Lileth Hernandez
  • 61
  • 1
  • 1
  • 3
  • I don't think I quite understand what the exact problem is. Care to elaborate? What exactly is giving your problems or isn't working. Also for better help, I's suggest posting a [simple runnable example (MCVE)](http://stackoverflow.com/help/mcve) using hard coded value for values you expect from DB. This way we can just copy->paste->compile->run your code and better see where the problem is – Paul Samsotha Jul 18 '14 at 12:08
  • You could iterate through your `ResultSet` and build up your `String[][]` as you go. I don't know of any easier way, sorry. – Dawood ibn Kareem Jul 18 '14 at 12:15
  • David, thats exactly wat i want but i dont know how to do it. I am not realy good in iterating data from resultset to String[][]. – Lileth Hernandez Jul 19 '14 at 09:52
  • I suggest you take a look at the link that @Braj provided. You should be able to gain something from it. – Paul Samsotha Jul 19 '14 at 10:19

2 Answers2

1

The below post might help you.

For your learning and better understanding read more about How to Use Tables where Custom Renderers and other examples are illustrated along with detail sample code.

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
0

One possible first step is to make a String[][] from your ResultSet. I'm assuming that there'll be a maximum number of rows that you want to be able to display, so I've included that idea here. This is just to show the basic idea of how the iteration might work. Here, maximumRows, numberOfColumns and resultSet are already in scope and initialised.

String[][] data = new String[maximumRows][numberOfColumns];
for (int rowNum = 0; rowNum < maximumRows && resultSet.next(); rowNum++) {
    for (int columnNum = 0; columnNum < numberOfColumns; columnNum++ ) {
        Object value = resultSet.getObject(columnNum + 1);
        if (value != null) {
            data[rowNum][columnNum] = value.toString();
        }
    }
}

One key point here is the resultSet.next() on the second line. This is what causes the ResultSet to move from one row to the next. It also returns false if there is no more data.

Once you have your String[][], you can pass it to the constructor of DefaultTableModel, or even directly to JTable, depending on the requirements of your table.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110