i have table documentfiles in which i have stored files path i'm retrieving the path from the database and then putting in JTable with cellRendering where i'm adding icon and text it is working fine but i'm facing problem with sort operation
here is my code
import java.awt.Component;
import java.awt.Dimension;
import java.io.File;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
public class GUI extends JPanel {
static JTable table;
static JPanel jPanel;
private static ArrayList<ArrayList<?>> cells = new ArrayList<ArrayList<?>>();
private static ArrayList<JLabel> columnNames =new ArrayList<JLabel>();
public GUI(){
columnNames.add(new JLabel("Name"));
columnNames.add(new JLabel("Type"));
columnNames.add(new JLabel("Size"));
columnNames.add(new JLabel("FilePath"));
table =new JTable();
table.setAutoCreateRowSorter(true);
jPanel=new JPanel();
new TableForm();
jPanel.setPreferredSize(new Dimension(300,300));
add(jPanel);
}
public static class TableForm {
public TableForm() {
try {
System.out.println(".......");
ResultSet rs;
rs=InsertDB.s.executeQuery(" select filepath from documentfiles ");
table.setModel(new ResultSetTableModel(rs, columnNames));
table.setDefaultRenderer(JLabel.class, new CellRenderer());
table.setAutoCreateRowSorter(true);
JScrollPane jsp=new JScrollPane(table);
jsp.revalidate();
jsp.repaint();
jPanel.add(jsp);
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void showTable() {
new TableForm();
}
}
public static class ResultSetTableModel implements TableModel{
private int rowCount, columnCount=4;
@Override
public int getRowCount() {
return rowCount;
}
public ResultSetTableModel(ResultSet resultSet, ArrayList<JLabel> titles) {
try {
ResultSetMetaData metaData = resultSet.getMetaData();
columnCount = metaData.getColumnCount();
cells = new ArrayList<ArrayList<?>>();
while (resultSet.next()) {
ArrayList<Object> row = new ArrayList(columnCount);
row.add(resultSet.getString(1));
cells.add(row);
}
rowCount = cells.size();
System.out.println(rowCount);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getColumnCount() {
return columnCount;
}
@Override
public String getColumnName(int columnIndex) {
return columnNames.get(columnIndex).getText();
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return JLabel.class;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return cells.get(rowIndex).get(columnIndex);
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
}
@Override
public void addTableModelListener(TableModelListener l) {
}
@Override
public void removeTableModelListener(TableModelListener l) {
}
}
public static class CellRenderer extends JLabel implements TableCellRenderer {
File f;
public CellRenderer() {
setOpaque(false);//MUST do this for background to show up.
}
public Component getTableCellRendererComponent(
JTable table, Object color,
boolean isSelected, boolean hasFocus,
int row, int column) {
f=new File(table.getModel().getValueAt(row, column).toString());
Icon icon= javax.swing.filechooser.FileSystemView.getFileSystemView().getSystemIcon(f);
setIcon(icon);
setText(f.getName());
setToolTipText(f.getName());
return this;
}
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new GUI();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]){
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
well i'm not able to find out where i'm going wrong, i don't have any idea how to get it done so i'm looking for some help from someone
Thanks in advance