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 3 of 3

Thread: Highligh row in JTabel

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Highligh row in JTabel

    hi,

    I'm using JTabel and AbstractTableModel to show data from DB.
    At the first version I simply show the data, and in this case when I click single row, this row was highlighted (the whole row).
    But later I add some changes in the colors and font- I implemented the methods prepareRenderer and isCellSelected for those changes,
    and in this version when I click in the table only the cell selected.

    The questions are why exactly this change causing this different behavior?
    And how can I achieve, easily, the behavior for highlighting all the row?

    Any help will be appreciated.
    Attached Images Attached Images


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Highligh row in JTabel

    I wonder if you're setting the cellSelectionEnabled property to true. If false, via setCellSelectionEnabled(false), then the JTable should highlight the selected row, not the cell (as long as rowSelectionAllowed is also true which I believe it is by default).

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Highligh row in JTabel

    Hi,

    Currently I'm not changing those property at all.
    I try set them in all combination - TRUE/TRUE, TRUE/FALSE, FALSE/TRUE, FALSE/ FALSE, and doesn't seem to have effect.

    however if I comment the 2 method overridden - prepareRenderer and isCellSelected, then it is highlight the row.

    import java.awt.*;
    import java.util.ArrayList;
     
    import javax.swing.JTable;
    import javax.swing.table.TableCellRenderer;
     
    public class BaseAbstractTable extends JTable
    {
    	private BaseAbstractTableModel baseTableModel;
    	private ArrayList<Boolean> fillRow_Arr;
     
    	public BaseAbstractTable(BaseAbstractTableModel p_tableModel, ArrayList<Boolean> p_fillRow_Arr)
    	{
    		super(p_tableModel);
    		baseTableModel=p_tableModel;
    		fillRow_Arr=p_fillRow_Arr;
     
     
        	//setCellSelectionEnabled(false);
        	//setRowSelectionAllowed(true);
    	}
     
    	public Component prepareRenderer(TableCellRenderer renderer,int row, int col) 
    	{  
            Component comp = super.prepareRenderer(renderer, row, col);
            if(baseTableModel.getRowElement_Map().get(row)==null)
            {
                comp.setBackground(Color.LIGHT_GRAY);
                comp.setForeground(Color.BLUE);
                comp.setFont(new Font("HELVETICA", Font.BOLD, 16)); 
            }
            else if(fillRow_Arr!=null && !fillRow_Arr.get(row))
            {
            	comp.setBackground(Color.ORANGE);
            }
            else
            	comp.setBackground(Color.WHITE);
     
            return comp;
        }
     
    	public boolean isCellSelected(int row, int col)  
    	{
    		return true;
    	}
    }