• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextArea

 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hey guys, i'm trying to add a text field to this but when I run it I don't get the text field. Can anyone help me out?
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from your indentation being all wrong, well lines 22 and 28 wrong ... how do you know you don't have a text area? It is possible that the area is so small you can't see it. Try changing line 14 to
textArea = new JTextArea("CodeRanch rocks :)");
and see what happens.
Read more about text components in the Javeā„¢ Tutorials.
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing: don't use null layouts. You have probably messed up the display with line 20, which you should remove.
 
Les Morgan
Rancher
Posts: 717
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And don't use your JFrame for your display area, use a JPanel instead, which you add to your JFrame.
 
Dave Tolls
Ranch Hand
Posts: 2282
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your class extends JFrame (which is generally not a good idea), but it also has a JFrame member variable (called 'f').
If you look at the code the textArea gets added to the current JavaFrame instance (the call to add() on line 15), but it's the (actually local to the constructor) JFrame (also called 'f') that gets shown.
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to add a text area for now where I can write something and then further on down the track have the information entered so be stored in a database, but that's for a later date.

What's so bad about having jframe extended from your class? Bare with me guys I'm still very new to this and I'm finding it difficult to grasp the ideas and concepts behind Java
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Extending a frame usually means adding business logic to it. The business logic should be in a different class. Keep the display classes for display and the business logic classes for business logic. That way, you don't often need to extend display classes.
 
Paul Clapham
Sheriff
Posts: 21884
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mitchell bat wrote:What's so bad about having jframe extended from your class?


When you extend JFrame, you're saying "This class behaves like a JFrame but its behaviour differs in some important way." But you aren't doing that, you're just using a JFrame and calling some of its methods. As far as the compiler is concerned, it's just a JFrame.

If you were going to have several of those JFrames in your application and you always called the same methods as part of their initialization, you might then think of having a subclass of JFrame with those standard initializations built in. But even then you would be better to have a factory method which created a JFrame and initialized it in the standard way.
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Extending a frame usually means adding business logic to it. The business logic should be in a different class. Keep the display classes for display and the business logic classes for business logic. That way, you don't often need to extend display classes.


Makes sense and i'll keep it in mind for future. So instead of extending the frame what do you think would be a better approach?
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
If you were going to have several of those JFrames in your application and you always called the same methods as part of their initialization, you might then think of having a subclass of JFrame with those standard initializations built in. But even then you would be better to have a factory method which created a JFrame and initialized it in the standard way.


As stated above what do you think would be the best, most simple and easy to follow way of going about that?
 
Paul Clapham
Sheriff
Posts: 21884
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mitchell bat wrote:So instead of extending the frame what do you think would be a better approach?


The code you've already written is just fine. Remove the "extends JFrame" and you should find it works just the same -- no, wait a minute, you need "f.add(textArea..." instead of "add(textArea..." and you need "f.setLayout(...". All your code should work on the JFrame referred to by the variable named f.
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: Try changing line 14 to
textArea = new JTextArea("CodeRanch rocks :)");
and see what happens.


Done and nothing changes
 
Paul Clapham
Sheriff
Posts: 21884
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mitchell bat wrote:Done and nothing changes


Extending JFrame and then working with another JFrame inside it was causing that problem. Should go away when you stop extending JFrame.
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took out the extends JFrame and changing the line

to thinking that I didn't set the area for the text to be written but still nothing

I'm trying to make the GUI look like this webpage
 
Paul Clapham
Sheriff
Posts: 21884
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. So far we've had various recommendations for change. You've changed various things. So now you're saying "don't work" with regard to a code base which we don't know what it looks like any more.
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


My apologies for not adding more detail, here is the source code
 
Piet Souris
Rancher
Posts: 1634
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Mitchell,

you are adding both the button and the textArea to the center of the contentpane of the frame.
I personally do not like to add objects directly to a frame, but here is a revised code:
(note that the button gets a size that is as wide as the textArea is; a slight disadvantage of BorderLayout);

I dropped the instance member 'f', since it is defined in the constructor and you don't need to keep a reference to this frane (in this program).

Edit: I don't have Java 8 around and I always forget how to use a decent lambda, so I hope that Campbell will help us out with this runnable)
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SwingUtilities.invokeLater(() -> new GUIApplication());
 
mitchell bat
Ranch Hand
Posts: 91
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Worked a charm guys! thanks alot!
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've got one more question for now, i want to add in radio buttons. Would it be better practice to create a new class and then call the method from within the MitchellBat class or just add it straight away into the same class...?
 
Piet Souris
Rancher
Posts: 1634
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As with everything, it depends on what you intentions are, but for the simple program so far: just add them to your class.

I advise to use a sepratate JPanel for the radiobuttons, to avoid any clutter. For instance: (as you see, the code gets longer and longer!)
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers guys, worked a charm!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!