Set JTable resize manual behaviour

I've been trying for the better part of the day to implement a manual resize behavior for table column widths but without success.
The JTable.setAutoResizeMode method that does this automatically has a couple of modes but not a weight mode (closest would be the one allocating new space equally to all columns, i.e. same weights).
What I want to do is to change the columns width according to preset weights when the table changes size. The reason behind this is that I want to set the columns to a specific width percentage (e.g. 80% 10% 10% 0%). But when the application is resized and subsequently the table the percentages gets skewed since the AutoResizeMode (if set to ALL_COLUMNS) will add surplus space equally to all columns.
My first approach was to implement a component listener that does just this:
public void componentResized(ComponentEvent e) {
     JTable table = (JTable) e.getComponent();
     TableColumnModel model = table.getColumnModel();
     double before = 0.0d;
     for(int i = 0; i < model.getColumnCount(); ++i)
          before += model.getColumn(i).getWidth();
     double delta = table.getSize().getWidth() - before;
     for(int i = 0; i < model.getColumnCount(); ++i) {
          TableColumn column = model.getColumn(i);
          column.setWidth((int) (column.getWidth() + columnWeights*delta));
... but the AutoResizeMode overrides this but if I turn off the AutoResizeMode then the table will not resize at all which means the component listener wont do a thing.
Any ideas as how to approach this problem would be appreciated!
/Maigo                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

Check out camickr's [Table Column Adjuster|http://tips4java.wordpress.com/2008/11/10/table-column-adjuster/]
db

Similar Messages

  • JTable resizing VS Horizontal scrollbar

    Heila',
    I have to show the content of a text file in a JTable; the JTable has 2 columns: the first contains the line number and the second column contains the text of the line.
    In order to allow the user to view and scroll the whole text line I added an horizontal scrollbar to the JTable.
    Here is the relevant code:
    gridVis.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    JPanGriglia = new JScrollPane(gridVis, JScrollPane.VERTICAL_SCROLLBAR_NEVER,
    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    JPanGriglia.getViewport().putClientProperty("EnableWindowBlit", Boolean.TRUE);     
    The problem is that if I set the AutoResizeMode to JTable.AUTO_RESIZE_OFF then I can see the horz scrollbar but when I resize the JFrame containing the JTable the JTable doesn't resize itself.
    Viceversa if I comment the statement:
    gridVis.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    then the JTable resizes correctly but the scrollbar disappears.
    Does exists the way to obtain both this features ?(correct resizing AND horizontal scroll bar)
    Thanks
    Ciao

    Try overriding getScrollableTracksViewportWidth() in JTable. The method returns a boolean value indicating whether the table should resize when its viewport is resized. If you setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS) and add a check to getScrollableTracksViewportWidth() such that it returns true only if viewport.getExtentSize().width is greater than some minimum width, then your table will resize automatically unless it is scaled below the minimum width, in which case a scrollbar will appear. If you're going to do this you'll need your JTable subclass to store a pointer to its scroll pane's viewport so it can get the extent size.

  • Set a "mobile" scroll behaviour to a scrollable List on a desktop app

    Hi,
    We're looking for some help on a Flex 4.5 AIR app deployed on a touch desktop environment.
    We're developping a kind of a kiosk app originally made for desktop machines (mouse). Recently we've tested it on a touch screen used with a stylus or fingers (not a mobile device but a touch screen connected to a PC), everything run smoothly without any changes as the touch events on the screen are interpretated as a mouse interaction by the driver.
    So everything works well but of course the UI is not designed to be manipulated with fingers and especially the behaviour of List scroll is not adapted : the desktop AIR app displays vertical scroll bars and buttons to scroll the list up and down but usually on a touch screen you scroll list by swiping it (touching an element and swiping up or left, the element is not selected because you've moved the finger before moving it up)
    I know that this behaviour for a Scroller exists in flex 4.5 within a "Mobile Project" (apparently it is possible to compile a "Mobile Project" into a desktop AIR app...?)
    So my questions are :
    1) Is it possible to set up this "mobile" behaviour of a scrollable List to a desktop AIR app ?
    2) Generally speaking, is it possible to set at the installation (by configuration or so) the "style" of the app (by "style" I mean the size and the behaviour of the components in order to be manipulated by a mouse or by a finger). It seems possible as the logic behind the component doesn't change, just the size (if it is a button and/or the bahaviour of the scroll if it is a List, and so on...). Is it something related to a Skin? or maybe a Theme?
    3) What the difference between a "desktop AIR app" and a "mobile app" in Flex Builder 4.5? (both project seems to have the same compiler's arguments, etc...)?
    Mich

    Hi there,
    Yes I use the mx datagrid because I need the drag and drop handling. The spark datagrid does not support this yet :-(
    So I can write my own drag and drop handlers for the spark datagrid or a touch scroll function for the mx version.
    I really think that componenents in Flex should be backwards compatible...
    Thanks

  • How do you add a video on i-pod after you set it to manual

    How do you add a video on i-pod after you set it to manual and not erase a ton of stuff in the process?
    Thanks

    after choosing the manually manage your songs you will not be able to add videos by clicking "update". all you going to do.. after converting videos compatible to ipod video, click your ipod on the left portion of the i tunes it will immediately show your songs from your ipod then drag the selected video from its folder to your i tunes just like adding songs to your ipod

  • Can links inside "set text of container" behaviours open in a new page?

    I am using the set text of container behaviour to show different information within the same page, to keep things neat and simple to navigate. Some of the text needs to be external links, but they open in the current page, not a new page even though I have set the target to _blank.
    Is there another way to keep using the set text of container and have the links open in another page?
    thanks!
    Using CS6 on Macbook Pro 2.6Ghz OSX10.8

    gryffun wrote:
    Well, after some further google searches, I'll answer my own question...
    selecting the link inside the set text of container and adding a behaviour to it -  "open in browser" will make the link open in a new window.
    Or ading target="_blank" to each link will do the same

  • Can I set the Fan manually..?

    I use BOINC and because of that my Thinkpad T410 crashes quite often!
    (Although I let BOINC only use one core!) Can I somehow set the fan manually to the maximum and set it back to normal when I stop BOINC?

    @mBurch,
    Just a suggestion ... since you leave the BOINC on when the laptop is idle, do elevate the laptop since the software is constantly running the proc up. By elevating the laptop you enable more air to get "pulled" into the base of the laptop thus better ventilation = cooler and happier laptop when you volunteer.
    Juz my 2 cents =)
    *Non Lenovo employee*
    I have a Y2P (i5) ... Feel free to ping me if you want me to test some applications with your Y2P if you have the same model. I don't mind keep doing recovery on it if needed .... =)

  • WRE54G v3 - I manage to set it up manually but it crashes when I add security

    I have attempted to set up the WRE54G via the cd but kept getting the "can not associate AP in repeater mode". I googled it and tried all the solutions suggested with no joy. It doesn't want to use the cd, I did it time after time changing things and nothing worked.
    So I decided to set it up manually. I used this link http://www.tomsguide.com/us/how-to,review-308.html to help me. It worked up to stage 9, when I attempted to set up WPA security like my router has. Once I set up the WPA security, I pressed ok on the window that popped up, and everything crashed. The window went to "can not find this page" - I had no confirmation that it had worked. I checked the wireless networks and no security came up on the network for the expander. Also the link LED stayed red on the actual wre54g.
    I am a bit of a novice at this kind of thing, but I managed it before with my linksys router, but now I've had to change it nothing works! It is causing me a lot of stress.
    My router is 192.168.0.1, its a sky sagem wireless router f@st 2420.
    I changed my WAN to 192.168.1.239 in order to see my expander. I then changed my expander to 192.168.0.40 so that my router and computers and expander can talk to each other!!
    Please help.
    Thanks for looking.

    You could have followed that too closely. If your router is 192.168.0.1, you should make the RE 192.168.0.240 (not 192.168.1.240 like in the instructions). You don't need to change the WAN to XXX.XXX.1.XXX.  
    Edit: OK, I read a little closer and RE IP addy should be OK. Doesn't have to be 240 but make sure it is unique. Also, since you have been changing your router settings, please try a power cycle of the whole network. Bring the router up first, wait for lights to stabilize, RE next, wait for steady blue link and flashing activity, then clients.
    Message Edited by Luckydog on 07-05-2008 08:48 AM
    Message Edited by Luckydog on 07-05-2008 09:08 AM
    Message Edited by Luckydog on 07-05-2008 09:11 AM

  • Set JTable in another JTable Column

    Hai Java Experts
    Can we Set JTable in another JTable Column
    In jsp we add one table in a column of another table. Like the previous way can we do it in swing?
    Pls Help me with example..
    Thanks

    [http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editrender]
    db
    edit And adding a table in a column of another table doesn't make any sense at all. Perhaps you meant showing a table in a cell of another table.
    Edited by: DarrylBurke

  • Unable to set the apn manually about the hotspot iphone4 with ios7.1

    Hello, I am writing in a forum and all the people who have updated their iphone4 ios7.1 to have the same problem, that it is impossible to set the apn manually.
    Here in Italy with the carrier (wind), we have tried all kinds of recovery, and also to contact the carrier says that of course is not at fault, because his apn is always the same.
    The solution is what? The only one I found is to use my old nokia and abandon my iphone4 with the hope that apple proceed to fix the problem ...............

    When I insert the data apn and go back the data are not saved and therefore I can not use it as a hotspot, but if I insert the data to the cellular data network apn and mms, they remain saved and then I have no problem.
    Only apn hotspots have this bug.
    From what I understand happens with iphone4.

  • How to set JTable column's color?

    How can I set JTable Columns' color? I only found this class DefaultTableCellRenderer
    which can set cell's color.

    rmalina wrote:
    You are going to need to derive a renderer class for your Column from DefaultTableCellRenderer and override the following function with something like this:
    @Override
         public Component getTableCellRendererComponent(JTable jTable, Object oValue, boolean isSelected, boolean hasFocus, int nRow, int nColumn) {
    super.setForeground(Color.GREEN);
    super.setBackground(Color.GREEN);
    }That would set your column to green.
    Edited by: rmalina on Jul 28, 2008 8:47 AMHow can I know I only change the columns' color instead of other cells?

  • Setting JTable cell editor

    Hallo,
    I have troubles setting jtable cell edtior. when i run code below, editor stays unchnaged.
    Do you know where's the problem, please (except in programmer^_^) ???
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    public class Test extends JFrame {
        JTable table;
        Test() {
            // Create table model
            table = new JTable();
            table.setModel(new DefaultTableModel(
                new Object[][] {
                    { Boolean.TRUE, Integer.valueOf(10) },
                    { "Hallo!", Boolean.FALSE },
                }, new String[] { "Col1", "Col2" }));
            // Setup frame a little
            setLayout(new BorderLayout());
            setBounds(new Rectangle(300,300,200,100));
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            add(table,BorderLayout.CENTER);
             * Two calls below have no effect:-(
             * What did i wrong?
            // set cell editor for all cells
            table.setCellEditor(new DefaultCellEditor(
                    new JComboBox(new String[] { "0", "1" }) ));
            // Use checkbox for booleans
            table.setDefaultEditor(Boolean.class, new DefaultCellEditor(
                    new JCheckBox() ));
            // Use combobox for strings
            table.setDefaultEditor(String.class, new DefaultCellEditor(
                    new JComboBox( new String[] { "Hallo!", "Bye!" } ) ));
        public static void main(String[] arg) {
            Test test = new Test();
            test.setVisible(true);
    }

    Hi again,
    yes it works when i set it for single column, but i'd like to use default editor, because column count is not fixed.
    According to documentation: "Sets a default cell editor to be used if no editor has been set in a TableColumn. If no editing is required in a table, or a particular column in a table, uses the isCellEditable method in the TableModel interface to ensure that this JTable will not start an editor in these columns. If editor is null, removes the default editor for this column class."
    So when i call
    column.setCellEditor(null); in TableColumnModelListener columnAdded event. But still it's not working. See sample below (i've changed it to cell renderer coz result is clear on single view, but problem stays same)
    More over problem seems to be with algoritm that decide which class to use. Object takes preference any time. W/o it npe arries.
    Regards
    Adam
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.DefaultTableModel;
    public class Test extends JFrame {
        JTable table;
        Test() {
            // Create table model
            table = new JTable();
            table.setModel(new DefaultTableModel(
                new Object[][] {
                    { Boolean.TRUE, Integer.valueOf(10) },
                    { "Hallo!", Boolean.FALSE },
                }, new String[] { "Col1", "Col2" }));
            // Setup frame a little
            setLayout(new BorderLayout());
            setBounds(new Rectangle(300,300,200,100));
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            add(table,BorderLayout.CENTER);
            table.setDefaultRenderer(Object.class,new Renderer(Color.YELLOW));
            table.setDefaultRenderer(String.class,new Renderer(Color.BLUE));
            table.setDefaultRenderer(Boolean.class,new Renderer(Color.RED));
            table.setDefaultRenderer(Integer.class,new Renderer(Color.GREEN));
            // use default renderer in all columns
            table.getColumn(table.getColumnName(0)).setCellRenderer(null);
            table.getColumn(table.getColumnName(0)).setCellRenderer(null);       
        public static void main(String[] arg) {
            Test test = new Test();
            test.setVisible(true);
        class Renderer extends DefaultTableCellRenderer {
            Color color;
            Renderer(Color color) { this.color = color; }
            public Component getTableCellRendererComponent(JTable table, Object value,
                    boolean isSelected, boolean hasFocus, int row, int column) {
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                setBackground(color);
                setToolTipText("Class is "+value.getClass().getName());
                return this;
    }Message was edited by:
    a3cchan

  • HT1202 unable to set iPod to manually sync

    When I connect my iPod nano to my pc, I get prompted to restore it.  However, I do not wish to restore the iPod because I use another computer with it, also.
    I located an Apple help file that guided me to the Summary tab within iTunes.  When I select the Summary tab for my iPod nano to set it to manually sync music, nothing happens.  I am operating iTunes 11.0.2.25.
    Please advise.

    Interesting article on ilounge about this issue;
    http://www.ilounge.com/index.php/news/comments/apple-locks-tv-out-in-new-ipods-b reaks-video-add-ons/11371
    What means are you using to connect the iPod to the video display?
    Cheers!
    -Bryan

  • How Do You Set Mail To 'Manual' ?

    Never had problem with iPhone until yesterday. I am not receiving 'push' e-mail anymore. I did not do any changes to the iPhone at all. Any e-mail now I have to go to inbox and click on the curved arrow symbol at bottom left of page in order to load the waiting e-mails. In a different posting someone mentioned to test 'push' function by setting e-mail to 'manual' and then sending your Yahoo account an e-mail from your computer (I assume using a different e-mail service). In the 'settings' menu for 'mail' I do not see where you set for 'manual'. How do you set mail to 'manual' setting? And do you think I should just RESET the iPhone (using the iPhone or hooking it up to iTunes on the computer to RESET) ?

    I have no other e-mail accounts set up. Anyway I did RESET my iPhone (using the RESET button on the iPhone) and all is well again (how long I do not know). My e-mails are being 'pushed' again so that I can see from the main menu the little red dots (with the numerals inside) showing how many e-mails are waiting for me. I love this feature. I had 'push' e-mail on my ex-Blackberry. BUT, I do not see the AUTO-CHECK anywhere. I am heading over to the Apple Techies at my local Apple store to ask them about this missing AUTO-CHECK. I have a feeling you need two account set up on your iPhone for this feature to appear. I could be wrong. Thanks, everybody........

  • Job msg 608: job set to cancelled manually

    hi  all,
    there is dtp from dso to cube(full load) which has failed with job log  : job set to cancelled manually and the msg 608.
    i couldnt find any locks, short dump etc.This is background job and there is no info. in sm21.
    when i try to debug (from dtp monitor screen) the data  is fine.there was a kernel update .
    what can be the reason and how to fix it?
    will the bg  job  info  be seen in sm21?(as i dont see any info abt the background job in sm21)
    Edited by: jumboash on Nov 24, 2009 6:48 AM

    Hi,
    check in Transaction SM36 and let me know the details
    Regards,
    Marasa.

  • P6N SLI Platinum- Cannot set Memory Speed -Manual/Linked

    It doesn't seem to matter whether I have the memory set to Linked or Manual- I get the Linked setting in any OC setup.  ??? At stock AUTO, it puts it at the rated 800mhz (pc6400), though.
    So for a 1520FSB setting (=380), it sets the memory to 760 in both the Linked setting and the Manual setting.  Sounds like something in the bios, but I have tried the 1.0, 1.1, and 1.22b bioses with the same result! 
    Unfortunately, I recorded my OC settings but not the output on my first numerous attempts. But I want to say that originally, when I started the OC's, when I set the memory to Manual it ended up being Linked. And vice versa. So that at 1400FSB (350) --Linked, it gave me the 800 Memory setting and at 1400 (350) --Manual, it gave me 700 on the memory. But I'm not positive now (about a hundred OC settings later --and yes now I've been writing it down) that it ever worked right.
    Any help or thoughts? Anyone else with this problem? Any fixes?
     

    Quote from: Maesus on 02-May-07, 22:57:14
    MSI BIOS is not flexible in overclocking or changing DDR RAM Timing settings. Please live with it and trial and error with the RAM timing setting that yields the best result.
    I'm just supposed to accept the fact that you cannot set the ram to a manual timing, that it will always run as linked..... then why have the option to select it in the bios if it's not going to be functional. That's just unacceptable!  live with it......right ! That's shoddy at best.! 
    Didn't have this problem with any Asus board (which has been several) or Abit board that I've owned! 
    And why is noone reporting this same behavior from their board and bios in this forum or over in hardforum or in the anandtech threads????

Maybe you are looking for

  • Actual cost of production order in inventory valuation

    Hi, Kindly tell me in our client requirement to "capture actual cost of drum manufactured. This cost will go to finished mixer. So actual goods receipt of drum should be with actual production order cost . Also actual mixer cost should be taken from

  • MacBook Pro freezing with external monitor?

    I recently got an external monitor to use with my MBP (early 09). So it seems that if I have my monitor hooked up (and maybe also when my power cable is plugged in) and my computer goes into sleep mode, the whole thing freezes and i have to hard rest

  • Can't open Word docs with Pages 08

    Since upgrading to SL I found that I can no longer open Word documents sent to me via email attachments. Can this be remedied? I thought Macs were able to "translate" Word files. Help!

  • Export Report to Excel - Advice Needed

    I'm currently trying to export a report from an Apex Application to an excel spreadsheet. My report has conditional formatting (background colour) that i want to apply to the spreadsheet. Does anyone have any ideas on how to achieve this, (Sylk, XML)

  • HT4623 iOS 7.0.3 update

    I have been trying to download the ios7 update on my iPad for 24 hours; it continues to show the message "less than one minute remaining." Help please!