Changing default JTable column headings

Hi,
I've created a simple JTable using the following constructor:
JTable table = new JTable ( numRows, numCols);
and I would now like to change the default column headings ("A", "B", "C" etc) to something else. I'm trying to use the following method
setColumnIdentifiers
but how do I access the DefaultTableModel associated with table so that I can call this method?
Is there another way of changing the headings of my simple table?
Thanks a lot,
Leori.

myTable.getTableHeader().getColumnModel().getColumn(0).setHeaderValue("Foo");

Similar Messages

  • JTable column headings - how to change after instantiation

    Is there a way to change the column headings on a JTable AFTER is has already been instantiated? I reviewed the JTable, TableModel and TableColumn API's in detail. The only method I found related to this is the 'getColumnName' in TableModel. But there is no associated 'setColumnName'.

    Hi!
    You can use the method setHeaderValue(Object) of TableColumn. I wrote a small example:
    import javax.swing.*;
    import javax.swing.table.*;
    public class TableExample {
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            Object[][] data = {
                { "a1", "a2", "a3" },
                { "b1", "b2", "b3" },
                { "c1", "c2", "c3" },
            Object[] headers = {
                "1", "2", "3"
            JTable table = new JTable(data, headers);
            frame.getContentPane().add(new JScrollPane(table));
            frame.pack();
            frame.setVisible(true);
            //  Change header for first column
            TableColumnModel columnModel = table.getColumnModel();
            TableColumn col0 = columnModel.getColumn(0);
            col0.setHeaderValue("New header value");
    }Søren

  • JTable Column Headings

    While adding a Jtable without a scrollpane, the column headings are not displayed, why?

    Why does JTable doesn't automatically display the table header?Because its actually two components. One is the JTable and one is the JTableHeader.
    If you add the JTable to a scroll pane (the most common situation), there is code in the JTable class that checks if the table is being added to a scroll pane. The scroll pane has a well know layout so the header can be added safely to the column header of the scroll pane.
    However if you add the table to a panel you don't know what layout manager is being used is so you don't know where to add the header so it is left up to the programmer to customize the layout appropriately.

  • Change default program documentation headings

    Hi,
    When you create online documention for a program (SE38 > Documentation > Change), you get standard headings appearing automatically, like:
    &INTEGRATION&
    &PREREQUISITES&
    etc.
    Is it possible to change this pattern to get other headings to appear automatically? So for example creating new documentation displays:
    &INTEGRATION&
    &PREREQUISITES&
    &ZAUTHOR&
    etc.
    Thanks.

    Hi,
    better don´t do it. I tried it once and does not work easily since there´s a function which reads what sections (the ones among &) have text for displaying it in in a help window. If you create your own &section&, this will not be read by the function.
    The form for documentation is a SAPscript form and the texts are saved as standard texts:
    Text Name       your programme                  
    Language        EN                            
    Text ID         RE   Program/module pool      
    Text object     DOKU       Online Documentation
    Style           S_DOCUS1  
    Form            S_DOCU_SHOW
    So, there´s much to create as Z-development. Moreover, I haven´t found a place in customizing where to assign the new form to the ABAP editor.

  • JTable Column Headings not displaying

    Hi
    I have recently made a Jtable a node of a Jtree.
    My table shows (semi-correctly) and is editable.
    I can't seem to see the headings of each column. and the left hand border line of the table is missing.
    Anyone know why this is?? I presume that I cant resize my columns due to the fact I cant see the headings.
    Thanks
    mike

    If you don't want to put the table in a scrollpane you can put it on a JPanel too...but use the BorderLayout and add the table to the center and the tableheader to the North portion.

  • Change Default Finder Column Width

    Is it possible to change the default widths of the columns in Finder windows? I know that I can temporarily change it, and as long as I don't restart, all subsequent windows will open at this width. But is there a way to make the change the new system default?

    This may be it...
    http://www.macosxhints.com/article.php?story=20030329162110811
    I didn't have theNlisv, and can't reboot to test right now.
    If you do not have a Nlsv item, quit Property List Editor, go to the Finder and open a directory window, change View to "as List," select "Select View Options," click on "All windows," change one of the items shown, and close the View Options window. This will write to the Finder's com.apple.finder.plist file within a few seconds.

  • How to change JTable column header text

    How do you set the text in the JTable column headers? I know you can create a JTable specifying the text in an array:
    <li>JTable(Object[][] rowData, Object[] columnNames)
    But if you create the JTable specifying a TableModel,
    <li>JTable(TableModel dm)
    the header text defaults to "A", "B", "C", etc. I cannot figure out how to access the text in the header names so it can be changed to something useful. I know how to get the JTableHeader for the table, but it does not seem to have methods for actually setting header values.

    I'm sure that model allows you to specify header values so you don't have to do so manually. I would be very surprised if it didn't override the default getColumnName() method to provide a reasonable names.She wasn't writing the class, but [url http://forums.oracle.com/forums/thread.jspa?messageID=9200751#9200751]outlining a design for me to implement. And, based on a previous comment I had made, I think she assumed I wanted the new design to look as much like the old as possible. There were no headers in the original design, which wasn't even a table.
    Anyway, this works:
        final static String statisticsColumnNames[] = {
         "Type", "Count",
         "Red QE", "Green QE", "Blue QE", "Average QE",
         "Distance"
         qErrors = new QEBeanTableModel();
         JTable errorTable = new JTable(qErrors);
         TableColumnModel tcm = errorTable.getColumnModel();
         for (int col = 0; col < statisticsColumnNames.length; col++)
             tcm.getColumn(col).setHeaderValue(statisticsColumnNames[col]);
    It looks like setHeaderValue() on the TableColumn is what I was looking for.Again, only used if you are dynamically changing the values at run time or you don't like the defaults provided by the Bean-aware model.I coded the above before I read your last post. The QEBeanTableModel is extremely specific to my program. I.e. I cannot imagine it being used anywhere else. Would it still be better to implement a getColumnName() within the table model? Looking at your [url http://www.camick.com/java/source/RowTableModel.java]RowTableModel.java source, I can see that it would not be difficult to do so.
    Just decided to add the getColumnName() method. This whole sub-project is based on implementing a clean modern design (and learning about Java Beans). You've clearly stated twice that the method I have implemented is for dynamic header values only, which has already answered what I asked last paragraph.

  • Column Headings display in Uppercase as default

    Is there a way i can make a newly created workbook display all column headers in uppercase, by default. Currently the column heading are in lower case and i have to go and manually change all column headings into uppercase

    Well, you cannot do anything at the workbook level, that I am aware of (where want all caps for one workbook, but not all caps for another workbook).
    Let me do mention this. When you create a business area, you follow a load wizard to step you through the steps to create the business area. The last step (or one of the last steps - cannot quite remember the order) is where you name the business area and give it a description. Below the Description section is a section to control how you want object names to generate. There is a Capitalize option, with the default being to capitalize the first letter of every word. I think here you could say to capitalize every letter. Since the heading default for every item in a folder is the item name, by saying to capitalize every letter for business area objects, then you would end up with all caps for your workbook headings that use this business area. The downside is that this holds true for any workbook using the business area.
    For business areas that already exist, you can manually change the property, in Discoverer Administrator, for every item in the business area, one at a time. There is a heading property for each item. You would just type in the heading that you want to be the default for that item. Yes, this would be extremely tedious, but that option is available for you.
    Personally, I would leave the load wizard default as is and just change any headings that I want to change when I create a workbook. If someone really wants their headings all in caps, then I would do that for that workbook. For most people I think the default works fine. But if you are sure that you always want headings in caps, then make sure you pick the right option whenver you create a new business area.
    Does this make sense? Good luck.
    John Dickey

  • Mouse cursor doesnt change to splitter over JTable column header.

    Hi, this problem seems to be fixed in 1.6 but does anybody know a work around in 1.5 on Windows XP?
    I'm afraid that I haven't been able to locate the relevant bug fix (if any) despite my best efforts with Google and searching these forums.
    The problem is that in 1.5 if the parent frame window is invisible then the mouse cursor doesn't change to the column splitter\resize icon when you hover the mouse between two columns on a column headers.
    If you run the following code under 1.5.0.7, you don't get the cursor change, if you change f.setVisible(false) to f.setVisible(true) then it works.
    public class ColumnResizeTest
        public static void main(String[] args)
            JFrame f = new JFrame("This is a test");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setSize(400, 200);
            f.setVisible(false); // change to true to get resize mouse cursor
            test(f);
        public static void test(Frame f)
            JPanel contentPanel = new JPanel(new BorderLayout());
            JDialog d = new JDialog(f);
            d.setSize(200, 400);
            contentPanel.add(new JScrollPane(new JTable(new DefaultTableModel(5, 5))), BorderLayout.CENTER);
            d.setContentPane(contentPanel);
            d.setVisible(true);
    }In my application at the point the JTable is displayed, the application frame is always invisible as the JTable is shown from the splash\login screen.
    Thanks in advance.
    Philip Wilkinson.

    Adding a mouse listener to either a cell renderer or cell editor in a table the cell cursor will not work because it is the table which responds to mouse motion events - not the individual cell (at least in the case of MouseEntered and MouseExited).
    My solution to this was to add a MouseMotionListener listener to the table and and on the mouseMoved event I called a method which determined whether or not the cursor to display should be a hand cursor or the default cursor.
    For example:
    * Adds a mouse motion listener to the table.
    public void addTableMouseMotionListener()
        table.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseMoved(MouseEvent me) {
                displayColumnCursor(me);
    * Method to change cursor based on some arbitrary rule.
    protected void displayColumnCursor( MouseEvent me ) {
        Point p = me.getPoint();
        int column = columnAtPoint(p);
        int row = rowAtPoint(p);
        String columnName = getColumnName(column);
        if (columnName.equals("SOME_COLUMN")) {
            setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        } else {
            setCursor(Cursor.getDefaultCursor());
    }Hope this helps solve your problem.
    cheers
    Greg

  • Is it possible to change the column headings when ever i execute the query

    in the runtime i want to change the column headings is that is possible.
    Ex: if write a timestampadd(.......) function in the table heading is that change the dates automatically whenever i execute the query?
    is this is possible?
    Product name 1-2-10 5-6-10
    aaaa 5 6
    bbfff 6 10

    Product name 1-2-10 5-6-10
    aaaa 5 6
    bbfff 6 10It looks like the dates are values in a column? Put that column in the Columns section of the Pivot Table and Product Name in the Rows section and it should produce what you are looking for.
    Edited by: David_T on May 19, 2010 2:33 PM

  • How to change the particular column background  color in jTable?

    I am woking with a project, in which I am using a jTable. Then
    How to change the particular column background color in jTable?

    Use a custom Renderer. This is the JTable tutorial:
    http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

  • (simple q) how do I change the font in a JTable column header?  thanks

    how do I change the font in a JTable column header?
    thanks

    JTableHeader header = yourTable.getTableHeader();
    // Set to serif, bold, size 12...
    header.setFont( new java.awt.Font( "serif", 1, 12 ) );
    // you can set others like color and border, too.
    header.setBackground(Color.white);
    header.setBorder( new EmptyBorder(15,2,15,2) );

  • How to catch selected text in JTable Column

    Hi there,
    I am learning JTable. Need help for How to get the selected text from the JTable Column which is set to be editable.
    for example in JTextFiled you have method on getSelectedText(), is there any method for tracking the selected text.
    Thanks in advance
    Minal

    Here's an example of the model I used in my JTable. Not the "getValueAt" method & "getRecordAt" method. You will have to have a Record object - but it only contains the attributes of an inserted record (with appropriate getters & setters). Hope this helps.
    public class FileModel5 extends AbstractTableModel
    public boolean isEditable = false;
    protected static int NUM_COLUMNS = 3;
    // initialize number of rows to start out with ...
    protected static int START_NUM_ROWS = 0;
    protected int nextEmptyRow = 0;
    protected int numRows = 0;
    static final public String file = "File";
    static final public String mailName = "Mail Id";
    static final public String postName = "Post Office Id";
    static final public String columnNames[] = {"File", "Mail Id", "Post Office Id"};
    // List of data
    protected Vector data = null;
    public FileModel5()
    data = new Vector();
    public boolean isCellEditable(int rowIndex, int columnIndex)
    // The 2nd & 3rd column or Value field is editable
    if(isEditable)
    if(columnIndex > 0)
    return true;
    return false;
    * 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();
    * Retrieves number of columns
    public synchronized int getColumnCount()
    return NUM_COLUMNS;
    * Get a column name
    public String getColumnName(int col)
    return columnNames[col];
    * Retrieves number of records
    public synchronized int getRowCount()
    if (numRows < START_NUM_ROWS)
    return START_NUM_ROWS;
    else
    return numRows;
    * Returns cell information of a record at location row,column
    public synchronized Object getValueAt(int row, int column)
    try
    FileRecord5 p = (FileRecord5)data.elementAt(row);
    switch (column)
    case 0:
    return (String)p.file;
    case 1:
    return (String)p.mailName;
    case 2:
    return (String)p.postName;
    catch (Exception e)
    return "";
    public void setValueAt(Object aValue, int row, int column)
    FileRecord5 arow = (FileRecord5)data.elementAt(row);
    arow.setElementAt((String)aValue, column);
    fireTableCellUpdated(row, column);
    * Returns information of an entire record at location row
    public synchronized FileRecord5 getRecordAt(int row) throws Exception
    try
    return (FileRecord5)data.elementAt(row);
    catch (Exception e)
    throw new Exception("Record not found");
    * Used to add or update a record
    * @param tableRecord
    public synchronized void updateRecord(FileRecord5 tableRecord)
    String file = tableRecord.file;
    FileRecord5 p = null;
    int index = -1;
    boolean found = false;
    boolean addedRow = false;
    int i = 0;
    while (!found && (i < nextEmptyRow))
    p = (FileRecord5)data.elementAt(i);
    if (p.file.equals(file))
    found = true;
    index = i;
    } else
    i++;
    if (found)
    { //update
    data.setElementAt(tableRecord, index);
    else
    if (numRows <= nextEmptyRow)
    //add a row
    numRows++;
    addedRow = true;
    index = nextEmptyRow;
    data.addElement(tableRecord);
    //Notify listeners that the data changed.
    if (addedRow)
    nextEmptyRow++;
    fireTableRowsInserted(index, index);
    else
    fireTableRowsUpdated(index, index);
    * Used to delete a record
    public synchronized void deleteRecord(String file)
    FileRecord5 p = null;
    int index = -1;
    boolean found = false;
    int i = 0;
    while (!found && (i < nextEmptyRow))
    p = (FileRecord5)data.elementAt(i);
    if (p.file.equals(file))
    found = true;
    index = i;
    } else
    i++;
    if (found)
    data.removeElementAt(i);
    nextEmptyRow--;
    numRows--;
    fireTableRowsDeleted(START_NUM_ROWS, numRows);
    * Clears all records
    public synchronized void clear()
    int oldNumRows = numRows;
    numRows = START_NUM_ROWS;
    data.removeAllElements();
    nextEmptyRow = 0;
    if (oldNumRows > START_NUM_ROWS)
    fireTableRowsDeleted(START_NUM_ROWS, oldNumRows - 1);
    fireTableRowsUpdated(0, START_NUM_ROWS - 1);
    * Loads the values into the combo box within the table for mail id
    public void setUpMailColumn(JTable mapTable, ArrayList mailList)
    TableColumn col = mapTable.getColumnModel().getColumn(1);
    javax.swing.JComboBox comboMail = new javax.swing.JComboBox();
    int s = mailList.size();
    for(int i=0; i<s; i++)
    comboMail.addItem(mailList.get(i));
    col.setCellEditor(new DefaultCellEditor(comboMail));
    //Set up tool tips.
    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setToolTipText("Click for mail Id list");
    col.setCellRenderer(renderer);
    //Set up tool tip for the mailName column header.
    TableCellRenderer headerRenderer = col.getHeaderRenderer();
    if (headerRenderer instanceof DefaultTableCellRenderer)
    ((DefaultTableCellRenderer)headerRenderer).setToolTipText(
    "Click the Mail Id to see a list of choices");
    * Loads the values into the combo box within the table for post office id
    public void setUpPostColumn(JTable mapTable, ArrayList postList)
    TableColumn col = mapTable.getColumnModel().getColumn(2);
    javax.swing.JComboBox combo = new javax.swing.JComboBox();
    int s = postList.size();
    for(int i=0; i<s; i++)
    combo.addItem(postList.get(i));
    col.setCellEditor(new DefaultCellEditor(combo));
    //Set up tool tips.
    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setToolTipText("Click for post office Id list");
    col.setCellRenderer(renderer);
    //Set up tool tip for the mailName column header.
    TableCellRenderer headerRenderer = col.getHeaderRenderer();
    if (headerRenderer instanceof DefaultTableCellRenderer)
    ((DefaultTableCellRenderer)headerRenderer).setToolTipText(
    "Click the Post Office Id to see a list of choices");
    }

  • Almost invisible JTable column headers

    I'm writing an applet which contains a JTable within a JScrollPane. For a while the column headings (text) were visible, but recently the column headings have become very small in height. There is barely enough there so that the mouse can be used to sort, or resize or reorder the columns, but the text can not be seen. It doesn't seem to be related to the number of rows in the table. The getColumnName() method is being called and returning the correct strings, but they don't show up on the screen. As I said, this was working ok until recently, and I don't know what I changed that might have caused it.
    Any ideas? Thanks ... John Lawrence

    I am trying to have a JTable with variable height header. I give the headers a renderer that parses the text into a vector (breaking it into lines), and then returns a JList populated with the vector.
    ex: if the text is "Line1\nLine2", the header will have double height, with two lines:
    Line1
    Line2
    The problem is the same that you encountered: Swing only looks at the header of the 1st column to determine the height of all! So if there are headers with more lines than the 1st one, they get chopped!
    Does anybody know a workaround?

  • Multi-colored JTable column heading?  How?

    Hi,
    I'm looking for a way to have JTable column heading text
    that can be displayed in multiple colors, so one string
    in one column header, might have each letter a different
    color.
    My application requires highlighting some of the letters
    of a word as red, while others are normal text (black).
    I've been using multi-line strings to simulate vertical
    naming for my column header, for example:
    PPP
    I I I
    N N N
    1 2 3
    for PIN1, PIN2, PIN3. The column data has a binary
    one or zero for each pin, grouped together in one JTable
    column. In my application, if there is an error, I want to
    color just the text representing, say PIN2 red and leave
    the surrounding PIN1 and PIN3 black.
    What approach should I take for doing this with JTable?
    Thanks!
    John Roberts
    [email protected]

    You should override the default cell renderer used by Java
    - JTable.getTableHeader().setDefaultRenderer(yourRenderer)
    Extend yourRenderer from JLabel and use HTML tags to change the color.
    - setText on JLabel with the HTML code like "<HTML> <BODY> <FONT COLOR="red"> A </FONT> <FONT COLOR="green"> B </FONT></BODY> </HTML>"

Maybe you are looking for

  • Moving music from external hard drive to i-tunes

    Please I need some help with moving music on an external hard drive to my i-tunes library. I think that I have cut and pasted it to i-tunes from this hard drive but once the hard drive is disconnected...i-tunes can not find it. I would appreciate a s

  • Pse8 trial version & scanning b&w negatives

    Hello! I've just downloaded the trail version before buying: I wanted to test if I am able to use my Canon flatbed scanner & pse 8 to scan B&W negatives and then turn them into positives. I want to do this from time to time instead of using contact s

  • How to chnage number of bits in every packet of a byte array

    Hi I have an array of byte array of 1 and 0s. I would like to make sure every pack of 1 or 0s contains 8 number I can use this example to explain it better. Lets say I have this array 111111110000000111111100000000 As can be first pack of 0s and the

  • Forms runtime info. - URGENT-

    I have an application developed in Forms 6. I have the following questions. 1. Should the end user purchase forms 6 to run the application? 2. If no, can he get forms runtime along with Oracle 9i, which can run the application? 3. Is Forms 6 runtime

  • Stupidly Large Image Editing Problems

    So I got some 10x14's the other day of my dad riding his old interceptor which were decent looking with a lot of noise and print errors and decided to clean them up. Found a 6 year old photo scanner that scanned to 2400DPI, though that was over kill