JTable auto size columns/headers

How do I autoresize a JTable column/headers so that each column/header is the minium size necessary.
I've tried setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); and that doesn't
seem to work.
Thanks

You need to calculate the width of the text and set the preferred size of each column.
If seen solutions posted in the forum (but no I don't know the exact posting) so you can search the forum to find them.

Similar Messages

  • Cannot auto size columns in iTunes 10.4.  Columns are all mixed up with headings I have never seen. Cannot change by right clicking on columns.

    Cannot auto size columns in iTunes 10.4.  Columns are all mixed up with headings I have never seen. Cannot change by right clicking on columns. Cannot order albums alphabetically by double clicking on heading.

    Very many thanks to you, Putaro!  Your answer provided the solution. It was a buggy plug-in that turned out to be the culprit.
    You're off the hook, Apple, and my apologies for stating that you let a dog run loose. You skated by this time, Apple. But I'm watching you.
    But again, Putaro's reference to the plugins I had installed over the years turned out to illuminate the problem. I have several plugins in my iTunes plugins folder, pretty much all designed to add more visualizer functionality. I removed all of them, and ALL of the problems I noted in my post went away, with full iTunes functionality restored.
    I began adding the plugins back to hopefully single out the culprit. Among the ones I had been using included fielder, G-Force, Timestrech, volcanokit, QuartzComposer, and Jax. I hadn't used any of them in a while - probably at least a verison or more back in iTunes updates, but iTunes continued to work with all of them installed. Currently, only QuartzComposer continues to work in iTunes 10.4. The rest no longer even show up as visualizer opitons, with one exception. Jax does show up, but does not work. AND THE PRESENCE OF JAX IS WHAT KILLED MY ITUNES.
    I looked into it and went back to the developer, and it appears that they discontinued developing Jax some time ago - evidently, it was buggy and crash prone. I remember after buying it, having problems with it - having never worked to its touted potential, but I didn't pursue it, and never uninstalled it. Leaving it in there caused all iTunes functionality to cease with the iTunes 10.4 update.
    If you've got Jax installed, consider removing it before updating to iTunes 10.4.
    My iTunes is now working right as rain, thanks again to Putaro!
    -Jon

  • "Auto Size Column" feature dissappeared after installing iTunes 7.4.1.2

    I recently downloaded and installed iTunes 7.4.1.2, and the problem that I have now is when I'm playing/using my iPod while viewing it through iTunes. The problem is that the "Auto Size Column" feature is now missing whenever I right-click on any of the column headings. The "Auto Size All Columns" feature is present, but that does not work. When I click on "Auto Size All Columns", it doesn't do anything to the width of the columns. Also, I can not manually adjust the width of the columns by clicking between the columns (so that the cursor turns into the left & right arrows <->).
    The Auto Size Column feature works perfectly when I'm using the iTunes Library on my hard drive... it just doesn't work anymore when I'm working in the iTunes Devices on my iPod.
    Because of all of the glitches and problems that I've had with all of the iPod updates, I'm still running my iPod on Software Version 1.0. More than likely, this is the cause of my problem... I just wanted to find out if anyone else is having the same problem.
    p.s. I hope that I don't have to install the latest Software Version 1.2.1 for my iPod in order to fix this problem... Do I live without being able to adjust the width of the columns, or do I live without all of the glitches and freeze-ups?
    Message was edited by: phizyx

    same is the case with me. Earlier it was working fine. i have gone to the extent of not enabling access to outlook express so that it doesn't run on the computer still it is giving problems

  • Auto size Column

    I am using Oracle DiscovererBI 10g and I have a worksheet column that I am tring to resize. I have auto size column checked, but for some reason the width of the column is always the size of the header and not the size of the largest item in the column. If I go to print preview everything looks fine.
    Any ideas?

    Hi
    Take a look at the properties of the item in the Admin tool. One of the properties is default width. When you tell Discoverer to auto size it will resize the column to the bigger of the heading width or the data width, up to the default width defined in Admin, if set. Most of the time the default width is just fine, but for large data items it needs to be adjusted.
    Have a go and let me know if this works.
    Best wishes
    Michael

  • Auto size columns but with hidden columns

    I want to hide some columns for interal use with the following statements:
    jTable.getColumnModel().getColumn(i).setMaxWidth(0);
    jTable.getColumnModel().getColumn(i).setMinWidth(0);
    jTable.getColumnModel().getColumn(i).setPreferredWidth(0);
    Now I find that the widths of hidden columns become not zero.
    But I still want to auto-size the columns, so I add
    setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    Then I find that the widths of hidden columns become not zero.
    Could anyone tell me how to auto size the columns while keeping the hidden columns with zero widths?
    Thanks!

    If you want to hide columns then remove them from the TableColumnModel. The data is not removed, the columns are just not painted. Here is an example:
    http://forum.java.sun.com/thread.jsp?forum=31&thread=411506

  • JTable: Easy groupable column headers with hideable columns

    Hi All,
    I have a requirement to make a JTable which displays
    columns with 2 values - a current value and an old one.
    The old value can be hidden or shown by user preference.
    The values are not editable. The 2 values have a single
    heading.
    I've been mucking about with GroupableTableHeader and
    HideableTableColumnModel and all that. This way of
    solving the problem requires siginificant coding.
    Then I had an idea - just use the TableCellRenderer to
    make it look like 2 columns ! This fits my requirements
    and is much simpler.
    So for anyone who has the same requirement, here is
    some sample code. I think you'll all agree that in my
    specific case it is a very good way of providing the needed
    functionality.
    Here is the sample code:
    import javax.swing.*;
    import javax.swing.border.AbstractBorder;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.DefaultTableCellRenderer;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    public class DoubleColumnTable extends JTable {
        private boolean secondValueVisible = true;
        public DoubleColumnTable() {
            super();
            setModel(new AbstractTableModel() {
                public String getColumnName(int column) {
                    return column + "";
                public int getColumnCount() {
                    return 5;
                public int getRowCount() {
                    return 5;
                public Object getValueAt(int rowIndex, int columnIndex) {
                    return new Integer[]{new Integer(rowIndex), new Integer(columnIndex + 10)};
            setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                    Integer[] twoValues = (Integer[])value;
                    JPanel panel = new JPanel();
                    if (secondValueVisible) {
                        panel.setLayout(new GridLayout(0, 2));
                        JLabel firstLabel = new JLabel(twoValues[0].toString(), JLabel.CENTER);
                        JLabel secondLabel = new JLabel(twoValues[1].toString(), JLabel.CENTER);
                        secondLabel.setBackground(Color.lightGray);
                        secondLabel.setOpaque(true);
                        secondLabel.setBorder(new LeftBorder());
                        panel.add(firstLabel);
                        panel.add(secondLabel);
                    } else {
                        panel.setLayout(new GridLayout(0, 1));
                        JLabel firstLabel = new JLabel(twoValues[0].toString(), JLabel.CENTER);
                        panel.add(firstLabel);
                    return panel;
        private void toggle() {
            secondValueVisible = !secondValueVisible;
            repaint();
        class LeftBorder extends AbstractBorder {
            public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
                g.setColor(Color.gray);
                g.drawLine(x, y, x, height);
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setSize(300, 300);
            frame.setLocation(300, 200);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Container contentPane = frame.getContentPane();
            final DoubleColumnTable table = new DoubleColumnTable();
            contentPane.add("North", new JButton(new AbstractAction("Toggle") {
                public void actionPerformed(ActionEvent e) {
                    table.toggle();
            contentPane.add("Center", new JScrollPane(table));
            frame.setVisible(true);
    }Message was edited by:
    NY_Consultant
    Message was edited by:
    NY_Consultant
    Message was edited by:
    NY_Consultant

    This should read
    "I have a table with column headers and a row headers using a single column from a second table."

  • Can I Auto-Size Columns On Finder?

    I like to use "Finder" in the Columns style (3rd Button on top), however every time I launch finder it seems that I have to resize the columns in order to read it completly.  Is there a way to set it up so that it auto-sizes completly on it's own?

    There's another hidden trick.
    First the basics: If you double click the cursor on the line that separates two columns, it will resize the column to the left to fit its content.
    But if you hold down the Option key and then double-click any such line, it will resize all open columns to fit their respective contents.
    It still isn't true auto-resizing, but this does makes my life a lot easier! ;-)

  • Auto size column width in repor generation toolkit

    Im using RGT for excel creating tables i excel i wan to auto size the column width automatically.how to do this?
    kavi

    Basically everything you want to do needs to be done with ActiveX. The Report Generation Toolkit does just that. If there's something you need to do that the RGT doesn't do, then you need to use Excel's object model to call the properties and methods to do what you want. This information is in the Excel documentation, not the LabVIEW documentation. Examples ship with LabVIEW on how to use ActiveX to control Excel. There have also been tons of examples posted on this forum on various operations on Excel. Search and you will find them. There are also lots of examples in the Excel thread.
    Message Edited by smercurio_fc on 08-30-2009 09:18 AM

  • Always Auto Size Columns?

    Hi,
    I was wondering if it's possible to make iTunes always auto size the columns in every playlist, etc. It would be a really cool feature to implement if it's not already possible. If anybody knows how to do this, I'd love to know also.
    Thanks

    Well you can sort of do it manually. Each playlist should remember which columns are on, what order they are in, & what size each column is.
    To auto size a column you can place the cursor at the border between the two columns in the header so that the cursor shows a vertical line with two (or one in some cases) arrows coming out of the side. Double click with this and the column will resize to fit the longest content in the column). You can quickly do this for each column in a playlist.
    Patrick

  • Auto size columns

    Hi, I was clicking around iTunes and I was wondering what "Auto Size All Columns" meant (stupid, right?) Anyway, I clicked it, did not like it, and now I can't figure out how to get it back to normal. Is there any way to reverse this action?

    If you want to hide columns then remove them from the TableColumnModel. The data is not removed, the columns are just not painted. Here is an example:
    http://forum.java.sun.com/thread.jsp?forum=31&thread=411506

  • 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

  • Auto size of columns is not working after latest update of itunes 8.02

    columns will no longer auto resize correctly after the 8.02 update. the song title column hides song titles on request to resize very annoying please fix this Apple. any thoughts?

    SAME HERE!
    I just created a New List in my iTunes 8.0.2, and noticed the same Columns Width Issue, in List View and Cover Flow View:
    When I Controll-clicked on any Column, and chose Auto Size All Columns, it made the Name Column width more narrow then needed to display it's full content.
    I Controll-clicked on the Name Column, and chose Auto Size Column. It didn't make it wider to extend its width to the the width of the longest Name in that Playlist.
    I tried different Playlists, and another Library and it was the same issue!
    It did work in Music and Podcasts, but not in Videos and Audio Books. Unchecking Columns didn't help.
    Repairing Permissions, and Restarting iTunes and Restarting my Powerbook G4 didn't help either...
    I am sending this bug report to Apple
    http://www.apple.com/feedback/itunesapp.html
    and hope others will do also, if they encounter the same problem.
    And while at it, please join me in asking Apple to make Lyrics Field SEARCHABLE! What an underutilized opportunity that Lyrics Field is. I can store my notes in there, and have clickable web links, if they are in the same format as the one above, in this post. And while at it, make it Searchable on iPhones/iPods, or at least visible!
    Powerbook G4 17', 1.67, 10.4.11, 2GB RAM, all latest Software Updates

  • JTable without column headers?

    I want a JTable without the column headers.
    How can i do this?
    Cheers?

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Test extends JFrame {
        public Test() {
         String[] head = {"","",""};
         String[][] data = {{"0-0","0-1","0-2"},
                       {"1-0","1-1","1-2"},
                       {"2-0","2-1","2-2"}};
         JTable myTable = new JTable(data,head);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         Container content = getContentPane();
         content.add(new JScrollPane(myTable), BorderLayout.CENTER);
         setSize(200,200);
         show();
        public static void main(String[] args) { new Test(); }
    }

  • JTable column headers not displaying using custom table model

    Hi,
    I'm attempting to use a custom table model (by extending AbstractTableModel) to display the contents of a data set in a JTable. The table is displaying the data itself correctly but there are no column headers appearing. I have overridden getColumnName of the table model to return the correct header and have tried playing with the ColumnModel for the table but have not been able to get the headers to display (at all).
    Any ideas?
    Cheers

    Class PublicationTableModel:
    public class PublicationTableModel extends AbstractTableModel
        PublicationManager pubManager;
        /** Creates a new instance of PublicationTableModel */
        public PublicationTableModel(PublicationManager pm)
            super();
            pubManager = pm;
        public int getColumnCount()
            return GUISettings.getDisplayedFieldCount();
        public int getRowCount()
            return pubManager.getPublicationCount();
        public Class getColumnClass(int columnIndex)
            Object o = getValueAt(0, columnIndex);
            if (o != null) return o.getClass();
            return (new String()).getClass();
        public String getColumnName(int columnIndex)
            System.out.println("asked for column name "+columnIndex+" --> "+GUISettings.getColumnName(columnIndex));
            return GUISettings.getColumnName(columnIndex);
        public Publication getPublicationAt(int rowIndex)
            return pubManager.getPublicationAt(rowIndex);
        public Object getValueAt(int rowIndex, int columnIndex)
            Publication pub = (Publication)pubManager.getPublicationAt(rowIndex);
            String columnName = getColumnName(columnIndex);
            if (columnName.equals("Address"))
                if (pub instanceof Address) return ((Address)pub).getAddress();
                else return null;
            else if (columnName.equals("Annotation"))
                if (pub instanceof Annotation) return ((Annotation)pub).getAnnotation();
                else return null;
            etc
           else if (columnName.equals("Title"))
                return pub.getTitle();
            else if (columnName.equals("Key"))
                return pub.getKey();
            return null;
        public boolean isCellEditable(int rowIndex, int colIndex)
            return false;
        public void setValueAt(Object vValue, int rowIndex, int colIndex)
        }Class GUISettings:
    public class GUISettings {
        private static Vector fields = new Vector();
        private static Vector classes = new Vector();
        /** Creates a new instance of GUISettings */
        public GUISettings() {
        public static void setFields(Vector f)
            fields=f;
        public static int getDisplayedFieldCount()
            return fields.size();
        public static String getColumnName(int columnIndex)
            return (String)fields.elementAt(columnIndex);
        public static Vector getFields()
            return fields;
    }GUISettings.setFields has been called before table is displayed.
    Cheers,
    garsher

  • Autofit JTable column headers

    I have a problem. I am using eclipse. What i want to do is that in a Jtable, i want such a mechanism that no matter what the table headers are(any font, size), the jtable adjusts the column width to show the whole table header. The headers and the fonts are read from a file.
    presently i m doing this:
    //used to set the column widths
    FontMetrics metrics = tbl.getFontMetrics(tbl.getFont());
    // Set column widths
    int tempWidth;
    for (int i=0; i<columnNames.length ; i++)
    tempWidth = metrics.stringWidth(tbl.getColumnName(i))+ (2 * tbl.getColumnModel().getColumnMargin());
    tcm.getColumn(i).setPreferredWidth(tempWidth);
    tcm.getColumn(i).setPreferredWidth(columnNames.length()*7);
    The problem is that this does not work for all fonts.
    Can ne 1 plz provide sample code???

    Here's the full code I posted before to do this.
    It resizes the entire JTable to fit the column size, no matter what font properties, and
    optionally includes using the column header as well.
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=606240
    regards,
    Owen

Maybe you are looking for