Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging jtable cell color after jitem click
    text
    copied!<p>I am new on Java Swing. I have jtable which in column 2, when doing a right click on a cell a jpopmenu shows with two options(in progress / solved) and I want to modify cell color to green when clicking option "Solved". I know that I need a cell Renderer. How to put it?</p> <p>There's my source code:</p> <pre><code>public class logaff { static File font_file = new File("font/MYRIADPRO-REGULAR.ttf"); static JPopupMenu pm; static JMenuItem one = new JMenuItem(); static JMenuItem two = new JMenuItem(); static JTable table; public static void main(String[] args) throws FontFormatException, IOException { Font fontt = Font.createFont(Font.TRUETYPE_FONT, font_file); Font sizedFont = fontt.deriveFont(Font.PLAIN, 15); JPanel logaffich = new JPanel(); logaffich.setBackground(Color.black); logaffich.setLayout(null); JPanel tableau = new JPanel(); tableau.setLocation(5, 15); tableau.setSize(790, 520); tableau.setBackground(Color.white); JButton a = new JButton("fg"); String[] columnNames = { "ID", "Description", "Status", "Cam", "Elapsed Time" }; String[][] data = new String[10][5]; for (int i = 0; i &lt; data.length; i++) { for (int j = 0; j &lt; data[i].length; j++) { data[i][j] = "Table " + i + ", " + j; } } int[] columnsWidth = { 158, 450, 60, 40, 80 }; table = new JTable(data, columnNames); int k = 0; for (int width : columnsWidth) { TableColumn column = table.getColumnModel().getColumn(k++); column.setMinWidth(width); column.setMaxWidth(width); column.setPreferredWidth(width); table.setRowHeight(55); } JScrollPane scrollPane = new JScrollPane(table); table.setFillsViewportHeight(true); tableau.setLayout(new BorderLayout()); tableau.add(scrollPane, BorderLayout.CENTER); pm = new JPopupMenu(); pm.add(one); pm.add(two); table.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent me) { try { showPopup(me); } catch (FontFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); logaffich.add(tableau); TitledBorder title1; title1 = BorderFactory.createTitledBorder(null, "New Log Alert", TitledBorder.LEFT, TitledBorder.TOP, sizedFont, Color.white); logaffich.setBorder(title1); // panel pour le formulaire JFrame frame = new JFrame(); frame.setContentPane(logaffich); // Create and set up the content pane. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(808, 565); frame.setResizable(false); frame.setVisible(true); } private static void showPopup(MouseEvent me) throws FontFormatException, IOException { // is this event a popup trigger? if (pm.isPopupTrigger(me)) { Point p = me.getPoint(); final int row = table.rowAtPoint(p); final int col = table.columnAtPoint(p); // if we've clicked on a row in the second column if (row != -1 &amp;&amp; col == 1) { File font_file = new File("font/MYRIADPRO-REGULAR.ttf"); Font fontt = Font.createFont(Font.TRUETYPE_FONT,font_file); Font sizedFont = fontt.deriveFont(Font.BOLD, 17); final ImageIcon progress = new ImageIcon("images/progress.png"); one.setIcon(progress); one.setFont(sizedFont); final ImageIcon ok = new ImageIcon("images/ok.png"); two.setIcon(ok); two.setFont(sizedFont); one.setText("In progress " + row + "."); two.setText("Solved " + row + "."); pm.show(table, p.x, p.y); } one.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent me) { System.out.println("sfsdf" + row); } }); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload