Arrow on JTables column header

Hey guys,
I have a few dialogs with tables on etc, everythings fine, its just one table when the program runs, when u hovver(sp?) over the line on the column header it normally turns into a black arrow like so <--> and the you can resize it.
But this one table wont do that, ive created these in netbeans and just cant see the difference with the other tables. the columns are still resizable just remains an normal arrow though rather than <-->
Thanks for any help

You can't resize the column if use the code below,but the arrow will display
TableColumn tableColumn = this.getTableHeader().getColumnModel().getColumn(i);
                         tableColumn.setPreferredWidth(columnsize);
                         tableColumn.setMinWidth(columnsize);
                         tableColumn.setMaxWidth(columnsize);reply if it help you..

Similar Messages

  • (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) );

  • 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>"

  • 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.

  • JTable Column Header Problem, Please help me

    What Listener I have to used to get the column index when user change the width of column header using mouse.

    There is no listener that reports this activity. The TableColumnModelListener can only tell you if columns change position or are added/removed from the column model.
    To detect changes, you will need to use a brute force mechanism, namely, to extend the JTableHeader class just a bit.
    When the user clicks on a header to resize a column, there is a property in JTableHeader called ResizingColumn that has a type of TableColumn. It is set by the UI delegate when the drag operation starts to the column being resized. When the drag operation ends, the value of ResizingColumn is set to null. You might start with something like this:
      JTable table = new JTable(...);
      MyTableHeader header = new MyTableHeader();
      table.setHeader(header);
      table.setModel(...);
    class MyTableHeader extends JTableHeader
        public void setResizingColumn(TableColumn column)
            super.setResizingColumn(column);
            if (column != null)
                System.out.println("Resizing Column #" + column.getModelIndex());
            else
                System.out.println("Resizing Ended");
    }This will only work for user resizing of columns, not for sizes changed programmatically or by the automatic sizing features of the header.
    Mitch Goldstein
    Author, Hardcore JFC (Cambridge Univ Press)
    [email protected]

  • How to have a combobox in JTable column header?

    Any simple ways for doing this? I am trying to get a filter to a JTable, so that only the rows containing the selected value in that column would be shown.
    I first need to get this combobox into the header of a column.

    Thanks.
    I have already tried with this way, but it was too long and complicated to implement into my file.
    Could someone post the principles here.
    Like this:
    // create my combobox
    JComboBox myBox = new JComboBox();
    myBox.addItem("Cat");
    myBox.addItem("Dog");
    // put the combobox to column header
    ??? ANY CODE HERE ???
    Tnx ahead!
    OK!
    AUlo

  • JTable column header to span multiple columns

    Hi people. I've spent the last day trying to do this with no luck. Most of the links in the forum are old and don't exist any more and the ones that lead to some code end up not working on java 5 (I can compile and run but the result is not what I wanted)!
    So my question is this, how do I have a column header that spans more than one column, which has individual column headers below it?
    Thanks, Paul.
    EDIT: I should point out that this is the place I've tried and although the screen shot of the first example looks exactly like what I want to do, it does not work as advertised. [http://www.crionics.com/products/opensource/faq/swing_ex/JTableExamples1.html]
    Edited by: Boomah on 05-Nov-2008 15:08

    Something like this?import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.util.Collections;
    import java.util.Enumeration;
    import java.util.List;
    import javax.swing.*;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.TableColumnModelEvent;
    import javax.swing.event.TableColumnModelListener;
    import javax.swing.table.*;
    public class MultiSpanHeader {
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
                new MultiSpanHeader().makeUI();
       public void makeUI() {
          final JTable table = new JTable(5, 6);
          final JTableHeader header = table.getTableHeader();
          header.setReorderingAllowed(false);
          final TableColumnModel model = table.getColumnModel();
          Enumeration<TableColumn> enumColumns = model.getColumns();
          final List<TableColumn> columns = Collections.list(enumColumns);
          final JTable dummy = new JTable(0, 3);
          final JTableHeader dummyHeader = dummy.getTableHeader();
          dummyHeader.setReorderingAllowed(false);
          dummyHeader.setResizingAllowed(false);
          final TableColumnModel dummyModel = dummy.getColumnModel();
          Enumeration<TableColumn> enumDummyColumns = dummyModel.getColumns();
          final List<TableColumn> dummyColumns = Collections.list(enumDummyColumns);
          model.addColumnModelListener(new TableColumnModelListener() {
             @Override
             public void columnAdded(TableColumnModelEvent e) {
             @Override
             public void columnRemoved(TableColumnModelEvent e) {
             @Override
             public void columnMoved(TableColumnModelEvent e) {
             @Override
             public void columnMarginChanged(ChangeEvent e) {
                dummyColumns.get(0).setWidth(columns.get(0).getWidth());
                dummyColumns.get(1).setWidth(columns.get(1).getWidth() +
                      columns.get(2).getWidth());
                dummyColumns.get(2).setWidth(columns.get(3).getWidth() +
                      columns.get(4).getWidth() + columns.get(5).getWidth());
             @Override
             public void columnSelectionChanged(ListSelectionEvent e) {
          JScrollPane pane = new JScrollPane(table);
          pane.setPreferredSize(new Dimension(600, 200));
          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.add(pane);
          frame.pack();
          JPanel panel = new JPanel(new GridLayout(2, 1));
          panel.add(dummyHeader);
          panel.add(header);
          pane.getColumnHeader().setView(panel);
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
    }Note: this is just presented as an idea, would need a lot of work to make it robust and flexible enough for practical use.
    db

  • JTable : column header with subheaders

    I need to create a table that looks like this:
    ======================
    Car....|...PRICE......
    .......|dollars.|euros
    ======================
    camry..|18K.....|17k...
    bmw....|80K.....|76K...
    metro..|22K.....|30K...
    (sorry for lousy "graphics")
    My quesion is about the two-layered "PRICE" column header that has two subheaders for two different currencies.
    What is the best way to approach this?

    So there's no standard feature for that (like in HTML)-- you basically have to implement it all yourself?

  • How to use JButton as a JTable column header?

    I'd like to use JButtons as my JTable's column headers.
    I built a JButton subclass which implemented TableCellRenderer and passed it to one of my table column's setHeaderRender method. The resulting header is a component that looks like a JButton but when clicked, it does not visually depress or fire any ActionEvents.

    You might want to check this example and use it accordingly for your requirements.
    http://www2.gol.com/users/tame/swing/examples/JTableExamples5.html
    Reason: The reason you're unable to perform actions on JButton is JTableHeader doesn't relay the mouse events to your JButtons.
    I hope this helps.
    have fun, ganesh.

  • JTable column header

    Hello,
    I have the following code:
              JPanel p = new JPanel (new BorderLayout());
              String[] c = {"Other Node", "Direction", "Annotation"};
              Object[][] d = {{"a", "b", "c"}};
              JTable t = new JTable(d, c);
              p.add(t);The problem is that I dont see the column headers (eg "Other node" etc), but I do see the column data, in this case: "a", "b", "c".
    Why I cant see they column headers?
    Thank you,

    See this...
    http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#simple
    It says if you are not using a scroll pane, you should get the table header using the getTableHeader() and add yourself to the container.
    Hope it helps
    thanks

  • JTable column header is not displaying

    Here I have the sample code which is not displaying the header names of each row (Names, Size, Status).
    import javax.swing.*;
    import javax.swing.table.*;
    import java.awt.*;
    public class sample extends JFrame
        JTable table;
        String names[] = {"Names","Size","Status"};
        TableModel model = new AbstractTableModel()
            public Object getValueAt(int row, int col)
                return "Data";
            public int getColumnCount()
                return 3;
            public int getRowCount()
                return 5;
            public String getColumnName(int col)
                return names[col];
        public sample()
            setSize(400,400);
            setDefaultCloseOperation(3);
            Container cp = getContentPane();
            table = new JTable(model);
            cp.add(table);
            setVisible(true);
        public static void main(String a[])
            new sample();
    }Anybody can give idea for me.

    Read the JTable API. It shows you how to do this. In addition it has a link to the tutorial on "How to Use Tables" which contains many examples.

  • 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

  • Wrapping table column header text

    Hi
    I want my jTable column header texts to be wrapped automatically when the text does not fit in the width of the column. Any one help me?
    thanks
    mortoza

    Hi
    Thanks for your good advise. I have checked the site and found double line header of table. BUT, those lines are static i.e. not wrapping rather fixed. I want it to be wrapped automatically when the width is smaller than the required width to display in on single line. Can anyone help?
    Mortoza

  • JTable - Check box in column header with sorting

    Hello
    I have 3 problems/questions.
    1. When adding sorting to a table with
    setAutoCreateRowSorter(true);and you click on a column header, normally a sorting arrow will appear, and indicate in what direction its sorting,
    but in the column head with the checkbox, even though the sorting works, the arrow will never appear. Is there anyway to fix this?
    2. Is there anyway to get the checkboxes in the table cells to be aligned left (I dont mean the columnheader checkbox, but the ones below)?
    3. When you click on the columnheader to either sort or check the checkbox, you will have to wait about a second between each click, or else it won't react and won't sort. sometimes you will have to click twice for it to react. If you click a lot of times fast, then it won't react until you stop for a second, and click again, though it will react only on the first click, but not the secont, third, fourh, and so on, until you stop for a second. Is there anyway to fix this, or atleast make it have a better reaction time?
    This is the code for creating the table:
    public class Example extends JFrame{
        private Container container;
        private JTable exampleTable;
        public Example(){
            container = getContentPane();
            ExamleTableModel examleTableModel = new ExamleTableModel();
            exampleTable = new JTable(examleTableModel);
            TableColumn tc = exampleTable.getColumnModel().getColumn(0);
            tc.setCellEditor(exampleTable.getDefaultEditor(Boolean.class)); 
            tc.setCellRenderer(exampleTable.getDefaultRenderer(Boolean.class)); 
            tc.setHeaderRenderer(new CheckBoxHeader(new MyItemListener()));
            container.add(new JScrollPane(exampleTable), BorderLayout.SOUTH);
            exampleTable.setAutoCreateRowSorter(true);
            setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );       
            this.pack();
            setSize(500,500);
            setVisible(true);
        }This is the code for the checkboxheader:
        class MyItemListener implements ItemListener 
                public void itemStateChanged(ItemEvent e) { 
                    Object source = e.getSource(); 
                    if (source instanceof AbstractButton == false) return; 
                    boolean checked = e.getStateChange() == ItemEvent.SELECTED; 
                    for(int a = 0, b = exampleTable.getRowCount(); a < b; a++) { 
                        for(int c = 0, d = exampleTable.getRowCount(); c < d; c++) { 
                            if(exampleTable.getColumnName(c).equals("")){
                                exampleTable.setValueAt(new Boolean(checked),a,c);
        class CheckBoxHeader extends JCheckBox 
            implements TableCellRenderer, MouseListener {
            protected CheckBoxHeader rendererComponent; 
            protected int column; 
            protected boolean mousePressed = false; 
            public CheckBoxHeader(ItemListener itemListener) { 
                rendererComponent = this; 
                this.setHorizontalAlignment(JLabel.LEFT);
                rendererComponent.addItemListener(itemListener); 
            public Component getTableCellRendererComponent( 
                    JTable table, Object value, 
                    boolean isSelected, boolean hasFocus, int row, int column) { 
                if (table != null) { 
                    JTableHeader header = table.getTableHeader(); 
                    if (header != null) { 
                        rendererComponent.setForeground(header.getForeground()); 
                        rendererComponent.setBackground(header.getBackground()); 
                        rendererComponent.setFont(header.getFont()); 
                        header.addMouseListener(rendererComponent); 
                setColumn(column); 
                rendererComponent.setText("");
                setBorder(UIManager.getBorder("TableHeader.cellBorder")); 
                return rendererComponent; 
            protected void setColumn(int column){this.column = column;} 
            public int getColumn(){return column;} 
            protected void handleClickEvent(MouseEvent e) { 
                if (mousePressed) { 
                    mousePressed=false; 
                    JTableHeader header = (JTableHeader)(e.getSource()); 
                    JTable tableView = header.getTable(); 
                    TableColumnModel columnModel = tableView.getColumnModel(); 
                    int viewColumn = columnModel.getColumnIndexAtX(e.getX()); 
                    int column = tableView.convertColumnIndexToModel(viewColumn); 
                    if (viewColumn == this.column && e.getClickCount() == 1 && column != -1) { 
                        doClick(); 
            public void mouseClicked(MouseEvent e) { 
                boolean active=true;
                JTableHeader header = (JTableHeader)(e.getSource()); 
                switch (e.getID()) {
                case MouseEvent.MOUSE_CLICKED:
                case MouseEvent.MOUSE_PRESSED:
                case MouseEvent.MOUSE_RELEASED:
                    JTable tableView = header.getTable();
                    TableColumnModel columnModel = tableView.getColumnModel();
                    int viewColumn = columnModel.getColumnIndexAtX(e.getX());
                    int column = tableView.convertColumnIndexToModel(viewColumn);
                    Rectangle bounds = tableView.getCellRect(-1, column, false);
                    if (e.getX() > bounds.x + 16) { // only if one the checkbox
                        if (e.getID() == MouseEvent.MOUSE_CLICKED) {active = false;}
                    }else{active=true;}
                if(active){
                    handleClickEvent(e); 
                    ((JTableHeader)e.getSource()).repaint();   
            public void mousePressed(MouseEvent e){mousePressed = true;} 
            public void mouseReleased(MouseEvent e){} 
            public void mouseEntered(MouseEvent e){} 
            public void mouseExited(MouseEvent e){} 
    }If you want the code for the ExamleTableModel, I will post it if you ask (having trouble with the character limit).

    What are you supposed to cast it to? I might have tried to cast it to something wrong.
    I tried
    TableCellRenderer tcr = table.getDefaultRenderer(Boolean.class);
    ((DefaultTableCellRenderer)tcr).setHorizontalAlignment(SwingConstants.LEFT);and that gave me a ClassCastException. But I might have been trying to cast it to the wrong thing.

  • How to display the sorting arrows in the table column header

    Hi
    I am doing a sorting for some columns from the click of the table column headers, In Developer studio, we are able to view the up and down arrows, I need that also to be made visible at run time, so that the user knows that there exists sorting based on that particular column. Is there any other way of doing it ? I cant place more than one control on the header or wrap the text to two lines..also. please clarify. thanks

    I assume you are using NW 04.
    The table column header contains a IWDCaption element which may display an icon and a text. See property IWDCaption.imageSource.
    The table tutorial might also be helpful.
    Armin

Maybe you are looking for

  • I can not open PDF's from Safari.

    I have already uninstalled Adobe Reader and made Preview my default reader again. When i open a PDF, i get back a black screen. Previously, i would get a black screen with message saying i needed to accept the License and Agreements of Adobe Reader.

  • Problem when upload file

    Hello everybody, i need to upload a file to a server and i use the following code: DiskFileUpload upload=new DiskFileUpload(); List items=upload.parseRequest(request); Iterator itr=items.iterator(); while(itr.hasNext()) { FileItem item=(FileItem)itr.

  • Search format with logical operators in TREX

    We've  activated the index for business object BUS1001006 for material master on SES_ADMIN. The index was created successfully and the two new search help was activated as well. Now testing it in development, I'm trying to figure out how you include

  • Intel iMac 17"s slow - even after SU and firmware update

    Just got 3 Intel iMacs (w/512MB RAM) to upgrade 3 users at my company, and have found basic startup (and later, opening apps) to be cripplingly slow. I have already: Run Software Update; Run the EFI Firmware update; Repaired permissions; Run Repair D

  • Exchange rate type mismatch in the invoice

    Hi experts, When I am creating an invoice, invoice is picking up wrong exchange rate type MTR which is not at all maintained in the customer details. The sold to party is having the exchange rate type TR2. Can anybody please help on the issue.?