How to edit JLabel Text String at run time?

Dear friends.

I google quite a while, also search this excellent forum, I need to edit JLabel's Text String at run time, like just right click or left click mouse, then I can edit/update JLabel's Text String, then persist my updating forever untill next I change them.

Any guru can throw some light or gave some good example?

Thanks

Sunny

[371 byte] By [sunnymanmana] at [2007-11-27 1:22:42]
# 1
Use a JTextField. When you click on the text field you make it editable. When you finish typing you make it non-editable.
camickra at 2007-7-12 0:10:12 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks, still confused,

My Jable JL has two parts:

Upper part: Icon

Lower Part: TextString

so I need to put TextField in lower part so that I can edit at my will,

see code below

import javax.swing.*;

import java.awt.*;

public class JTextFieldTest extends JFrame {

public JTextFieldTest() {

super("JTextField Test");

getContentPane().setLayout(new FlowLayout());

JLabel jl = new JLabel();

jl.setPreferredSize(new Dimension(300, 200));

JTextField textField1 = new JTextField("1", 1);

JTextField textField2 = new JTextField("22", 2);

JTextField textField3 = new JTextField("333", 3);

jl.setLayout(new FlowLayout());

jl.add(textField1);

jl.add(textField2);

jl.add(textField3);

getContentPane().add(jl);

setSize(300, 200);

setVisible(true);

}

public static void main(String argv[]) {

new JTextFieldTest();

}

}

Is it correct?

But I hope to remove bevel in TextField so that it looks like a pane untill I click it the TextField will appear.

thanks for advice

sunny

sunnymanmana at 2007-7-12 0:10:12 > top of Java-index,Desktop,Core GUI APIs...
# 3
> Upper part: Icon> Lower Part: TextString> so I need to put TextField in lower part so that I can edit at my will,draw your icon in the textField's border (top)
Michael_Dunna at 2007-7-12 0:10:12 > top of Java-index,Desktop,Core GUI APIs...
# 4

Michael: can you advice more details? some code maybe helpful.

see my code below,

import javax.swing.*;

import java.awt.*;

public class JTextFieldTest extends JFrame {

public JTextFieldTest() {

super("JTextField Test");

getContentPane().setLayout(new FlowLayout());

JLabel jl = new JLabel();

jl.setPreferredSize(new Dimension(300, 400));

JTextField textField3 = new JTextField("This is a fat Pig", 3);

ImageIcon pigIcon = createImageIcon("Pig.gif","a cute pig");

jl.setLayout(new BorderLayout());

jl.add(textField3,BorderLayout.SOUTH);

jl.setIcon(pigIcon);

getContentPane().add(jl);

setSize(300, 400);

setVisible(true);

}

/** Returns an ImageIcon, or null if the path was invalid. */

protected static ImageIcon createImageIcon(String path,

String description) {

java.net.URL imgURL = TextSamplerDemoNew.class.getResource(path);

if (imgURL != null) {

return new ImageIcon(imgURL, description);

} else {

System.err.println("Couldn't find file: " + path);

return null;

}

}

public static void main(String argv[]) {

new JTextFieldTest();

}

}

Here I can put Icon in top and TextField at bottom, but TextField looks not so good, I hope it display like a normal label, not really TextField until I click it.

any idea?

Thanks

sunny

sunnymanmana at 2007-7-12 0:10:12 > top of Java-index,Desktop,Core GUI APIs...
# 5

here's something to play around with

import javax.swing.*;

import java.awt.*;

class Testing

{

Image img = null;

public void buildGUI()

{

String imageFile = "test.gif";

try

{

img = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource(imageFile), imageFile));

}

catch(Exception e){}

JTextField tf = new JTextField(5){

protected void paintComponent(Graphics g) {

super.paintComponent(g);

if(img != null) g.drawImage(img,0,0,this);

}

};

if(img != null) tf.setMargin(new Insets(img.getHeight(tf),0,0,0));

JFrame f = new JFrame();

f.getContentPane().add(tf);

f.pack();

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

Michael_Dunna at 2007-7-12 0:10:12 > top of Java-index,Desktop,Core GUI APIs...