Hi!

I'm writing a java app with a swing gui. The problem is the following. I am developing on a mac, where it runs fine. but when I run it on Windows Posready09 (Windows XP), which is the platform the app is intended for, or Windows7, half of the window doesnt load. It just stays gray, until I start to klick on stuff which makes components appear. Also when I drag the window the graphics get all messed up.

It worked fine on Windows until recently. I have made some minor adjustments and additions since then, but nothing I can put my finger on which would cause this problem.

Does anyone know what this could be caused by? Some windows java gui secrets I'm not aware of?

Post your code and I'll see what I can do. The first thing to check would be are you using an MAC specific library files? If so then you will need to find/make their windows equivalent

I have isolated the problem.

In one class i override the paint method which looks like this...

public void paint( Graphics g ) {
        autoResizeColWidth( billTable, billTableModel );
        this.paintComponents(g);
    }

When i comment out pain it works fine, but i want to be call autoResizeColWidth every time paint is called. But problem seems to be the paintComponents method. Should I paint child components some other way?

Ok! Problem solved, It works if i call paintChildren istead of paintComponents, I guess i should have tried for myself just a bit longer.

Do you have an explanation to why painComponents didn't work?

From my understanding paint by default calls paint components so you should not have to reference it. Did you remember to call the super paint components method?

Oh I didn't call supers paintcomponents. But the idea was to override paint so I could call autoResizeColWidth.

Should I maybe only call super.paintComponents() instead and autoResizeColWidth? In reverse order that is.

Oh I didn't call supers paintcomponents. But the idea was to override paint so I could call autoResizeColWidth.

Should I maybe only call super.paintComponents() instead and autoResizeColWidth? In reverse order that is.

What does the auto-resize column width method look like

It just resizes table columns to its contents, looks like this...

public JTable autoResizeColWidth(JTable table, DefaultTableModel model) {
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.setModel(model);

        int margin = 5;

        for (int i = 0; i < table.getColumnCount(); i++) {
            int                     vColIndex = i;
            DefaultTableColumnModel colModel  = (DefaultTableColumnModel) table.getColumnModel();
            TableColumn             col       = colModel.getColumn(vColIndex);
            int                     width     = 0;

            // Get width of column header
            TableCellRenderer renderer = col.getHeaderRenderer();

            if (renderer == null) {
                renderer = table.getTableHeader().getDefaultRenderer();
            }

            Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);

            width = comp.getPreferredSize().width;
            
            // Get maximum width of column data
            for (int r = 0; r < table.getRowCount(); r++) {
                renderer = table.getCellRenderer(r, vColIndex);
                comp     = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false,
                        r, vColIndex);
                width = Math.max( width, comp.getPreferredSize().width );
            }

            // Add margin
            width += 2 * margin;

            // Set the width
            col.setPreferredWidth(width);

        }

        DefaultTableColumnModel colModel  = (DefaultTableColumnModel) table.getColumnModel();
        int w0 = colModel.getColumn(0).getPreferredWidth();
        int w2 = colModel.getColumn(2).getPreferredWidth();
        colModel.getColumn(1).setPreferredWidth( scroll.getViewport().getExtentSize().width -w0 -w2 -4 );

        ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(
            SwingConstants.LEFT);

        // table.setAutoCreateRowSorter(true);
        table.getTableHeader().setReorderingAllowed(false);

        return table;
    }

}

my paint method looks like this now

public void paint( Graphics g ) {
        autoResizeColWidth( billTable, billTableModel );
        super.paintComponent(g);
}

this works, so does this.paintComponents(g), instead of super.paintComponents(g).

my paint method looks like this now

public void paint( Graphics g ) {
        autoResizeColWidth( billTable, billTableModel );
        super.paintComponent(g);
}

this works, so does this.paintComponents(g), instead of super.paintComponents(g).

Good glad its working. since you are not overriding the paintComponents method in your class calling this.paintComponents is the same as calling super.paintComponents.

Make sure to mark the thread solve :)

Best of luck with the Java programming

Yea i thought that to, but it doesn't work in windows for some reason. super.paintComponent must be doing something more than calling paintComponents in windows.

Yes i thought that to, but the behaviour is totally different in Windows. Well well, the problem is solved. Thanks for you help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.