aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Cross plaftform GUI layout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Cross plaftform GUI layout" Watch "Cross plaftform GUI layout" New topic
Author

Cross plaftform GUI layout

Sarone Thach
Ranch Hand

Joined: Jun 25, 2003
Posts: 89
Hi all,
I'm developing and designing my GUI on Windows XP. The Layout of the GUI looks fine, on XP, but when I deploy it to the Mac platform everything looks out of place, the text is shifted either left or right, its not aligned, and I can't see some text.
What is going on? I somehow don't think the layout manager makes a difference, whether it is Gridbaglayout, or panelayout or border. They just don't appear nice on the Mac platform.
If anyone has some hints, documents, links, anything to help me understand why I can't get it right please forward some information to me.
Thanks so much in advanced.
Sarone
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

What look and feel are you using?


GenRocket - Experts at Building Test Data
Sarone Thach
Ranch Hand

Joined: Jun 25, 2003
Posts: 89
The look and feel on Windows is Windows and the look and feel on Mac is Mac OS X Aqua. I prefer to use te Aqua on the Mac instead of the Java Look and feel or Metal.
So the question is now, is it possible to design a GUI in one look ie Windows and displaying it on another look and feel, without all the alignment issues. If not, hmmm help!
Because I can only develop on Windows XP. As I am using JBuilder 9, and its not supported on the MAC.
Thanks
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Well, Layout Managers were designed to overcome such issues. However, from my experience, they don't do the job quite well enough. I always had problems going from Windows to Linux. And sometimes it was with the same look and feel. The main problems I had were font issue between the default font for Windows and the default Font for Linux. Linux's Font was a bit bulkier and made things layout kind of strange in places.
I will leave this thread to someone a bit more afluent in the MAC world though.
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
I am no GUI pro, by any stretch of the imagination, so take what I say with a grain of salt.
I have recently written and released 2 Java apps in Swing (see http://www.filenabber.com if curious) . In both I set the Look and Feel to the System Look and Feel. I use a BoxLayouts and GridLayouts and add Panels to those.
A user emailed me and told me that both of my apps worked on Mac OS X and sent me screenshots. One of them had a problem though. The fonts on the Mac caused one of my dropdowns to display incorrectly because of the sizing. I came up with 2 solutions:
1)Allow the Mac user to resize the main windows so that everything fixed itself.
2)Make the inital window size on a Mac slightly larger so that it fixed itself.
Neither of these solutions is perfect (in fact, they are really just "hacks"). I have also internationalized (i18n) my programs so that at any time, I can drop in a new message bundle file and the entire program will be in another language. This opens up a whole new can of worms since all of the label, buttons, etc. will now be different sizes. I think most of the GUI problems will be solved in my case by resizing the window so everything will fit (but I need to test that theory out
Brian
[ October 07, 2003: Message edited by: Brian Pipa ]

My Java Freeware:<br />MACCC - <a href="http://maccc.pipasoft.com" target="_blank" rel="nofollow">http://maccc.pipasoft.com</a><br />Nator - <a href="http://nator.pipasoft.com" target="_blank" rel="nofollow">http://nator.pipasoft.com</a>
Narayanan Jayaraman
Ranch Hand

Joined: Apr 09, 2003
Posts: 52
I do have the same problem. Everything looks/works great in Windows. I am using SystemDefault Look&Feel. But when I deploy to Sun solrais everything goes havoc. And also I get this error ....
Font specified in font.properties not found [-urw-itc zapfdingbats-medium-r-normal--*-%d-*-*-p-*-sun-fontspecific]
Any help ???
"Write Once and debug everywhere ? "
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
But when I deploy to Sun solrais everything goes havoc. And also I get this error ....
Font specified in font.properties not found [-urw-itc zapfdingbats-medium-r-normal--*-%d-*-*-p-*-sun-fontspecific]

I get a similar message on linux when running my app. It doesn't "go to havoc " though - everything works fine.
brian
Narayanan Jayaraman
Ranch Hand

Joined: Apr 09, 2003
Posts: 52
Did you do anything "special" when deployed in Linux system ?
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

There's actually a post in the Linux/Unix forum here that tells you how to fix the font.properties problem...

Narayanan, you have to be more specific with what you mean by "goes to havoc" if you want people to help figure out what is wrong... What works on Windows XP that doesn't work on Linux? Are you using all standard Swing widgets or lots of custom stuff? What JDK versions are you using on each machine?


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Narayanan Jayaraman
Ranch Hand

Joined: Apr 09, 2003
Posts: 52
Thanks for your link. I use JDK 1.3 in both windows and Solaris system. I have used basic Jframes and JTables and don't have any custom piece in it. When I run the application (I bring up through X-Manager) it doesnt display data in Jtables properly and also unable to navigate through JTables. Any Idea ?. What do I need to change font.properties file ? Appreciate your help.
Thanks
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Are there any TableCellRenderers or TableCellEditors used in the JTable? is there any code for setting the size of columns, rows, or the table in general? Especially be on the lookout for code that sets sizes to some specific value... default fonts sizes are different on different OSes and you will run into lots of problems assigning a set size to a widget that displays text. Where is the data for the table coming from - hardcoded values, a database, flat file, remote connection, etc.? What navigating actions are failing?

As far as fixing the fonts.properties error, all you have to do is find the ZapfDingbats font, copy it to the JDK fonts directory, and update the fonts.dir file in that directory. Or you can edit that entry out of fonts.properties to tell Java that that font isn't available.
Narayanan Jayaraman
Ranch Hand

Joined: Apr 09, 2003
Posts: 52
1. I have used TableCellRenderers/TableCellEditors used in the JTable.
2. Data value comes from database. I am not able to scroll through tables
3. Here is my code for setting column size
public static void sizeColumnsForTable(JTable table) {
TableColumnModel tcm = table.getColumnModel();
for(int i=0; i < tcm.getColumnCount(); ++i) {
TableColumn column = tcm.getColumn(i);
int w = getPreferredWidthForColumn(table, column);
column.setMinWidth(w);
//column.setMaxWidth(w);
}
}
//---------------------------------------------------------------------------------
public static int getPreferredWidthForColumn(JTable table, TableColumn col) {
int hw = columnHeaderWidth(table, col), // hw = header width
cw = widestCellInColumn(table, col); // cw = column width
return hw > cw ? hw+10 : cw+10;
}
//---------------------------------------------------------------------------------
public static int widestCellInColumn(JTable table, TableColumn col) {
int c = col.getModelIndex(), width=0, maxw=0;
for(int r=0; r < table.getRowCount(); ++r) {
TableCellRenderer renderer =
table.getCellRenderer(r,c);
Component comp =
renderer.getTableCellRendererComponent(
table, table.getValueAt(r,c),
false, false, r, c);
width = comp.getPreferredSize().width;
maxw = width > maxw ? width : maxw;
}
return maxw;
}
Appreciate your help. Thanks
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Hmmm... can you post the code for the renderers and editors you are using...

I tested out Windows look and feel and Motif look and feel on my Win2K box here at work and they return different sizes using the default font. I'll try this on my Linux box at home later and see what the results are, too... Maybe there is some code in a renderer or editor that is assuming a certain font size?

When you say "I am not able to scroll through tables", do you mean you aren't able to use the mouse scroll wheel or pushing the down button to scroll through the table? Maybe an editor is 'grabbing' focus for some reason? Or maybe widgets aren't reporting the right size and the scrollpane doesn't think they need to scroll?
Narayanan Jayaraman
Ranch Hand

Joined: Apr 09, 2003
Posts: 52
OK. I have listed Cell renderer/Editor. Regarding Table freezing issue, I display two tables in One single dialog window. when I click on data rows in left Table , Right side table changes it value. It works fine in windows. But in Solaris, Right side table doesnt change value and also I am unable to select rows in it. Please let me know if you need any more details. Thanks
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.tree.*;
import java.awt.event.MouseEvent;
import java.util.EventObject;
abstract public class AbstractCellEditor implements TableCellEditor {
protected EventListenerList listenerList = new EventListenerList();
protected Object value;
protected ChangeEvent changeEvent = null;
protected int clickCountToStart = 1;
//------------------------------------------------------------------------------
public void addCellEditorListener(CellEditorListener l) {
listenerList.add(CellEditorListener.class, l);
}
//------------------------------------------------------------------------------
public void cancelCellEditing() {
fireEditingCanceled();
}
//------------------------------------------------------------------------------
protected void fireEditingCanceled() {
Object[] listeners = listenerList.getListenerList();
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==CellEditorListener.class) {
if (changeEvent == null)
changeEvent = new ChangeEvent(this);
((CellEditorListener) listeners[i+1]).editingCanceled(changeEvent);
}
}
}
//------------------------------------------------------------------------------
protected void fireEditingStopped() {
Object[] listeners = listenerList.getListenerList();
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i] == CellEditorListener.class) {
if (changeEvent == null)
changeEvent = new ChangeEvent(this);
((CellEditorListener) listeners[i+1]).editingStopped(changeEvent);
}
}
}
//------------------------------------------------------------------------------
public Object getCellEditorValue() {
return value;
}
//------------------------------------------------------------------------------
public int getClickCountToStart() {
return clickCountToStart;
}
//------------------------------------------------------------------------------
public boolean isCellEditable(EventObject anEvent) {
if (anEvent instanceof MouseEvent) {
if (((MouseEvent)anEvent).getClickCount() < clickCountToStart)
return false;
}
return true;
}
//------------------------------------------------------------------------------
public void removeCellEditorListener(CellEditorListener l) {
listenerList.remove(CellEditorListener.class, l);
}
//------------------------------------------------------------------------------
public void setCellEditorValue(Object value) {
this.value = value;
}
//------------------------------------------------------------------------------
public void setClickCountToStart(int count) {
clickCountToStart = count;
}
//------------------------------------------------------------------------------
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
//------------------------------------------------------------------------------
public boolean stopCellEditing() {
fireEditingStopped();
return true;
}
//------------------------------------------------------------------------------
}
BulbEditor.java
=========================================
public class BulbEditor extends AbstractCellEditor {
BulbRenderer renderer = new BulbRenderer();
public BulbEditor() {
renderer.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setCellEditorValue(Boolean.TRUE);
fireEditingStopped();
}
});
}
});
}
//------------------------------------------------------------------------------
public Component getTableCellEditorComponent(
JTable table, Object value,
boolean isSelected,
int row, int col) {
return renderer.getTableCellRendererComponent(
table, value, true, true,
row, col);
}
//------------------------------------------------------------------------------
}
BulbRenderer.java
==========================================
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class BulbRenderer extends DefaultTableCellRenderer {
private ImageIcon darkBulb = null; //new ImageIcon("button.jpg")
private ImageIcon brightBulb = null; //new ImageIcon("button_lit.jpg");
//------------------------------------------------------------------------------
public BulbRenderer() {
setHorizontalAlignment(JLabel.CENTER);
}
//------------------------------------------------------------------------------
public BulbRenderer(ImageIcon darkBulb, ImageIcon brightBulb) {
setHorizontalAlignment(JLabel.CENTER);
this.darkBulb = darkBulb;
this.brightBulb = brightBulb;
}
//------------------------------------------------------------------------------
public Component getTableCellRendererComponent(
JTable table, Object value,
boolean isSelected,
boolean hasFocus,
int row, int col) {
Boolean b = (Boolean)value;
setIcon(b.booleanValue() ? brightBulb : darkBulb);
return this;
}
//------------------------------------------------------------------------------
}
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Sorry... I still don't see anything that would cause your application to "go t o havoc" under Linux...

I still think something somewhere is hardcoding some sizing information... this problem could be a bug in the Linux version of the JDK itself, but I think a lot more people would notice it if it was a JDK level problem...

Try running your application on Windows with Motif Look and Feel. This is what Linux uses as system look and feel, so if there are any issues with sizing you should see this on whatever platform is using this L&F too...

just replace the line

with


and see if the same problem occurs on Windows.

Also, if you want to email me your code (nate@javaranch.com) I can try to debug it all together and see if I see a problem... (it looks like all the code would take up too much space to post in the forums...)
Narayanan Jayaraman
Ranch Hand

Joined: Apr 09, 2003
Posts: 52
Hi Nathan,
I have re-installed JDK 1.3 and it seems to be apperaring fine. But yet to do full blown testing. I 'll update accordingly. Thanks for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cross plaftform GUI layout