?
Solved

How do I overlay a JLabel image with text?

Posted on 2004-09-20
4
Medium Priority
?
1,620 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 6

Accepted Solution

by:
expertmb earned 500 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

Cyber Threats to Small Businesses (Part 1)

This past May, Webroot surveyed more than 600 IT decision-makers at medium-sized companies to see how these small businesses perceived new threats facing their organizations.  Read what Webroot CISO, Gary Hayslip, has to say about the survey in part 1 of this 2-part blog series.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

762 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