Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default Colouring in JTable cells?

    Is there a simple few lines of code I can use to colour in cells of a JTable if I specify the cell location of the table? What I want to do is create an array containing 1's and 0's and then create a loop to go through the array and colour in the table, if the array value is 1 the cell should be red if 0 stay plain. The array values go to each cell sorta thing, 1st value or array = 1st cell, 2nd value of array = 2nd cell etc..... Is this possible in Java? Thanks.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Colouring in JTable cells?

    Moved from New to Java.

    Check out the section on using custom renderers in the tutorial linked from the JTable API.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default Re: Colouring in JTable cells?

    Looking at a few of them it seems to be quite difficult to colour in cells. I'm only really a beginner in Java so need to find the easiest way possible :/

  4. #4
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default Re: Colouring in JTable cells?

    Looked through a fair amount of examples and it seems ridiculously annoying to just background colour in certain cells, I have no idea how I'd apply that to certain cells based on array values! Surely there is an easier way?!

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Colouring in JTable cells?

    There is no free lunch. If you want to customize a JTable's rendering, you have to learn and understand renderers.

    What do you need this for? There may be a simpler approach using a GridLayout.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default Re: Colouring in JTable cells?

    Pretty much what I said in the original post, I was planning on having an array containing 1's and 0's then a table. Going to loop through the array if it's a 1 colour the cell red, if 0 leave blank. 1st array value = 1st cell, 2nd array value = 3nd cell and so on. Is there an easier way to accomplish this than using a table?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Colouring in JTable cells?

    If all you want to do is color cells in a grid, I would use a two dimensional array of JLabel with setOpaque(true) and setBackground(...) placed in a GridLayout.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default Re: Colouring in JTable cells?

    ahh that does make it a lot easier to colour in the cells, I didn't mention but I will have 3 tables of 3x3, 3x4 and 3x3 which will mean there will be 30 cells all together, does that mean seeing as the cells are labels that I'll need to create 30 labels or is that bad practice? Because at the moment I have to use the following code just to draw one cell so I'd have to do this 30 times to get what I want? do you know where I could read about looping through an array and changing labels through that?

    Java Code:
    setLayout(new GridLayout(3,3));
    
    JLabel label1 = new JLabel("label1");
    add(label1);
    label1.setOpaque(true);
    label1.setBorder(BorderFactory.createLineBorder(Color.black));
    Last edited by burgergetsbored; 04-13-2012 at 02:34 PM.

  9. #9
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default Re: Colouring in JTable cells?

    ah ha! Got that sorted now by using an array to draw out all the labels to the grids. The only thing that I can't get working is the label[x].setBorder(BorderFactory.createLineBorder(Color.bl ack)); the background colouring works fine but you can't seem to apply the border.

    Moved on a bit now but can't seem to call the method that holds the draw table for some reason can you see any problems with this code, it works fine when placed in the public JLabelDraw but wont do anything when placed in a method and called? :


    Java Code:
    public class JLabelDraw extends JFrame implements ActionListener
    {
    	Integer[] seats = {1,1,1,0,0,0,0,0,0,0,0,0,0};//ignore the first number, start from the 2nd.
    	int DAYS = 13;
    	JPanel mainContent = new JPanel(new GridLayout(4,3));
    
    public void drawTable()
    {
        int k = 0;
        Label[] labels = new Label[13];
    
        for (int x = 1; x < DAYS; x++)
            {
               // create Label with right alignment
               labels[x] = new Label("label" + x);
               //labels[x].setBorder(BorderFactory.createLineBorder(Color.black));
               mainContent.add(labels[x]);
               k = k+1;
    
               if(seats[k]==1)
               {
               labels[x].setBackground(Color.red);
               }
         }
    }
    
    
    public void actionPerformed(ActionEvent event) //button presses
    {
        drawTable();
    }
    }

  10. #10
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default Re: Colouring in JTable cells?

    anyone?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Colouring in JTable cells?

    Consider providing more detail -- enough so that we can understand the problem and be able to help you find an answer.

  12. #12
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default Re: Colouring in JTable cells?

    looking at the structure of the code does that seem correct so when the button is pressed the actionListener should start the drawTable method and do the code in that? Because at the moment it wont run that piece of code.Thanks

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Colouring in JTable cells?

    Looking at the code, there's no button.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default Re: Colouring in JTable cells?

    That's not all the code :L I probably shouldn't put it all on here as it's for an assignment. But the button's all linked up and works fine as I've changed the contents of drawTable method to system.out.printline and it printed fine to the command prompt window when the button was pressed. But for some reason that block of code above inside drawTable specifically wont run :S

  15. #15
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default Re: Colouring in JTable cells?

    do you think If I sent you the code you could have a look at it? It's probably a really easy solution but I just have no idea what it is and need it finished soon :/

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Colouring in JTable cells?

    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  17. #17
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default Re: Colouring in JTable cells?

    Right took out some pointless things that shouldn't really change anything, the problem is that I got the method calling al-right now. It's called first when the program is run to draw the initial tables which works fine, then it's called again upon the button press. The problem now is once the button is pressed for some reason nothing will happen until the frame is resized and when you do that it doubles what is on the page.
    Last edited by burgergetsbored; 04-16-2012 at 10:44 PM.

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Colouring in JTable cells?

    Quote Originally Posted by burgergetsbored View Post
    (sorry the forums attachment function isn't working.)
    Why do you need an attachment? If it's an SSCCE, it'll be short enough to post here. If it's too long tp post, that reduces your chance of getting help.

    Many members here, self included, won't click links or download files. Some can't, because corporate firewalls block most file sharing sites.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  19. #19
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default Re: Colouring in JTable cells?

    It's as short as I can get it without it causing problems when running just hought a link would have been easier.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.Arrays;
    
    public class Booking extends JFrame implements ActionListener{
    
    	//GLOBAL VARIABLES.
    	String custAge[] = {"Adult","Child","OAP"}; // String arrays for the contents of the combo boxes
    	String seatBlocks[] = {"Left","Middle","Right"};
    	String filmTimes[] = {"1","3","5","7"};
    
    	Integer[] Leftseats = {1,1,1,1,0,0,0,0,0,0,0,0,0};//ignore the first number, start from the 2nd.
    	Integer[] Rightseats = {1,1,1,0,0,0,0,0,1,0,0,0,0};//ignore the first number, start from the 2nd.
    	Integer[] Centerseats = {1,1,0,0,0,0,0,0,0,0,0,0,0};//ignore the first number, start from the 2nd.
    	int SEATS = 13;
    
    	JPanel leftBlock = new JPanel(new GridLayout(4,3)); //this will create the grid for left block seats
     	JPanel rightBlock = new JPanel(new GridLayout(4,3)); //this will create the grid for right block seats
     	JPanel centerBlock = new JPanel(new GridLayout(4,3)); //this will create the grid for center block seats
    
    	JPanel mainContent = new JPanel(new BorderLayout(30, 30));
    
    	public Booking(){
    
    	//create the main window
    	JFrame frame = new JFrame("Testing");
    	frame.setSize(800,800); //set the size of the main window
    	frame.setLayout(new BorderLayout(20,20));
    
    	JPanel optionPanel = new JPanel(new FlowLayout());
    
    	JButton btnSubmit = new JButton("Submit Order");
    	btnSubmit.addActionListener(this);
    
    	//add components to optionPane
    	optionPanel.add(btnSubmit);
    
    	frame.add(optionPanel, BorderLayout.NORTH);//add the option pane to the frame
    	frame.add(mainContent,BorderLayout.CENTER);
    	frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    	frame.setVisible(true);
    
    	updateSeating();
    	}
    	public void updateSeating()
    	{
    		//this class is going to be called often to read the global arrays and update seating plan.
    		Label[] labels = new Label[13]; //create an array of 13 labels
    
    		for (int x = 1; x < SEATS; x++) //for 1-13 times do this
    	     {
    
    	       labels[x] = new Label("seat" + x); //create a a label from the array and give it a value
    	       leftBlock.add(labels[x]); //add the label to the leftBlock panel
    
    	       if(Leftseats[x]==1)// if the value of x in array is 1 colour in the label red, if not ignore.
    	       {
    	      	 labels[x].setBackground(Color.red);
    	   	   }
         	 }
    
         	 //do the same for the right panel grid
    		 for (int x = 1; x < SEATS; x++) //for 1-13 times do this
    		 {
    
    		 	labels[x] = new Label("seat" + x); //create a a label from the array and give it a value
    		 	rightBlock.add(labels[x]); //add the label to the leftBlock panel
    
    		 	if(Rightseats[x]==1)// if the value of x in array is 1 colour in the label red, if not ignore.
    		 	{
    		 	    labels[x].setBackground(Color.red);
    		 	}
         	 }
    
         	//do the same for the center panel grid
    		for (int x = 1; x < SEATS; x++) //for 1-13 times do this
    		{
    
    			labels[x] = new Label("seat" + x); //create a a label from the array and give it a value
    			centerBlock.add(labels[x]); //add the label to the leftBlock panel
    
    			if(Centerseats[x]==1)// if the value of x in array is 1 colour in the label red, if not ignore.
    			{
    				 labels[x].setBackground(Color.red);
    			}
         	}
    
         	mainContent.add(leftBlock, BorderLayout.WEST);
    		mainContent.add(rightBlock, BorderLayout.EAST);
    		mainContent.add(centerBlock, BorderLayout.CENTER);
    	}
    
    	public void updateArray(){
    		//this is just for testing at the moment to see if using this method will change the array thus chaging seating plan
    		int p=5;
    		for (int y = 0;y < 3;y++ )
    		{
    			Leftseats[p]=1;
    			p = p+1;
    		}
    
    		System.out.println(Arrays.toString(Leftseats)); //to check they've changed
    	}
    
    	public void actionPerformed(ActionEvent event){
    	updateArray();
    	updateSeating();
    	}
    
    	public static void main(String args[]){
    		new Booking();
    	}
    }

  20. #20
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Colouring in JTable cells?

    when you do that it doubles what is on the page
    Isn't that rather obvious? The method adds DAYS labels. If that isn't desired behavior, don't add another set of components.

    nothing will happen until the frame is resized
    Whenever you add/remove components to/from an already shown container, you need to revalidate() the container, which tells it to re-layout its contents. In many cases, a repaint() is also necessary, so it's safer to always call repaint() after revalidate().

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Page 1 of 2 12 LastLast

Similar Threads

  1. Jtable cells not populating correctly
    By wlc in forum AWT / Swing
    Replies: 2
    Last Post: 04-04-2011, 05:57 AM
  2. Add datas to cells of JTable ???
    By bilgohan in forum AWT / Swing
    Replies: 5
    Last Post: 02-22-2010, 09:56 AM
  3. Coloring JTable cells
    By ProgrammingPup in forum Advanced Java
    Replies: 2
    Last Post: 11-04-2009, 10:57 PM
  4. How to merge cells of JTable
    By nehaa in forum AWT / Swing
    Replies: 1
    Last Post: 05-19-2009, 01:07 PM
  5. Getting objects from JTable Cells.
    By girigl in forum AWT / Swing
    Replies: 3
    Last Post: 12-24-2008, 10:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •