JTable: Changing column sizes

Hi,
i have two JTables, when one table's column is resized i want to change the size of the equivalent column in the other table.
this is the code i use:
getColumnModel().addColumnModelListener(new TableColumnModelListener() {
            public void columnMarginChanged(ChangeEvent e) {
                if (e.getSource() instanceof DefaultTableColumnModel) {
                    DefaultTableColumnModel dcl = (DefaultTableColumnModel) e.getSource();
                    setTotalTableReferenceModel(dcl);
            public void columnSelectionChanged(ListSelectionEvent e) {
            public void columnAdded(TableColumnModelEvent e) {
            public void columnMoved(TableColumnModelEvent e) {
            public void columnRemoved(TableColumnModelEvent e) {
public void setTotalTableReferenceModel(DefaultTableColumnModel dcl) {
        DefaultTableColumnModel colModel = (DefaultTableColumnModel) totalTableReference.getColumnModel();
        colModel.getColumn(0).setPreferredWidth(200);
        colModel.getColumn(1).setPreferredWidth(200);
        colModel.getColumn(2).setPreferredWidth(200);
        colModel.getColumn(3).setPreferredWidth(200);
        colModel.getColumn(4).setPreferredWidth(200);
        colModel.getColumn(5).setPreferredWidth(200);
        colModel.getColumn(6).setPreferredWidth(200);
        colModel.getColumn(7).setPreferredWidth(200);
    }so basically, on column change, i set the sizes of the other columns to test does it work, but the other table's columns don't move.
any ideas?
Thanks.

Works fine for me.
Here's a simple example:
public class Test {
     public static void main(String[] args) {
          JFrame frame = new JFrame("Test");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setLayout(new BorderLayout());
          final JTable table1 = new JTable(new DefaultTableModel(new String[][]{{"1", "2"}, {"3", "4"}}, new String[]{"col1", "col2"}));
          final JTable table2 = new JTable(new DefaultTableModel(new String[][]{{"5", "6"}, {"7", "8"}}, new String[]{"col3", "col4"}));
          table1.getColumnModel().addColumnModelListener(new TableColumnModelListener() {
               public void columnAdded(TableColumnModelEvent e) {
               public void columnMarginChanged(ChangeEvent e) {
                    for (int i = 0; i < table1.getColumnCount(); i++)
                         table2.getColumnModel().getColumn(i).setPreferredWidth(table1.getColumnModel().getColumn(i).getWidth());
               public void columnMoved(TableColumnModelEvent e) {
               public void columnRemoved(TableColumnModelEvent e) {
               public void columnSelectionChanged(ListSelectionEvent e) {
          frame.add(new JScrollPane(table1), BorderLayout.NORTH);
          frame.add(new JScrollPane(table2), BorderLayout.SOUTH);
          frame.pack();
          frame.setVisible(true);
}If you want further help post a Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the problem.

Similar Messages

  • Cannot change column size in iPod Classic

    I received my Classic today and transferred over all my music. After making changes to various things, I tried to adjust the size of the columns. I cannot.
    I was able to delete a column, then re-add it then change the size. In my 5th Gen Video ipod I could just but the cursor on the right side of the column and click, it would adjust to the size of the largest, Name, Artist, Album etc. I am able to change column sizes in itunes, just not the ipod.
    I've looked through threads to find something that resembles my problem but most seem to be with itunes and not the ipod. Both computers I use are updated to 7.4.1
    I was wondering if I restore the factory settings would I be able to change the column sizes without having to delete and re-add them? Or is this another new lovely and VERY annoying update to itunes/ipod?
    Thanks.

    I can confirm that it's not (specifically) your iPod and it's not your OS. I'm running the newest iTunes through Mac OS 10.3.9 with a 3G iPod and just made a playlist with the same problems. NONE OF MY OTHER PLAYLISTS are affected - the columns there adjust just fine, but any NEW playlists I generate on my iPod will not allow me to individually adjust columns. Right-clicking on a column header does not even allow me to "Auto Size Column", but only to affect ALL the columns, or choose a column to add or remove.
    Interestingly enough - new playlists I generate in iTunes (not on the iPod) work normally. However, it isn't feasible for my 10GB hard drive to automatically sync with my 15GB iPod, so I can't test and see if dragging a playlist to the iPod will generate one with alterable columns.
    My opinion is that this is in some way related to the 7.4 or 7.4.1 update. I believe I last generated a playlist before the 7.4 update, and had no problems.

  • Change column size

    At the publisher's request, I need to change the book size from 7x10 to 7.5x10.5
    I would like to avoid reformatting every page in every chapter.  I want to keep the column size the same and just increase the margins.  However, when i change the page size, the three columns automatically adjust to that new size.
    I have tried selecting "adjust layout" but it doesn't help.  There doesn't seem to be any option for columns except how many there are.  Is there any way to adjust column size independent of page size?

    Peter's last answer was pretty good.
    Did you make your columns by drawing three text boxes on a page, or by drawing a single text box with three columns inside it?
    Did you make your columns by making a master page? Or did you draw a whole bunch of text boxes for each page of your document?

  • Changing column size

    Hi
    I am working on oracle 8i. I have requirement to change a column size from varchar(3) to varchar2(4). I know the command to change the column size. all I need to know, their are 6 child tables. they are referencing this tables column.
    what is the best way do I need to change child tables column size first and then i need to change column sixe of main table. do i need to consider to change views and triggers ans indexex.
    please help me

    Hello,
    You would increase dramatically your chances to get an answer by posting in the Database forum.
    Francois

  • JTable column size

    Hi,
    I have a few question.
    I'm programming by using Sun One studio 4 up 1 CE.
    I want to use the JTable of 2 column and make each columns diffrent size.
    On that IDE shoud I do to change the column size?
    And This is Off Topic. But I 'd like to ask you.
    I use the CE version of studio. Is it OK for me to provide the product for commerce?

    This section from the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/table.html]How to Use Tables shows how to change column sizes.

  • ALV Column size changing

    Hi,
    How to change column size of ALV report during run time
    Regards,

    Hi,
    I think, you need to increase or decrease the width of the column.
    It cannot be changed dynamically .....
    but what we can do is
    In your itab, check all the records for that field's length
    take the highest size and pass it to the fieldcat.
    and see the results.
    So, if less width is there then the width of the column is less and if is very wide then the column will be wide
    Regards,
    Venkatesh

  • Changing the size of column in JTable as per requirement

    I'm copying content of excel file into the JTable.
    But as the number of columns increases the size of columns get reduced.
    But i want the table of same column size for any number of columns in the table.
    If display area size is less then JTable must scroll according to the size.

    Scroll bars appear when the table's size is larger than the scrollpane's viewport. So your viewport is a fixed size. You adjust the size of the table to get the scrollbars to appear and get smaller (as the table's size increases).

  • Change the size of a Column in a Table

    I have a JTable with two columns but I don't know how to change the size of the columns
    Thanks.

    Hi,
    You have to use TableColumn and use JTable.setPreferredSize() method to size ur Columns.
    Hope this helps.
    Cheers
    SS

  • Repositioning viewport of column size change

    I have a JTable with multiple columns and rows. Columns can be resized or moved by dragging the column headers. Whenever a column is resized the table viewport jumps to the top of the table. I am looking for a way to either hold the viewport at the current location (i.e. the location immediately prior to performing the column size change) or to reposition it to that location after the column size change bumps it to the top.
    I have set a TableColumnModelListener to handle the margin change events and the column moved events. In the case of the column moved event (which also bumps the viewport to the top) I can reposition the viewport to the original location with no problem, but the columnMarginChanged event seems to generate a whole flurry of viewport repositioning events, many of which occur after the actual columnMarginChanged event has been processed, thereby overriding any viewport repositioning that I attempt in the event handler.
    I have set a change linstener on the table's viewport to handle repositioning events, but I have no way of determining if the event was triggered by a column resizing operation or by a legitimate scrolling operation.
    Does anyone have any suggestions?

    I'm using 1.4.2_06 on XP and don't experience this problem.
    If you need further help then read this link on [url http://www.physci.org/codes/sscce.jsp]Creating a Simple Demo Program before posting again.

  • Artifacts in JTable TableHeader, when size changed via setPreferredSize

    Hi
    I'm using a JTable to show some data instead of a JList.
    Now when i change the size of the table header via setPreferredSize, the last column header does some graphical errors/artifacts when i scroll to it. How can I avoid this, but still change the size of the header?
    The table is added to a scroll pane and the scroll pane is added to the center area of the panel, which has border layout.
    Any and all help is appreciated, thanks a lot.
    - Hessi

    Don't change the size of the table header.
    Change the size of the TableColumn. Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables" for a working example.

  • How to change font size, maximum column size in the result screen ?

    hi All
    That's great when using SQL Dev.
    But I also have a trouble that how to change font size, maximum column size in the result screen ?
    My users think that font in result screen is shown very small, and whenever the data in each colum is long then it's not shown full data in column, they must double click for extend the size. Have the option to default the max size for showing full data in each column ? I try but still not to do that .
    Appreciate for anyone to help us.
    Thanks all.
    Sigmasvn

    You can't change the font for the results screen yet, however you will be able to select an auto-fit option for selected columns, so if some columns have slitghtly wider text you'll be able to set the column widths to handle these wider columns.
    Also, there s the option of switching the layout of a record in the grid.
    Sue

  • How do i change the color of a JTable's column names

    hai,
    i'm very new to java and trying to customize the look of a JTable.
    how do i change the color of a JTable's Column names. i know its very simple, i just couldn't figure out how to do it!!
    thanx in advance

    table.getTableHeader().setForeground(Color.RED);

  • How to change font size of OATableBean Column Headers

    Dear All,
    I am facing a problem, senario is i am having a table layout with about 20 columns and each column header is about 30 characters long now the problem is when the table is rendered because of header font size its layout is not good looking ... is there any way that i can controll the font size of colmun headers.
    I have tried this solution provided in this forum:
    CSSStyle csNum = new CSSStyle();
    csNum.setProperty("font-size", "8");
    OAPageLayoutBean plb = pageContext.getPageLayoutBean();
    OAMessageStyledTextBean columnBean = (OAMessageStyledTextBean) plb.findChildRecursive("Description");
    columnBean.setInlineStyle(csNum);
    but it only change font size of column data not the header.
    Thanks in advance.

    Hi,
    // Get a handle to the column's header
    OAColumnBean columnBean =
      (OAColumnBean)tableBean.findIndexedChildRecursive("<columnBeanId>");
    OASortableHeaderBean colHeaderBean =
      (OASortableHeaderBean)columnBean.getColumnHeader();
    colHeaderBean.setText("<newText>");Regards,
    Gyan

  • SQL-developer 4 EA unable to change the column sizes

    when viewing data, it is not possible to change the column sizes like you do in a spreadsheet by sliding between the column-headers.
    regards Karsten

    when viewing data, it is not possible to change the column sizes like you do in a spreadsheet by sliding between the column-headers.
    regards Karsten

  • How to change the size of the scrollbar in JScrollPane?

    I set a jtable in the jscrollpane,but the table is very small,so I want to change the size of the scrollbar in the jscrollpane.How can i do?Please help me,Thanks.

    Dropping and re-creating the table is by far the best way (of course you need to unload the table's data to a flat file with ttBulkCp first and then reload it again afterwards).
    a second option is to drop the column and re-add it with the new size but you still need to preserve the data first and put it back afterwards and this is harder for a single column than for a whole table. Also, if you are using replication you should exercise care when using ALTER TABLE to ensure iall replicated copies of the table maintain the same physical structure.
    Chris

Maybe you are looking for