What i’m trying to do it’s highlight the serial number that i scan from a box of items.
So when i scan the S/N. My idea is that the table, which contains the items in the box, change their color. My english sucks, so i think images can explain my idea in a better way.
So my code does the following:
First i load the table with the info from the box 1 (“Gaveta”):
Then when i filter the S/N of the item in the box:
i face my problem, the search paints the correct S/N but it damages my tablemodel.
My code:
Data search:
public void buscarNumeroSerie (String nserie) { try {
String [] campos={"NUMERO_SERIE","MARCA","GAVETA"}; String filtroNS = nserie; String NSSQL = "SELECT NUMERO_SERIE,MARCA,GAVETA FROM" + "(SELECT NUMERO_SERIE,MARCA,GAVETA FROM discos_wd " + "UNION " + "SELECT NUMERO_SERIE,MARCA,GAVETA FROM discos_toshiba " + "UNION " + "SELECT NUMERO_SERIE,MARCA,GAVETA FROM discos_seagate " + "UNION " + "SELECT NUMERO_SERIE,MARCA,GAVETA FROM discos_samsung " + "UNION " + "SELECT NUMERO_SERIE,MARCA,GAVETA FROM discos_hitachi )" + "AS TROUBLE WHERE NUMERO_SERIE LIKE '%"+filtroNS+"%'"; System.out.println(NSSQL); nsconn = metodosPool.dataSource.getConnection(); //ModeloTablaLista = new DefaultTableModel(null, campos); stmnt = nsconn.prepareStatement(NSSQL); ResultSet nsrs = stmnt.executeQuery(NSSQL); String [] nsfila = null; //if(nsrs.next()== true){ // String [] nsfila = new String[3]; while (nsrs.next()) { nsfila = new String[3]; nsfila[0]=nsrs.getString("Numero_Serie"); nsfila[1]=nsrs.getString("Marca"); nsfila[2]=nsrs.getString("Gaveta"); // ModeloTablaLista.addRow(nsfila); } if (nsfila == null) { Object[] opcionesPurga = {"Agregar Disco Hitachi", "Agregar Disco Toshiba", "Agregar Disco Seagate", "Agregar Disco Samsung", "Agregar Disco WD", "Omitir"}; int sinDiscoEnTabla = JOptionPane.showOptionDialog (rootPane, "Disco no encontrado, ¿que desea hacer?:", "Disco no encontrado", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, opcionesPurga, opcionesPurga[1]); System.out.println(sinDiscoEnTabla); switch (sinDiscoEnTabla){ case 0: sinDiscoAddHi.setVisible(true); break; case 1: sinDiscoAddTo.setVisible(true); break; case 2: sinDiscoAddSe.setVisible(true); break; case 3: sinDiscoAddHi.setVisible(true); break; case 4: sinDiscoAddWD.setVisible(true); break; case 5: JOptionPane.showMessageDialog(null,"Por favor ingrese un nuevo disco:"); tfNumeroSeriePurga.setText(""); break; } } nsrs.close(); stmnt.close(); // tablaDiscosGaveta.setModel(ModeloTablaLista); ClaseColor colorear = new ClaseColor(0); tablaDiscosGaveta.getColumnModel().getColumn(0).setCellRenderer(colorear); } //else // { // System.err.println("No existen datos asociados"); // JOptionPane.showMessageDialog(rootPane, "Disco no encontrado, quiere:"); // } catch (SQLException nseerr) { System.err.println(""+nseerr.getSQLState()); JOptionPane.showMessageDialog(null, "Error al buscar n" +nseerr, "Error en la operacion ", JOptionPane.ERROR_MESSAGE); } }
Table Renderer:
public class ClaseColor extends DefaultTableCellRenderer {
private final int patron; public ClaseColor(int patron) { this.patron = patron; } @Override public Component getTableCellRendererComponent (JTable tablaDiscosGaveta, Object value, boolean Selected, boolean hasFocus, int row, int col) { if (!tfNumeroSeriePurga.getText().equals(tablaDiscosGaveta.getValueAt(row, patron).toString())) { System.out.println("FramePurgar.PurgarDiscos1.ClaseColor.methodName()"+"no pille nada !"); } else { setForeground(Color.RED); //setFont(font); // super.getTableCellRendererComponent(tablaDiscosGaveta, value, Selected, hasFocus, row, col); return this; }
return this;
} }
What i’m trying to do is paint every S/N that matchs with the textField “Serial Number” which does the search.
Something like this:
so any new S/N i search, get a mark while i keep scanning S/N, and then the S/N that are not marked, are eliminated from the table.
Advertisement
Answer
Final update at this, i fixed my problem changing the value at the following line:
Component comp = super.getTableCellRendererComponent(tablaDiscosGaveta, col, rootPaneCheckingEnabled, rootPaneCheckingEnabled, row, col);
for
Component comp = super.getTableCellRendererComponent(tablaDiscosGaveta, value, rootPaneCheckingEnabled, rootPaneCheckingEnabled, row, col);