• Post Reply Bookmark Topic Watch Topic
  • New Topic

My hate/hate relationship with Swing  RSS feed

 
Chris Crawford
Ranch Hand
Posts: 116
1
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been using Java and Swing for about ten years now, and I have NEVER been comfortable with Swing. Maybe it's because I spent 20 years displaying graphics directly, and the highly indirect methods in Swing just won't get down my craw. In any event, I have come here many times to get help with my Swing problems. About half the time I get answers just by searching through the extensive library of past answers, but this is not one of those times. Before I launch into details, though, I'd like to ask some general questions.

First, is it inadvisable to mix AWT components with Swing components? In this case I have a JFrame containing AWT TextAreas, JRadioButtons, and a JButton. Am I just asking for trouble? I am using a null layout for the JFrame and hard-coding component positions. Is it inadvisable to use a null Layout and hard-code component positions?

Yes, I already have tried the pure Swing approach with a proper layout system (just some nested BoxLayouts). I have torn out the last wisps of hair I once had trying to get the thing to work properly. It screws up in myriad ways. My clever debugging techniques (e.g., change just one thing at a time and see how the change alters the output) all fail miserably; Swing's responses to such techniques appear to be arbitrary. If you'd like a good laugh, you can check out my alternately pathetic and furious attempts to get it working in full-court Swing here:
I Hate Swing, Volume XXIX

I warn you, it's a long, messy tale of trial and error (always error).

Second, why would my AWT TextArea wrap text lines properly while running inside Eclipse, yet refuse to do any line wrapping when exported to a standalone application?

Third, I have setBackground(Color.white) on every single component in the program, yet under some circumstances, I get partial lightGray backgrounds. I'm sure that this reveals some error in my code, but I can't even begin to guess what it might be. There's no reference anywhere in my code to Color.lightGray. Who's putting that lightGray into my program?

I know that I really should come to terms with Swing. I have spent lots of time going through older code of mine that I eventually managed to get working, and I can't figure out why my current effort fails. I will not post my code yet, because I'd like to get answers to these three easier questions; I hope that such answers will help me avoid putting my code (about 350 lines) up and asking you folks to pore over every damn line, saving us all some time.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it inadvisable to mix AWT components with Swing components?

The answer used to be Yes, but it's gotten better: http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html

Is it inadvisable to use a null Layout and hard-code component positions?

Yes

why would my AWT TextArea wrap text lines properly while running inside Eclipse, yet refuse to do any line wrapping when exported to a standalone application?

Not sure abut the difference, but I take it using TextArea.setLineWrap(boolean) explicitly did not have the desired effect? This may help: http://www.coderanch.com/t/332768/GUI/java/Text-wrapping
 
Chris Crawford
Ranch Hand
Posts: 116
1
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help. Both of the links you provided proved to be interesting reading. I'm relieved to see that the lightweight and heavyweight components now play together nicely. The discussion on this board that you linked to comes from 2001 -- isn't stuff that old obsolete by now? The fellow there is wrapping the lines himself. I actually did that in an earlier version of my code, but decided that I was being too primitive. I may go back to that.
I think that I'll try a third approach: start from scratch with a pure Swing approach, and then build it up from there. Perhaps that will solve my problem. If not, I'll be back here with code.

Thanks again.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could make the switch to JavaFX. If you really want to
 
Chris Crawford
Ranch Hand
Posts: 116
1
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I've tried a variety of things, and I'm still stumped, so here goes with the code. Most of the listing below the paint() method is irrelevant. I include it only for completeness. The question I ask is this: What is causing the four malfunctions listed in the long comment section?

 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I wouldn't use a TextArea instead of a JTextArea just because I theoretically could. I still wouldn't drag old AWT components out of the storage locker if there were equivalent Swing components available (which there are).

And if I were trying to get some facet of the GUI to work nice, as in your problem description, I would write a small piece of code to play around with. Have a look at the SSCCE document (<-- follow that link) to see what I mean -- I really don't want to download all of your code to see what it does.

Edit: I'll just note that your paint(Graphics) method doesn't start out by calling the superclass's paint(Graphics) method, as it should. It isn't clear to me why you even have that method, since as far as I can see it isn't painting anything on the screen.
 
Chris Crawford
Ranch Hand
Posts: 116
1
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I'll replace the TextArea with a JTextArea. I had that in place at one point; I'll restore it to see if that helps this time around. And I'll be sure to use the TWO calls required to make line wrapping happen properly.

Yes, the paint() method is an orphan now. Just leftover baggage.

I'll reduce it to a SSCCE to make it more manageable.

Thanks. I'll post results when I have them.
 
Chris Crawford
Ranch Hand
Posts: 116
1
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops! The JTextAreas were already in place. No plain AWT TextAreas.

I'm stripping this baby down to the absolute minimum necessary to demonstrate the problem.
 
Chris Crawford
Ranch Hand
Posts: 116
1
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This message contained an error that I am now correcting.
 
Chris Crawford
Ranch Hand
Posts: 116
1
Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As always, the process of formulating a problem for other people to see has itself solved the problem. The funny thing is, I don't really know what it is that I did that solved the problem. I stripped the thing down to the absolute minimum that worked, then slowly started adding features bit by bit. If an addition created a problem, I fixed it quickly and moved on. In many cases, I changed just one line of code at a time, checked that everything was still OK, then moved to the next line.

It was a tedious process, but now the thing works properly.

One interesting realization: I have always designed code by first setting up the data structures, then setting up the display and UI. This time I reversed that process.

Thanks for all the help; usually the best way to help somebody out of a problem is to require them to formulate it in precise terms.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent!

(Of course there are two steps to that process: first somebody tells you to simplify your problem, and second you actually do that. We don't always hear about the second step here, but it's good to see the process works when done right.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!