Solved

How do I overlay a JLabel image with text?

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
stackato and cloud 4 85
windows explorer path to command prompt 5 43
javap not working 8 44
Adding multiple JVM environments to RedHat 6 7 18
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

786 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