JTable, JLabel and its icon (JLabel cannot be configured using the icon)?

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) { //This time return only the JLabel without icon return (JLabel) value; } else { return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); } } } 

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.

+4
source share
1 answer

It seems to me that you just want to leave all the cells in the table empty, except for those that the user clicked, where should the icon icon appear?

If so, your table model should not contain JLabel instances. JLabel is a visual component used to visualize some data graphically. The data itself is not JLabel. In your case, I think it should be logical (true when clicked by the user, false otherwise).

Now you can use your own renderer (although the default for booleans may also be OK) to display your boolean values. This renderer will be a subclass of DefaultTableCellRenderer , which itself is a subclass of JLabel. The renderer (the same instance for all cells configured to use this renderer) will simply display the marked icon if the Boolean value for rendering is a true and unmarked icon (or no icon at all) when the Boolean value for rendering is false.

Then your click handler will have only one mission: to make the cell with the click contain true, not false. To do this, you just need to change the corresponding value in the table model.

Repeat: a table model is used to store data. Think of it as the data you find in the database. Will you store JLabel in a database? No, you must have a boolean, a string or an integer. The table can display this data the way you want, and this is the mission of the visualization tool.

Side note: stop with the Hungarian notation: this doesn't make sense in Java. This makes it difficult to read the code. Everything except primitive types is an object, and you cannot assign a meaningful prefix to all possible types. Rather, use readable and meaningful English names: tableModel , not o_dtb , correctAnswer , not s_questAns , userAnswer , not s_userAns .

+4
source

All Articles