Vertical Headers in JTable

Anybody ever done something like that ?
I need to display a very large grid, where the headers have a fairly long text, but each cell only has a single character in it, so I was thinking of making the headers vertical.
Any help, pointers would be appriciated.

Actually I am looking for a way to show text on an angle in the header :-)

Similar Messages

  • How can I add custom right-click-menu to column headers in JTable?

    Can anyone point me to a topic on how to customize a popup menu for column headers in JTable? Specifically, I want to add things like "auto-size column" and "hide column".
    Thanks,
    Matt

    Right-click on your table.  Then go to Advanced->Runtime Shortcut Menu->Edit.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines

  • How can I set column headers in JTable?

    I only want to set 3 column headers in JTable (without TabelModel). Can you help me?

    Hello dn77,
    Here's is a little sample I just rigged up, hope it helps.
    import java.awt.*;
    import javax.swing.*;
    public class TestOne{
         public static void main(String arg[]) {
              Object rows[][] = {
                   {"One", "1", "I"},
                   {"Two", "2", "II"},
                   {"Three", "3", "III"},
                   {"Four", "4", "IV"},
                   {"Five", "5", "V"},
                   {"Six", "6", "VI"},
                   {"Seven", "7", "VII"},
                   {"Eight", "8", "VIII"},
                   {"Nine", "9", "IX"},
                   {"Ten", "10", "X"},
                   {"Eleven", "11", "XI"},
                   {"Twelve", "12", "XII"},
                   {"Thirteen", "13", "XIII"},
                   {"Fourteen", "14", "XIV"},
                   {"Fifteen", "15", "XV"},
                   {"Sixteen", "16", "XVI"},
                   {"Seventeen", "17", "XVII"},
                   {"Eighteen", "18", "XVIII"},
                   {"Nineteen", "19", "XIX"},
                   {"Twenty", "20", "XX"}
              String headers[] = {"Notation", "Decimal", "Roman"};
              JFrame frame = new JFrame("3 Column header");
              JTable table = new JTable(rows, headers);
              JScrollPane scrollPane = new JScrollPane(table);
              frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
              frame.setSize(300,150);
              frame.setVisible(true);
    }-Merwyn,
    Developer Technical Support,
    http://www.sun.com/developers/support.

  • Vertical text in jTable headers

    Hi,
    I have got a jTable with quite a bit of columns in it. Is there any way I can write the text - vertically in the header (first) row.
    Please let me know if you have got any suggestions.
    Thanks.
    halosys.

    I remember reading about it, maybe in the next Java version.
    You can do it yourself by creating for the column headers a TableCellRenderer.

  • Vertical headers for columns in JTable

    Hi,
    I'm actually working on JTables and i'd like to set the headers of some columns to appear vertically instead of by default horizontal.
    I've found some tuts or examples to set the header on multiple lines but no one to set the header vertical...
    I want something like that :
    before :
    column1 | column2
    row1 |
    row2 |
    and after setting header columns vertical ( with column1 and column2 the column headers of my JTable )
    c | c
    o | o
    l | l
    u | u
    m | m
    n | n
    1 | 2
    ( and orientation of row headers not changed )
    Thanx for your reply
    Scottish

    Hi,
    Create you own colunm header renderer... it is actualy a cellRenderer. That cellRenderer should extends JLabel or JButton depending on what you want and you must put a paint method in it that draws the vertical text.
    JRG

  • Custom column headers for JTable in JScrollPane

    I want a heirachical header structure on a scrolled JTable. I've successfully generated a second JTableHeader which moves it's tabs with the normal header. If I add the secondary JTableHeader into the container above the whole scroll pane it's does almost what I want, but it's not quite correctly aligned.
    What I want to do is to put both the automaticaly generated JTableHeader and my extra one into the JScrollPane's column header area.
    I wrapped the two headers together into a vertical Box and tried calling the setColumnHeaderView() on the scrollpane, and then creating a JViewport and using setColumnHeader(). Niether seems to have any effect. The basic table header obstinately remains unaltered.
    There seems to be some special processing going on when JTable and JScrollPane get together, but I can't understand how replacing the column header viewport can be ineffective.

    Thanks. I think I've just cracked it more thoroughly, though. [I found this bug report|http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5032464]. This has guided me to a work-around that seems stable so far. I'm using an extended JTable class anyway (mostly to do with table header width behaviour). I've added a field to my own table class and the following override:
    The trick is to work out where the dirty deed is done, having search all the scroll pane related classes for special casing JTable.
    public class STable extends JTable {
        @Override
        protected void configureEnclosingScrollPane() {
            if (secondaryHeader == null) {
                super.configureEnclosingScrollPane();
            } else {
                Container p = getParent();
                if (p instanceof JViewport) {
                    Container gp = p.getParent();
                    if (gp instanceof JScrollPane) {
                        JScrollPane scrollPane = (JScrollPane) gp;
                        // Make certain we are the viewPort's view and not, for
                        // example, the rowHeaderView of the scrollPane -
                        // an implementor of fixed columns might do this.
                        JViewport viewport = scrollPane.getViewport();
                        if (viewport == null || viewport.getView() != this) {
                            return;
                        JPanel hdrs = new JPanel();
                        hdrs.setLayout(new BorderLayout());
                        hdrs.add(secondaryHeader.getHeader(), BorderLayout.NORTH);
                        hdrs.add(getTableHeader(), BorderLayout.SOUTH);
                        scrollPane.setColumnHeaderView(hdrs);
                        //  scrollPane.getViewport().setBackingStoreEnabled(true);
                        Border border = scrollPane.getBorder();
                        if (border == null || border instanceof UIResource) {
                            Border scrollPaneBorder =
                                    UIManager.getBorder("Table.scrollPaneBorder");
                            if (scrollPaneBorder != null) {
                                scrollPane.setBorder(scrollPaneBorder);
        }I'm hopeful that will prevent the column header view from being overwritten by later layout operations.

  • Vertical ScrollBar for JTable disappears when trying to scroll!

    Hi!
    I have 2 JTables and these are in a JScrollPane. I need to add a Vertical ScrollBar in such a way that when the number of rows increases, the scrollbar will scroll both the table's data.
    I am not using the Vertical ScrollBar of the ScrollPane instead i have added a JScrollBar to the two Panes( i.e. scrollPane.setVerticalScrollBar() ). This works fine.
    But when I remove the columns from the right JTable , i.e. I have only one JTable left, the Vertical scrollbar disappears inspite of the number of rows being the same.
    I have tried setting ViewPortView etc. but nothing works.
    Please help!

    Remember that was a test. It tells us the iPhoto is essentially sound and the issue is within that main Library.
    Next move is to try repair that library.
    Option 1
    Back Up and try rebuild the library: hold down the command and option (or alt) keys while launching iPhoto. Use the resulting dialogue to rebuild. Choose to Repair Database. If that doesn't help, then try again, this time using Rebuild Database.
    If that fails:
    Option 2
    Download iPhoto Library Manager and use its rebuild function. (In early versions of Library Manager it's the File -> Rebuild command. In later versions it's under the Library menu.)
    This will create an entirely new library. It will then copy (or try to) your photos and all the associated metadata and versions to this new Library, and arrange it as close as it can to what you had in the damaged Library. It does this based on information it finds in the iPhoto sharing mechanism - but that means that things not shared won't be there, so no slideshows, books or calendars, for instance - but it should get all your events, albums and keywords, faces and places back.
    Because this process creates an entirely new library and leaves your old one untouched, it is non-destructive, and if you're not happy with the results you can simply return to your old one.  
    Regards
    TD

  • JTextFields above column headers in JTable

    I am looking for a way to put a single row above (for filtering purposes) the table header in a JTable. Right now I am using JTextFields above my table for the filters, but as the columns are resized or moved so would the text fields have to be. So I am thinking putting a row above the table header would be appropriate.
    Any suggestions or examples in this area?

    Hello
    I've been working on something similar (filters above a JXTable) and i managed to get it working. So i'm putting it on this thread if someone find it with google :)
    The code for moving / resizing columns works fine, but the code for removing/adding columns doesn't work very well because it only allows one removed column at a time, or that you add the columns in the correct order.
    My solution is a bit different. I put a listener on the column property "visible", associated with the filter component above the column. That way, when i remove (hide) a column, the filter component removes itself from the panel, and when i reactivate the column, the component adds itself to the panel.
    Because the property change event is fired after the columnadded / columnmoved event, i have to use invokeLater thread to actually move the component at its right position.
    Here's the code for the moved/resized listener:
    import java.awt.Component;
    import java.awt.Dimension;
    import javax.swing.JPanel;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.TableColumnModelEvent;
    import javax.swing.event.TableColumnModelListener;
    import javax.swing.table.TableColumnModel;
    import fr.dga.gesma.catalyse.ihm.beans.FilterPanel;
    public class ExternalHeaderColumnModelListener implements TableColumnModelListener {
        private JPanel filterRow;
        private JTable table;
        public ExternalHeaderColumnModelListener(JPanel header, JTable table) {
            filterRow = header;
            this.table = table;
        public void columnAdded(TableColumnModelEvent e) {
        public void columnRemoved(TableColumnModelEvent e) {
        public void columnMarginChanged(ChangeEvent e)
            TableColumnModel tcm = table.getColumnModel();
            int columns = tcm.getColumnCount();
            for (int i = 0; i < columns; i ++)
                FilterPanel filtre = (FilterPanel)filterRow.getComponent( i );
                Dimension d = filtre.getPreferredSize();
                d.width = tcm.getColumn(i).getWidth();
                filtre.setPreferredSize( d );
            SwingUtilities.invokeLater(new Runnable()
                public void run()
                    filterRow.revalidate();
        public void columnMoved(TableColumnModelEvent e)
            final int toIndex = e.getToIndex();
            final int fromIndex = e.getFromIndex();
            // Delaying so the component exists when we move it
            SwingUtilities.invokeLater(new Runnable()
                public void run()
                    Component moved = filterRow.getComponent(fromIndex);
                    if (moved != null) {
                        filterRow.remove(fromIndex);
                        filterRow.add(moved, toIndex);
                        filterRow.validate();
        public void columnSelectionChanged(ListSelectionEvent e) {}and the code for the filter component (listener part) :
        private Container parent = null;
        // Taken from ColumnControlButton (JXTable component)
        // This listener is added to the TableColumn associated to this filter component
        protected PropertyChangeListener createPropertyChangeListener() {
            return new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent evt) {
                    if ("visible".equals(evt.getPropertyName())) {
                        updateFromColumnVisible((Boolean) evt.getNewValue());
         * Hides or shows the filter component. This
         * is called on change of tablecolumn's <code>visible</code> property.
         * @param visible column visible state to synch to.
        private void updateFromColumnVisible(boolean visible) {
            if (visible) {
                if (parent != null) {
                    parent.add(this);
                    parent.validate();
            } else {
                parent = this.getParent();
                if (parent != null) {
                    parent.remove(this);
                    parent.validate();
        }Hope this helps someone :)

  • Initial headers in JTable

    Hi
    I am having a problem using AbstractTableModel. The thing is that it is working perfectly for my application but I can get the header to appear with the initial names that I am setting. I am following some examples that I found on how to use this class, but in all of them the headers are declared at the begining in an String array;
    e.g. String[] headers= {"Last", "Name", "Sex"}
    But on the rest of the code I can't find where these values are actually set as headers for the table. When the table is displayed I get "A", "B", and "C" as the headers.
    Thanks in advance

    Once that you have your clumn names you must to put obn the Model.....
    String[] headers= {"Last", "Name", "Sex"}
           // Create a model of the data.
            TableModel dataModel = new AbstractTableModel() {
                public String getColumnName(int column) {return headers[column];} //NOTE THIS ROW...

  • JTable Multi Level Row Headers

    Hi All
    does any one know what's the better way to create MultiLevel Row Headers in JTable.
    for Ex.
    |- Hot
    |- Drinks--------|
    | |- Cold
    Food-------|
    | |- Soft
    | - Fruits-------|
    |- Hard
    and it goes on....to the n level.
    Is there any predefined classes available in Swing for this functionality.
    Thanks in Advance
    Kiran.R

    where did you find this ?
    http://www2.gol.com/users/tame/swing/examples/JTableExamples1.html
    That has some pretty hardcore examples!
    -jonathan

  • JTable  with headers extending JDialog

    I am creating a class file extending JDialog. I have populated the JTable on the form and want to include headers on the table. The dialog form opens and the table is visible and populated but with no headers. Has anyone had success with headers on JTables when creating a JDialog object?
    I have other tables created with headers so I wondered if the problem was with JDialog?

    inorder to make headers visible you need to add scroll pane to table,
    either by:
    JScrollPane scrollPane = JTable.createScrollPaneForTable(table);
    or:
    scrollPane = new JScrollPane(table);
    unless there's no scroller, you can't see headers.

  • Shading part of a JTable Cell dependent upon the value of the cell

    Hi
    Was hoping some one woudl be able to provide some help with this. I'm trying to create a renderer that will "shade" part of a JTable cell's background depending upon the value in the cell as a percentage (E.g. if the cell contains 0.25 then a quarter of the cell background will be shaded)
    What I've got so far is a renderer which will draw a rectangle whose width is the relevant percentage of the cell's width. (i.e. the width of the column) based on something similar I found in the forum but the part I'm struggling with is getting it to draw this rectangle in any cell other than the first cell. I've tried using .getCellRect(...) to get the x and y position of the cell to draw the rectangle but I still can't make it work.
    The code for my renderer as it stands is:
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JLabel;
    import javax.swing.JTable;
    import javax.swing.table.TableCellRenderer;
    public class PercentageRepresentationRenderer extends JLabel implements TableCellRenderer{
         double percentageValue;
         double rectWidth;
         double rectHeight;
         JTable table;
         int row;
         int column;
         int x;
         int y;
         public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
              if (value instanceof Number)
                   this.table = table;
                   this.row = row;
                   this.column = column;
                   Number numValue = (Number)value;
                   percentageValue = numValue.doubleValue();
                   rectHeight = table.getRowHeight(row);
                   rectWidth = percentageValue * table.getColumnModel().getColumn(column).getWidth();
              return this;
         public void paintComponent(Graphics g) {
            x = table.getCellRect(row, column, false).x;
            y = table.getCellRect(row, column, false).y;
              setOpaque(false);
            Graphics2D g2d = (Graphics2D)g;
            g2d.fillRect(x,y, new Double(rectWidth).intValue(), new Double(rectHeight).intValue());
            super.paintComponent(g);
    }and the following code produces a runnable example:
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    public class PercentageTestTable extends JFrame {
         public PercentageTestTable()
              Object[] columnNames = new Object[]{"A","B"};
              Object[][] tableData = new Object[][]{{0.25,0.5},{0.75,1.0}};
              DefaultTableModel testModel = new DefaultTableModel(tableData,columnNames);
              JTable test = new JTable(testModel);
              test.setDefaultRenderer(Object.class, new PercentageRepresentationRenderer());
              JScrollPane scroll = new JScrollPane();
              scroll.getViewport().add(test);
              add(scroll);
         public static void main(String[] args)
              PercentageTestTable testTable = new PercentageTestTable();
              testTable.pack();
              testTable.setVisible(true);
              testTable.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }If anyone could help or point me in the right direction, I'd appreciate it.
    Ruanae

    This is an example I published some while ago -
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    public class Fred120 extends JPanel
        static final Object[][] tableData =
            {1, new Double(10.0)},
            {2, new Double(20.0)},
            {3, new Double(50.0)},
            {4, new Double(10.0)},
            {5, new Double(95.0)},
            {6, new Double(60.0)},
        static final Object[] headers =
            "One",
            "Two",
        public Fred120() throws Exception
            super(new BorderLayout());
            final DefaultTableModel model = new DefaultTableModel(tableData, headers);
            final JTable table = new JTable(model);
            table.getColumnModel().getColumn(1).setCellRenderer( new LocalCellRenderer(120.0));
            add(table);
            add(table.getTableHeader(), BorderLayout.NORTH);
        public class LocalCellRenderer extends DefaultTableCellRenderer
            private double v = 0.0;
            private double maxV;
            private final JPanel renderer = new JPanel(new GridLayout(1,0))
                public void paintComponent(Graphics g)
                    super.paintComponent(g);
                    g.setColor(Color.CYAN);
                    int w = (int)(getWidth() * v / maxV + 0.5);
                    int h = getHeight();
                    g.fillRect(0, 0, w, h);
                    g.drawRect(0, 0, w, h);
            private LocalCellRenderer(double maxV)
                this.maxV = maxV;
                renderer.add(this);
                renderer.setOpaque(true);
                renderer.setBackground(Color.YELLOW);
                renderer.setBorder(null);
                setOpaque(false);
                setHorizontalAlignment(JLabel.CENTER);
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col)
                final JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
                if (value instanceof Double)
                    v = ((Double)value).doubleValue();
                return renderer;
        public static void main(String[] args) throws Exception
            final JFrame frame = new JFrame("Fred120");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setContentPane(new Fred120());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
    }

  • Problem with vertical text in crystal report viewer

    Hi,
    I am displaying vertical text in crystal report designer and deploying the report in crystal reports server 2008. The deployed report is being displayed using JAVA in crystal report viewer control.
    Problem: Vertical text header  is getting collapsed wth other vertical headers.
    Please help on this.
    Thanks,

    Hi Vijay,
    I found out another method of retaining all the chart formatting that is done in the preview pane.
    Once you have done all the changes then you will find interestingly there is a chart menu option that appears in the tool bar. Once you click on it there is an option "Apply Changes to All Charts".
    Select that option, close your preview. Open it once again and you will find that all the settings are still there and the Web Viewer is able to display them properly.
    I think that I resolved this.
    Regards,
    Gourav

  • JTable problem

    I'm writing an application that will display info in multiple tables. I thought it would be a good idea to write my own table class. The problem is that only the table data appears, but not the table headers.
    Layout.java creates an object of MyTable.java. The "createTable" constructor returns a JPanel wich in turn contains a JTable.
    Layout.java (creates the GUI, extends JFrame)
        String[] headers = {"1","2","3","4","5","6"};
        String[][] data = {{"one","two","three","four","five","six"},{"seven","eight","nine","ten","eleven","twelve"}};
        MyTable jt = new MyTable();
        Component table = jt.createTable(data, headers);
        JScrollPane jsp = new JScrollPane(table);
        getContentPane().add(jsp, BorderLayout.CENTER);
    MyTable.javapublic Component createTable(String[][] d, String[] h)
        data = d;
        headers = h;
        JPanel contentPane = new JPanel();
        TableModel tm = new AbstractTableModel(){
         public int getRowCount(){return data.length;}
         public int getColumnCount() {return headers.length;}
         public Object getValueAt(int r, int c) {return data[r][c];}
              public String getColumnName(int c) {return headers[c];}
        JTable jt = new JTable(tm);
        jt.setMaximumSize(new Dimension(700,40));
        jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        contentPane.add(jt);
        return contentPane;
    }

    The table headers are not really part of the table, they are a separate component.
    Normally a JTable is added directly to a JScrollPane. The JScrollPane is smart enough to take the column information create a component and add this component to the JScrollPane using the setColumnHeaderView() method of JScrollPane.
    In your example you are adding the JTable to a Component and then adding the Component to a JScrollPane so the headers don't get created.
    I suggest you add your table to the scrollpane in your createTable() method.

  • Using arrowbuttons in JTable

    When I use the arrowbuttons to move the "bar" (vertical) in my JTable, the ScrollPane doesn't scroll when I reach the top/bottom. The "bar" just keep moving out of sight and I have to use the mouse on the scrollbar. How do I fix that?

    ok, I was wrong. It's not when I use arrowbuttons. I've made two buttons, up and down, to move items in the table. The "bar" follows the "moving" row. This way a user can move the row out of view.
    So basicly what I need to know is how to scroll the ScrollPane manually.

Maybe you are looking for

  • I have encountered a peculiar problem in MM01 material master upload.

    I have encountered a peculiar problem in MM01 material master upload. The BDC program uploads data using Session method. The problem is when i process the session in foreground mode the data uploads successfully, but when i process it in background m

  • Dynamically passing the URL

    Hi In the receiver SOAP Adapter I got to send the Target URL ( Connection Parameters.....> Target URL) dynamically on the basis of URL that I get from payload. Is it possible to do this by using UDF ?? If so could you plz explain how we can do this ?

  • Java.rmi.RemoteException:  newServlets not found

    I am developing a java servlet using J2EE 1.4 RI. I have developed a new application called newServlets.ear. This application has a web component named Servlet1. When I click on Tools, Deploy I get the following error: stop newServlets running... jav

  • Best way to deploy PipelineComponents extending PipelineComponentSupport?

    Hi, we are having troubles trying to deploy some custom pipeline components that extend the PipelineComponentSupport class. The problems are related to the fact that when we deploy our pipeline components in our ejb jars, the classloader is unable to

  • Is it possible to install JAVA and ABAP trial ??????

    Hi i have read some posts in this forum which say that it is possivel  to install a full stack ABAP + JAVA with EP. But some of this are from 2006, and the versions that they installed are not the same anymore. <a href="https://www.sdn.sap.comhttp://