Solved

How do I overlay a JLabel image with text?

Posted on 2004-09-20
4
1,486 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now