• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Invisible JTable Column Header!

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I create a table but cannot see the Column Headers.
I only see Data Rows. How can I see the headers?
My code:
jTable1 = new JTable();
.
.
String [] columnNames = {"xxx","xxx",xxx",....}
DefaultTableModel model = new DefaultTableModel(columnNames,numberOfRows);
jTable1.setModel( model);
ATTENTION : I don't want to use jTable(Object[],Object[]) Constructor.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this code takes the column names from the vector. u can have anu data container to hold the names of the headers. but all u must do is to implement the methods in the AbstractTable in a way suitable for ur datacontainer

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.DefaultCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class MyTableModel extends AbstractTableModel
{


Vector data = new Vector();
Vector dataContent= new Vector();
Vector columnNames= new Vector();
static int count=1;


public MyTableModel()
{

columnNames.add("Sl No:");
columnNames.add("Mem Code/ AC no:");
columnNames.add("Crew ID.");
columnNames.add("Amount");
columnNames.add("Delete");
dataContent.add(new Integer(count));
dataContent.add(new Integer(0));
dataContent.add(new Integer(0));
dataContent.add(new Double(0.00));
dataContent.add(new Boolean(false));

data.add(dataContent);

}

public int getColumnCount() {
return columnNames.size();
}

public int getRowCount()
{
return data.size();
}

public String getColumnName(int col)
{
return (String)columnNames.get(col);
}

public Object getValueAt(int row, int col)
{
Vector temp = (Vector)data.get(row);

return temp.get(col);
}

/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c)
{
return getValueAt(0, c).getClass();
}

/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col)
{
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col ==0)
return false;
else
return true;

}

/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col)
{


Vector temp =(Vector)data.get(row);
System.out.println(temp);
temp.removeElementAt(col);
temp.insertElementAt(value,col);
data.removeElementAt(row);
data.insertElementAt(temp,row);
fireTableCellUpdated(row, col);
/*
Deleting a row if the Boolean is true
*/
if(col==4)
{
count --;// decresing the count
printDebugData();

data.removeElementAt(row);
for (int i = row;i<data.size();i++)>
{
Vector temp1=new Vector((Vector)data.get(i));
System.out.println(temp1);
int tempcount=((Integer)temp1.get(0)).intValue();
tempcount --;
temp1.removeElementAt(0);
temp1.insertElementAt(new Integer(tempcount),0);
data.removeElementAt(i);
data.insertElementAt(temp1,i);
}
/*
To notify the model that the row has been deleted
*/
fireTableRowsDeleted(data.size(),data.size()+1);

}
printDebugData();
Value.setTotal(getTotal());
System.out.println("Total= "+getTotal());
}

public void addRow()
{
Vector newContent= new Vector();
count++;

newContent.add(new Integer(count));
newContent.add(new Integer(1));
newContent.add(new Integer(1));
newContent.add(new Double(0.00));
newContent.add(new Boolean (false));

data.add(newContent);
fireTableRowsInserted(data.size(),data.size());


}
private void printDebugData()
{
int numRows = getRowCount();
int numCols = getColumnCount();

for (int i=0; i < data.size(); i++)
{
Vector temp= (Vector)data.get(i);
for (int j=0; j < temp.size(); j++)
{
System.out.print(temp.get(j));
}
System.out.println();
System.out.println("--------------------------");
}
System.out.println("--------------------------");
System.out.println("the size is "+data.size());
}

public double getTotal()
{

/*
Calculates the total amount in the data vector
*/
double ret=0.00;
for (int i =0; i<data.size();i++ )>
{
Vector temp=(Vector)data.get(i);

ret=ret+((Double)temp.get(3)).doubleValue();
}
return ret;
}
}
 
Wink, wink, nudge, nudge, say no more, it's a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic