Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Extending DefaultTableCellRenderer issue

  1. #1
    Junior Member
    Join Date
    Sep 2020
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Extending DefaultTableCellRenderer issue

    I want to change the color of a row in my table and i also need to be able to change the color of that row back to the standard white color. I am invoking setRowColor() on my CustomTableCellRenderer-object and its working fine. however, after i have done a color-change on a row it is not possible to highlight that row anymore by dragging the mouse over the cells which is a problem. What am i missing? Thanks!

    edit: yes, sorry about that, im new here. anyway, here's the code for the CustomTableCellRenderer and a class with main() to run. Ive added some buttons to do the color changing and as you see the rows that have been exposed to color-change can no longer be highlighted if you drag your mouse over the cells.
    if(isSelected) {
    cell.setBackground(table.getSelectionBackground()) ;
    }
    is not working for some reason. Thank you for your help!


    CusomtTableCellRenderer.java :
    import java.awt.Color;
    import java.awt.Component;
    import java.util.HashMap;
    import java.util.Map;
     
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableCellRenderer;
     
    class CustomTableCellRenderer extends DefaultTableCellRenderer {
     
            private Map<Integer, Color> mapColors;
     
     
            public CustomTableCellRenderer() {
               mapColors = new HashMap<Integer, Color>();
            }
     
            public void setRowColor(int row, Color color) {
                mapColors.put(row, color);
     
            }
     
            @Override
            public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
     
            	Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, 1);
            	Color color = mapColors.get(row);
            	if (color != null  ) {
     
            	    cell.setBackground(color);
            	} else {
     
            	    if(isSelected) {
            	        cell.setBackground(table.getSelectionBackground());
     
     
     
            	    }
     
            	    else {
            	        cell.setBackground(table.getBackground());
     
     
            	    }
     
            	}
            	return cell;
     
            }
        }

    RunThis.java :
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
     
     
    public class RunThis extends JFrame implements ActionListener{
     
    	JTable table;
    	JPanel panel;
    	JScrollPane pane;
    	CustomTableCellRenderer cell;
    	JButton setRed, setWhite;
     
    	public RunThis() {
     
    		table = new JTable(50,2);
    		pane = new JScrollPane(table);
    		panel = new JPanel();
    		panel.add(pane);
    		add(panel, BorderLayout.CENTER);
     
     
    		setRed = new JButton("Set red color");
    		setRed.addActionListener(this);
    		setRed.setActionCommand("red");
    		add(setRed,BorderLayout.SOUTH);
     
    		setWhite = new JButton("Set white color");
    		setWhite.addActionListener(this);
    		setWhite.setActionCommand("white");
    		add(setWhite,BorderLayout.NORTH);
     
    		cell = new CustomTableCellRenderer();
     
     
    		for(int i=0; i<table.getColumnCount(); i++) {
    			table.getColumnModel().getColumn(i).setCellRenderer(cell);
    		}
     
     
     
    		pack();
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setVisible(true);
    	}
     
     
    	public static void main(String[] args) {
    		RunThis obj = new RunThis();
    	}
     
     
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		if(e.getActionCommand() == "red") {
    			for(int i=0; i<table.getRowCount(); i++) {
    				if(table.isRowSelected(i)) {
    					cell.setRowColor(i, Color.RED);
    		}
    			}
    		}
    		if(e.getActionCommand() == "white") {
    			for(int i=0; i<table.getRowCount(); i++) {
    				if(table.isRowSelected(i)) {
    					cell.setRowColor(i, Color.WHITE);
    		}
     
    	}
    		}
    	}
    }
    Last edited by lorekeeper; September 12th, 2020 at 11:35 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Extending DefaultTableCellRenderer issue

    Can you make a small complete program that compiles and executes and shows the problem?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Extending DefaultTableCellRenderer issue

    Sorry about not seeing the update. The site does not send me emails so I look at the last posts on the threads to see if there has been any activity. When an earlier post is updated, I don't see it.
    I'll look at this tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Extending DefaultTableCellRenderer issue

    Does the mouse selection action work as desired when cell color has been set? Or does it only work for cells that have not had their color changed? When you change the color, I don't know if JTable looks at the new color to see if it is the same as the default color. So if you set the color to white it should display the same as when the color is changed to red.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    lorekeeper (September 13th, 2020)

  6. #5
    Junior Member
    Join Date
    Sep 2020
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Extending DefaultTableCellRenderer issue

    yes thats what was the problem setting the cell color to white is not going to work because of exactly what you said. so to fix my problem all i had to do was to set the cell color to null.
    i just made an assumption and didnt even think of this...
    Thank you!

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Extending DefaultTableCellRenderer issue

    I'm glad it was an easy fix.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Extending JFrame
    By runkerr in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 5th, 2013, 09:54 PM
  2. Extending Jframe
    By runkerr in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 11th, 2013, 12:19 PM
  3. Reg:extending a jpanel
    By mohamed_mashood in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 23rd, 2013, 05:40 AM
  4. Extending class
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 8
    Last Post: January 10th, 2012, 05:06 PM
  5. class extending
    By Reem in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 18th, 2010, 01:49 AM