Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Weird Windows/Linux inconsistencies

 
Ranch Hand
Posts: 102
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote a few programs in Windows, some using Java Swing and others using JavaFX.  They work great in Windows 10 but when I try to run them in Linux Mint 20 with Cinnamon, I get strange problems:

- First of all, I can't seem to open them by double-clicking and have to type the command instead.  If I double-click I get an error that they don't have execute permissions, even though I copied them onto the ext4 drive and set the execute permission (the box is checked when I view the properties).  I somehow got it to work on a different Linux installation, but I don't remember what I did differently.  EDIT: Actually, if I right-click to choose a program to run it with, it doesn't seem to give me the option for Java 8, even though I set that to the default rather than 11.

- Then when I run the program, I get things the wrong size.  I don't know if the windows and buttons are the wrong size, or the text, but text labels which fit inside of windows when I run using Windows 10 do NOT fit in the size of a popup window, or in some cases the text for the name of a button doesn't fit, so instead of saying "Cancel", it'll say something like "Can...".  This is unacceptable because there is crucial information which must be displayed, and it must be obvious what each button does.

- In Windows I had a popup window that I wanted to hide (I don't remember whether this is in Swing or FX), but I think it was the only window at the time, and I wanted to show another one afterwards.  Anyway, I tried it a few ways, but for some reason, if I successfully hid the window it closed the program.  So to get around it, instead of hiding it I just set the coordinates to negative numbers, so that it wouldn't display on the screen no matter what resolution it's using.  This works great in Windows, but then when I run it in Linux it automatically sets the coordinates to 0 and puts the window in the corner of the screen!

So much for write once, run anywhere.  I REALLY need this to be compatible between both types of OS, and I very much want to avoid having to make two slightly different versions, especially since I don't have Eclipse or any sort of development software installed in Linux, and I was only planning to run the programs there, so I'd have to go back to Windows to try to fix some code that I think might fix a problem, compile it and put it on a USB stick, put it in the other computer and then test it.  As you can imagine, that method is NOT ideal!

EDIT: And then what really makes no sense is that I'm setting the exact width and height to the pixel, so I guess it must be a difference in font size, right?  Or in some cases it may possibly be a different size of window border which could leave less interior space I suppose.  But what could I do about either of those problems?
 
Terrance Samson
Ranch Hand
Posts: 102
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I did find this one link:

https://www.perfectabstractions.com/blog/how-to-install-windows-fonts-in-java-on-linux

And it looks like it might do what I want, but it's 6 years old and using Java 7, whereas I'm using one of the newer updates of version 8.  Does anyone know if that will work or not?  One of the instructions says to delete a file so that it will use the new stuff instead (though it doesn't actually tell me to create any file).  I suppose I could just rename or move the file and it would have the same effect, right?  That way, if anything went wrong I could reverse it.  But one way or another I need to get this to work.
 
Marshal
Posts: 26493
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Terrance Samson wrote:I'm setting the exact width and height to the pixel



This is most likely your problem, then. Writing Swing code in which you specifically set the size of components seems like a good idea at first, but it even causes problems when your code is used on screens with different resolutions. Swing was specifically written as a write-once-run-anywhere application and so you don't need to (and shouldn't) do that. Swing components will set their own preferred sizes based on font size and screen resolution, so don't fight that. At most you should set the preferred size of your top-level panel, and maybe not even that.
 
Paul Clapham
Marshal
Posts: 26493
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Terrance Samson wrote:Anyway, I tried it a few ways, but for some reason, if I successfully hid the window it closed the program.  So to get around it, instead of hiding it I just set the coordinates to negative numbers, so that it wouldn't display on the screen no matter what resolution it's using.



It's better to fix a problem rather than look for workarounds. I would expect that setVisible(false) would hide a window, for example. You can ask those questions here too.
 
Terrance Samson
Ranch Hand
Posts: 102
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but when I did setVisible(false) it caused the program to exit!  Even when I was debugging the debugger stopped.

And concerning the font size, I realize now that it might not generally be the best thing to try to set the sizes manually, but I wrote all of the programs that way because:

- I'm used to doing it that way, especially in .NET and C++, where I have no layout managers (and I've never really liked layout managers).

- I have stuff that must align to grids or else it would look pretty ridiculous, so I thought standard button sizes would be reasonable.

- It didn't occur to me that two operating systems couldn't even set the font sizes to match, or for that matter, that Java wasn't just specifying them itself in order to make them consistent, and why wouldn't it do that?

Anyway, I did follow the instructions in the link that I posted previously, but that didn't seem to have any effect at all.  I don't know why though.  I installed the fonts and edited the configuration file, though admittedly, I couldn't find the file that it suggested I remove in step 4 (I have a different version of Java and it may be installed in a different location or something, but I'm not sure).

Really though, I expected that I might only get this sort of problem if I used AWT (which I didn't), since it's dependent on the OS, but I didn't expect it in Swing and JavaFX.
 
Paul Clapham
Marshal
Posts: 26493
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Terrance Samson wrote:Yes, but when I did setVisible(false) it caused the program to exit!  Even when I was debugging the debugger stopped.



I just wrote this code, which is just a button which hides the JFrame and sets a timer to unhide it 5 seconds later:

 
Rancher
Posts: 3232
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes, but when I did setVisible(false) it caused the program to exit!


Well, I've never used Linux before but I doubt using setVisible(false) will cause the application to close if the application is coded properly.

Even Paul's example above does not follow Swing coding guidelines.

All Swing components should be created and updated on the Event Dispatch Thread (EDT) or you could have random results.

Will the above code cause a problem? I doubt it, but you never know.

Read the section from the Swing tutorial on Concurrency for more information.

Regarding layout. If you choose not to use layout managers then you will have problems. Layout managers a a main reason the application should be platform independent. They account for different pixels sizes and different component LAF. Using appropriate layout managers will allow components to be aligned in grids easily.
 
Paul Clapham
Marshal
Posts: 26493
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


if you like, but I too doubt it will make a difference.
 
Bartender
Posts: 1304
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes, but when I did setVisible(false) it caused the program to exit!  Even when I was debugging the debugger stopped.



Try to search for files named "hs_pidxxxxx". I've encountered cases on both linux and windows where some operations on GUI components caused the crash of the VM.
It turned out that some components executing calls to native libs to render itself (or to perform some othe operation under the hood) caused an internal unrecoverable error. I experienced this weird issue, for example, on Windows machines after a GPU driver updated, when using, for example, JFileChooser, which under the hood relies upon native file management.
I resolved the problem by installing the latest JRE release (of the same version, I mean).
 
Terrance Samson
Ranch Hand
Posts: 102
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul Clapham, thanks for the code, but it's hiding the form before it starts the timer, so by then the program would already close.  Though if I remember correctly, when you start the timer it will just freeze for that amount of time before it gets a chance to hide the form.  So either way that wouldn't work, unless I'm overlooking something.

Rob Camick, actually it was closing the program in Windows, which was why I did it that way, but I assume it might have the same problem in Linux anyway.  I looked into doing concurrency stuff in Swing and FX, and I had so many problems with it not working at all that I decided to just do things the simple way (in C# at least I was able to call the DoEvents function, but does Java even have an equivalent?).  In any case, I got it working on Windows just how I want, but I only need it to work the same on Linux.  Also, it may be too late to start dealing with layout managers, because I've already written all of my code without them, and frankly I've spent so many months on everything that I really don't want to have to go back in and do anything really significant.  I'm just about done with this program, but I just need it to work on Linux.  How can there be "different pixel sizes"?  Aren't all pixels the same size?  Or I guess different monitors would have different pixel sizes but as long as everything were the same number of pixels, it should display larger or smaller, but all parts would be relatively the same size by ratio.

Claude Moore, that's really weird.  I guess that may be what happened to me then.

Anyway, I don't think anyone has mentioned the link that I found about installing Windows fonts in Linux.  Does that seem like a good idea if I can get it to work?  I've had some trouble but if I can do that it should be the easiest solution.  By the way, this program will be in a controlled environment so I can guarantee that the same fonts are always installed whenever it's run.  The only other thing that I wonder is whether there's a difference in the font size when its run on a different OS.
 
Paul Clapham
Marshal
Posts: 26493
81
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Terrance Samson wrote:Paul Clapham, thanks for the code, but it's hiding the form before it starts the timer, so by then the program would already close.  Though if I remember correctly, when you start the timer it will just freeze for that amount of time before it gets a chance to hide the form.  So either way that wouldn't work, unless I'm overlooking something.



I hope you didn't think that the timer was an important feature of being able to hide the JFrame without ending the program. I put it there so that there would be something which subsequently made the JFrame visible again. Because your program is going to want to do that, right? And if you're asserting that the program ends before the timer starts, I can assure you that it doesn't because I actually ran the code and tested it before posting it. And no, the timer doesn't cause anything to freeze -- you could have a look at a tutorial to see what the timer actually does. And anyway it's not the timer which hides the form.

So yes, you're overlooking something. It seems like you've written a large amount of code and then run into problems; when that happens it's very hard to try to fix those problems because of the deadweight of all that code. It's better to write a small amount of code, an SSCCE, which demonstrates the problem, and then work on fixing that small amount of code. Once you've done that you should be able to implement the fix into your large amount of code.

My advice would be to write a small amount of code with two JFrames. One just contains some random text and the other has two buttons "Show" and "Hide" which show and hide the first JFrame using setVisible on it.
 
Terrance Samson
Ranch Hand
Posts: 102
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well actually I don't intend to show the window again because it shows at the beginning, hides and is replaced by a main window.  I realize it can be easier to make a small project to test a few things independently, but I didn't think it was necessary because I had gotten everything to work - or so it seemed, until I tried to run it in Linux.
 
Saloon Keeper
Posts: 23440
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To say explicitly what others have mentioned. GUI applications should NOT assume the availability of fonts, colors, geometries or other GUI assets if you intend to run them on any other computer than the one you designed them on. And yes, that includes other Windows computers.

Even in Windows, different screen resolutions and fonts were often encountered. People complain about how OpenOffice documents mess up text placement, but that's because they never dealt with multiple printers on Windows before TrueType™ fornts. Word gets its typesetting metrics from the fonts available on the printer and video output devices exactly like OpenOffice does. However, once TrueType came out, people gravitated mostly to the Microsoft-supplied Times New Roman, Arial and Courier Ugly fonts. Even under Windows, however, actual font selection has always been done by presenting a set of desired characteristics and having the graphics subsystem return a "best fit". The difference when porting to Linux is that the best-fit matches are not generally going to be an exact duplicate of the best-fit matches for a typical Windows machine.

And incidentally, yes, there is a font pack that supplies the core Microsoft TrueType fonts available as an add-on to Linux and it's easy to install, although for copyright reasons (presumably) it's not part of base Linux distros.

You should use a LayoutManager instead of absolute pixel co-ordinates. It's not ust a matter of monitors being 2400x1600 versus 1024x768 or whatever, Macintosh screens originally had square pixels and other machines had rectangular pixels, so a 45-degree line wouldn't plot right done by brute force either. Use of relative layouts will reduce the need to tune each and every button and text element manually.

Also note that Swing as a "skin" capability (Programmable Look and Feel, or PLAF) that can make Swing apps look less foreign to the OS that they are running on. There are about 5 PLAFs in the standard set and if memory serves, one it tuned especially for Windows.
 
Terrance Samson
Ranch Hand
Posts: 102
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, that's a pretty lousy situation.  As I said, I don't know about layout managers, because I've already set up the whole thing to run the other way, but maybe I'll just make adjustments and possibly have two versions of the program, even though it is Java.
 
Paul Clapham
Marshal
Posts: 26493
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Terrance Samson wrote:Well actually I don't intend to show the window again because it shows at the beginning, hides and is replaced by a main window.



Then you should close it by calling its dispose() method. Or perhaps you should have the main window start up and show a modal dialog instead.
 
Marshal
Posts: 72478
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Terrance Samson wrote:. . . two versions of the program . . .

That would be even lousier a solution. Yes, if you have got incorrectly designed Swing code, not using layouts, any correction will take a very long time and be demanding to write.
 
Campbell Ritchie
Marshal
Posts: 72478
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . have the main window start up and show a modal dialog instead.

Agree; use dialogue windows rather than messing about with multiple frames. As I said somewhere else a few minutes ago, one application, one JFrame.
 
Tim Holloway
Saloon Keeper
Posts: 23440
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Terrance Samson wrote:Well actually I don't intend to show the window again because it shows at the beginning, hides and is replaced by a main window.  I realize it can be easier to make a small project to test a few things independently, but I didn't think it was necessary because I had gotten everything to work - or so it seemed, until I tried to run it in Linux.



The technical term for this is "Splash Screen" and it's an old and well-established trick often used. Swing can do one just as well or better than any other GUI framework and it's not OS-dependent.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Tim says...Swing has a spashscreen already:
https://docs.oracle.com/javase/tutorial/uiswing/misc/splashscreen.html

The tutorial may be a little dated, but I don't think the class has changed much.
 
Terrance Samson
Ranch Hand
Posts: 102
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I don't know about a dialog window, because it must appear before the main window, and then the main window must only appear after this smaller window is closed.
 
Paul Clapham
Marshal
Posts: 26493
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Terrance Samson wrote:Well I don't know about a dialog window, because it must appear before the main window, and then the main window must only appear after this smaller window is closed.



So then it's a splash screen, like Tim and Dave just said. It's easy to use a splash screen in Java, you don't even have to write any code, it's just an option on the command line which runs your program.

Unless it requires user interaction, which you didn't mention.
 
Terrance Samson
Ranch Hand
Posts: 102
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But it must be an interactive window designed with buttons and there is code as well.
 
Paul Clapham
Marshal
Posts: 26493
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay then. Let me summarize my understanding of these two windows. You have an introductory window with buttons and processing. Then when the processing is complete (whatever that involves) the introductory window disappears and a new window (more permanent?) appears.

If that's the case then it seems to me that the GUI logic goes like this:

1. Display the introductory window.

2. When the processing is complete, dispose the introductory window and then display the permanent window.

But you seem to have done something different?
 
Campbell Ritchie
Marshal
Posts: 72478
315
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . .
1. Display the introductory window.

2. . . . dispose the introductory window . . .

You can of course use a JDialog as the introductory window. You can add buttons and panels to a dialogue window.
 
Terrance Samson
Ranch Hand
Posts: 102
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul Clapham, yes that's exactly correct, except that when I dispose of the first window it causes the program to close.

Campbell Ritchie, I'm not sure I have enough control over the exact layout and exactly what all I want to have on the introductory window.  I don't see why I can't just use a regular window.
 
Tim Holloway
Saloon Keeper
Posts: 23440
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A JDialog is a window, It just has additional properties to support the concept of a Dialog, which is to say fill-in-the-data and OK/Cancel.
 
Paul Clapham
Marshal
Posts: 26493
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Terrance Samson wrote:Paul Clapham, yes that's exactly correct, except that when I dispose of the first window it causes the program to close.



Then presumably when you say "dispose" you're doing something more than just calling its dispose() method.
 
Terrance Samson
Ranch Hand
Posts: 102
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, all I'm doing is .setVisible = false.
 
Paul Clapham
Marshal
Posts: 26493
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if you'd like to post a small example of code which shows that problem in action, I'm sure people here would have a look at it. Just claiming that setVisible() causes the program to end isn't helpful.

If you want to respond that your code is far too large to post here, then I'm sure it is. So have a look at this link: SSCCE which explains how to produce code suitable for discussing code in a forum context.
 
Paul Clapham
Marshal
Posts: 26493
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually... here's my SSCCE for your stated problem. Give it a try and see if you see the second frame.

 
Terrance Samson
Ranch Hand
Posts: 102
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that, but unfortunately I have to put this on hold for the moment, because I can't seem to run the version of Linux where I want to use all of this stuff (I have Linux on the HD and on a USB stick, but I want to run the programs on the USB stick, and running them on the HD would be a security problem, because of how it's configured).  So I'll have to figure out how to fix that problem (which I'm in the process of doing), before I can get back to this problem.  I swear, this whole process has been one setback after another!
 
Tim Holloway
Saloon Keeper
Posts: 23440
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Unzip JVM onto USB stick
2, Install Java app on USB Stick
3. Create run scripts for Windows and Linux and put them on the USB stick.

As long as the app doesn't ask for any HD-based resources (or other sandboxed/firewalled resources) then you're good to go.

Subject, of course to the fact that you may actually have to run the run scripts from a command window if the box you plug the USB stick into doesn't know how to run them by default.
 
Terrance Samson
Ranch Hand
Posts: 102
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're referring to the security issues, that's not going to cut it.  It's a long story, but I'll get this thing fixed eventually.  I just have to work on the USB Linux issue right now, and I might be able to fix it today, but I'll see how it does.
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic