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

string concatenation

Mike Brown
Greenhorn

Joined: Oct 19, 2003
Posts: 25
I need to take the input from two textboxes and display them with the paint
method. I got it working with the first textbox but how do I add a second
and put it together to be displayed by the paint method? Am I getting close
with this code. Eventualy it will display a first name, last name and emailaddress from three textboxes after the user pushes the button. Here is a
piece of my code where I am having the problem.
Thanks, Mike
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == btn)
{
Graphics g = getGraphics();
g.drawString(txtFirst.getText() + "" + txtLast.getText(), 10,190);
}
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Mike,
I've explained to you before, in other threads, why this approach (calling getGraphics() in actionPerformed() and painting things there) generally doesn't work, and I've also explained about adding a separate JPanel to paint on and how to override paintComponent() and use member variables to hold the state regarding what paintComponent() should paint. I'm disappointed that you're choosing to ignore that advice -- I don't think you'll find anyone here who wouldn't back me up in saying that what you're trying to do is simply wrong, and the way I explained to you is right.
But putting all of that aside for a moment: if calling drawString() with the result of a single getText() call works, then calling it with the result of adding several getText() return values together would work just as well. You don't say what thye specific problem you're having now is.


[Jess in Action][AskingGoodQuestions]
Mike Brown
Greenhorn

Joined: Oct 19, 2003
Posts: 25
Ernest,
I am sorry that I seem to not be taking your advice. It is just that some of the things you have explained are a bit beyond my level at this point. I will try to do as you say but it will lead to more questions. (If you don't mind) I do intend to learn to do it the proper way as you have explained and I have printed out all of you responses so that I can have them for a reference. Please don't think your time is wasted I am listening and taking it all in and I sincerely appreciate all of your help. This site has been my best resource to date.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Striving for something that's just beyond your reach is good, but struggling to put together code which works but which you don't understand is never a good idea; you just end up fooling yourself regarding how much you really know.
OK, so, let's begin at the beginning.

Read the code, add the necessary import statements, compile this baby, and run it (please do this before you read any further -- seriously!) Notice how the painted string is visible when you cover and expose the window, when you resize the window, etc. That's what you're after, and that's what the "paint in actionPerformed()" approach can't do for you.
Then, you just want to be able to change the string that's displayed based on an event. OK, that's easy, too.


Now, when you press the button, the text changes.
We want to get you to the point where you really understand the second program. Ask whatever questions you need to. When you get that that point, you'll be able to write your textfield program with confidence, no help from us.
Mike Brown
Greenhorn

Joined: Oct 19, 2003
Posts: 25
I can compile the first part of the code but when I run it I get an applet with the message Exception: java.lang.ClassCastException:MyPanel.
Here is my code and my html
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("Hello, World", 10, 50);
}
public static void main(String[] argv) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MyPanel(), BorderLayout.CENTER);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
<html>
<applet code = "MyPanel.class"
width = 400
height = 400
</applet>
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

It's not an applet, it's an application; just run
java MyPanel
Making it into an applet would make things just a little bit more complicated; we can upgrade this to an applet later, but it'll actually be simpler to understand as a standalone application first.
Mike Brown
Greenhorn

Joined: Oct 19, 2003
Posts: 25
Thank you for your patience Ernest. This is very diferent from the way that I have been attempting to do this as you know. Here are my questions regarding these things I havemt yet encountered.
1. super.paintComponent(g); //I am not sure what super is
2. theString = stringToDisplay;//I know that strings are immutable so how are we changing the contents of the string or are we?
3.How do we know when the user clicks the button with no actionPerformed method?
4. What are we doing with the last three lines? Is it to reset the frame?
As you can see I am still very green,but eager to learn. I have quite a few Java books but sometimes I feel that I am skipping around too much to try and find things that I am missing the bigger picture. I will look in my books to try to answer some of my own questions. I will laso experiment with the code you sent me. That sometimes helps me to see the importance of things. Thank you
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34


1. super.paintComponent(g); //I am not sure what super is

When you extend a class and override (redefine) one of its methods, super lets you call the original version of the method from the new version. Here, we're redefining paintComponent(). The version in JPanel already does something useful (mainly paint a plain, grey area of the correct size) so we invoke it using "super" before adding in our own code.

2. theString = stringToDisplay;//I know that strings are immutable so how are we changing the contents of the string or are we?

String objects are indeed immutable. We're not changing a String object, though. We've got a String variable which starts out referring to one String object, and then it changed to point to another one. Java variables are like "handles" or "pointers" to objects -- they are not the objects themselves. You might want to read this and then especially this.

3.How do we know when the user clicks the button with no actionPerformed method?

There is an actionPerformed method, although I put it into an "anonymous inner class" rather than into the MyPanel class. I probably shouldn't have done that as it's just one more thing to learn. You could rewrite the code like this:


4. What are we doing with the last three lines? Is it to reset the frame?

Not sure what last three lines you mean; do you mean the last two lines? When you create a JFrame object, its size is 0x0, and it's invisible. You have to tell it what size to be with setSize(), and show it with setVisible(true).
Hope this helps.
Mike Brown
Greenhorn

Joined: Oct 19, 2003
Posts: 25
Ernest,
One more thing on this and I will leave it alone. When I was trying to call getGraphics() in actionPerformed() and paint things frome there, It would work, but like you said any resize or overlap from another window would erase it. Why is that? If you could explain this when you have the time it would help me. thanks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

When the window needs to be repainted (when its resized or covered and exposed, etc) Java calls paintComponent(). The built-in version of paintComponent() for frames and panels and things just paints a blank rectangle. This would have the effect of erasing any other painting you've done. If you do it in paintComponent(), though, then your image gets repainted whenever necessary.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string concatenation