This summer is not a real summer, I thought. A cup of coffee makes me feel summer, tough (lol).
I have a slightly naughty JTable. OH MY GOD. Below is my JTable, which uses my own custom TableModel. You can see it using the getColumnClass () method, there ... it was created to return only JLabel . And then I also set up DefaultRenderer .
jtbl_inGameEasy = new javax.swing.JTable(); jtbl_inGameEasy.setFont(new java.awt.Font("squeaky chalk sound", 0, 14)); // NOI18N jtbl_inGameEasy.setForeground(new java.awt.Color(255, 255, 255)); jtbl_inGameEasy.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { }, new String [] { "null", "null", "null", "null", "null" } ) { boolean[] canEdit = new boolean [] { false, false, false, false, false }; public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit [columnIndex]; } public Class getColumnClass(int columnIndex) { return JLabel.class; } }); jtbl_inGameEasy.setDefaultRenderer(JLabel.class, new JLabelTableRenderer()); jtbl_inGameEasy.setFocusable(false); jtbl_inGameEasy.setOpaque(false); jtbl_inGameEasy.setRowHeight(55); jtbl_inGameEasy.setShowHorizontalLines(false); jtbl_inGameEasy.setShowVerticalLines(false); jtbl_inGameEasy.setTableHeader(null); jtbl_inGameEasy.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent evt) { jtbl_inGameEasyMousePressed(evt); } });
Where's the JTableRenderer ? Here ... this is my custom rendering below ...
public class JLabelTableRenderer extends DefaultTableCellRenderer { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (value instanceof JLabel) {
And then I need to put a JLabel inside each cell. I'm starting to add some JLabel (s) inside an array object over JTable cells using this code; (It's not a problem).
DefaultTableModel o_dtb = jtbl_inGameEasy.getModel(); o_dtb.addRow(myArrayCustomizedObjectofJLabel);
Everything works fine, I think. But because of my goal is that ---> To put the Icon icon or make it invisible as soon as the user clicks on the JTable Cells , so II tried to execute my MouseEvent once by clicking and calling these lines of code;
private void jtbl_inGameEasyMousePressed(java.awt.event.MouseEvent evt) { // Checking inGameEasy Table Ans javax.swing.JTable source = (javax.swing.JTable) evt.getSource(); int row = source.rowAtPoint(evt.getPoint()); int column = source.columnAtPoint(evt.getPoint()); DefaultTableModel o_dtb = (DefaultTableModel) jtbl_inGameEasy.getModel(); String s_questAns = "" + Game.getCurrentQuestion().getResult(); String s_userAns = "" + o_dtb.getValueAt(row, column); // These two lines below are not Working, why yaa?? ((JLabel) o_dtb.getValueAt(row, column)).setVisible(false); ((JLabel) o_dtb.getValueAt(row, column)).setIcon(myIcon); if (s_questAns.equals(s_userAns)) { Game.correct(); System.err.println("nice ans!"); } else { jll_txtMiss.setText("Miss : " + Game.wrong()); System.err.println("nope!"); } nextQuestion(); }
And it seems to me that Marked (below code with comments ) above does not work. Yes, JLabel cannot change its Icon (image), as well as its visibility. Is the model causing all this ? NB: My Cells data is added later after creating the model in different ways.