2 Replies - 10362 Views - Last Post: 06 October 2010 - 04:34 AM Rate Topic: -----

#1 xZachtmx  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 125
  • Joined: 14-June 09

Column names not showing in JTable

Posted 05 October 2010 - 07:24 PM

Hey Guys. I am trying to make a simple Jtable for school with column names but the column names do not show up. Here is my code:


	    String[] columnNames = {"Week",
	            "Beginning Weight",
	            "Loss",
	            "End Weight"};
	    
	    //Stores the data
	    String[][] data;

			//create the array and fill the table
			data = new String[w][4];
			for(int weekCount = 0; weekCount < w; weekCount++){
				data[weekCount][0] = Integer.toString(weekCount + 1);
				data[weekCount][1] = Double.toString(bw);
				//Calculate weight lost
				lw = bw * losrate;
				data[weekCount][2] = Double.toString(lw);
				
				//calculate the end of week weight
				ew = bw - lw;
				data[weekCount][3] = Double.toString(w);
				
				//set next week's beginning weight to this week's end weight
				bw = ew;
			}
			
			//populate the table
			//table model using data and column names
			DefaultTableModel model = new DefaultTableModel(data,columnNames);
			table = new JTable(model){
				//when checking if a cell is editable always return false
				public boolean isCellEditable(int rowIndex, int colIndex) {
			        return false;   //Disallow the editing of any cell
				}
			};
			table.setBounds(10, 36, 527, 214);  
			panel.add(table);



The data is showing correctly as it should but column names are no where to be seen. This is my first time with JTable using code snippets i found online with my formulas to populate data.

I hope you can understand and help!

Thanks so much!
Zach

Is This A Good Question/Topic? 0
  • +

Replies To: Column names not showing in JTable

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Column names not showing in JTable

Posted 05 October 2010 - 07:44 PM

You have to put your JTable in a JScrollPane for the header to automatically appears by magic

// table.setBounds(10, 36, 527, 214);
panel.add(new JScrollPane(table));

if not it is more complicated but feasable.. ask for it if you need so
Was This Post Helpful? 1
  • +
  • -

#3 xZachtmx  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 125
  • Joined: 14-June 09

Re: Column names not showing in JTable

Posted 06 October 2010 - 04:34 AM

Thank you so much pbl that worked! Now I have my headers. Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1