Solved

How do I overlay a JLabel image with text?

Posted on 2004-09-20
4
1,553 Views
Last Modified: 2008-01-09
I am extending the JLabel class using the JLabel(ImageIcon imageicon) constructor.
What I would like to do is put text directly in the center of the label, directly overlaying the image itself.

I have tried code like the following, which does not seem to work:

public void paint(Graphics g)
{
  super.paint(g);
  g.drawString("some text", 5, 5);
}

the JLabel itself is about 20 x 20 so I would think that I would at least see something. Thanks in advance for any help.
0
Comment
Question by:darkpegasus5
  • 3
4 Comments
 
LVL 6

Accepted Solution

by:
expertmb earned 125 total points
ID: 12109049
class myLabel extends JLabel{
      Image image;
      String text;
      public myLabel(String text, Image image) {
              this.image = image;
              this.text = text;
          }

          public void paintComponent(Graphics g) {
              super.paintComponent(g);  // Paint background

              // Draw image at its natural size first.
              g.drawImage(image, 25, 10, this);
                  g.drawString("mystring", 15,28) ;
          }


}
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12109054
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

import java.util.StringTokenizer;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

  class labelTest extends JDialog implements ActionListener{

      JTextField txtField = new JTextField();
      JButton btnOk = new JButton("Ok");
      int lineNumber = 0;
    // Fetch icon
    ImageIcon icon = new ImageIcon("c:\\somegif.gif");

    // Create a label with text and an icon; the icon appears to the left of the text
    JLabel label = new JLabel("Text Label", icon, JLabel.CENTER);

      public labelTest(Dialog dlg, boolean modal){


            JLabel label = new myLabel("Text Label", icon.getImage()); //, JLabel.TOP)


            btnOk.setName("Ok");
            //this.getContentPane().add(txtField, BorderLayout.CENTER);
            this.getContentPane().add(label, BorderLayout.CENTER);
            this.getContentPane().add(btnOk, BorderLayout.SOUTH);
            btnOk.addActionListener(this);
            this.setTitle("Select Line no.");
            this.setSize(200, 100);
            this.setModal(modal);
        this.show();
      }

      public void actionPerformed(ActionEvent event){
            Object obj = event.getSource();

            if(obj instanceof JButton){
                  if(((JButton)obj).getName().equalsIgnoreCase("Ok")){
                        String str = txtField.getText();

                        if(str != null && ! str.equals("")){
                              try{
                                    lineNumber = Integer.parseInt(str);
                              }catch(Exception ex){
                                    lineNumber = 0;
                              }
                        }
                        this.setVisible(false);
                        System.exit(0);
                  }
            }
      }

      int  getLineNumber(){
            return lineNumber;
      }

      public static void main(String[] s){
            new labelTest(null, true);
      }
}

class myLabel extends JLabel{
      Image image;
      String text;
      public myLabel(String text, Image image) {
              this.image = image;
              this.text = text;
          }

          public void paintComponent(Graphics g) {
              super.paintComponent(g);  // Paint background

              // Draw image at its natural size first.
              g.drawImage(image, 25, 10, this);
                  g.drawString("mystring", 15,28) ;
          }


}
0
 
LVL 1

Author Comment

by:darkpegasus5
ID: 12110967
That did it, thanks!
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12110971
:-)
mb...
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question