Place icon inside jLabel on jTextField

Asked

Viewed 1,459 times

2

How to change the date insertion mode in my jTextField?

Here’s what I got:

SS

I want to insert this icon that is in jLabel inside my jTextField. Is this possible?

Upgrade:

    import java.awt.*;  
import javax.swing.*;  
class Testing extends JFrame  
{  
  public Testing()  
  {  
    setDefaultCloseOperation(EXIT_ON_CLOSE);  
    JPanel p = new JPanel(new BorderLayout());  
    JTextField tf = new JTextField(5);  
    JLabel label = new JLabel(new ImageIcon("LogoIcon.png"));  
    label.setOpaque(true);  
    label.setBackground(tf.getBackground());  
    label.setPreferredSize(new Dimension(label.getPreferredSize().width,tf.getPreferredSize().height));  
    p.setBorder(tf.getBorder());  
    tf.setBorder(null);  
    p.add(label,BorderLayout.WEST);  
    p.add(tf,BorderLayout.CENTER);  
    JPanel p1 = new JPanel();  
    p1.add(p);  
    getContentPane().add(p1);  
    pack();  
    setLocationRelativeTo(null);  
  }  
  public static void main(String[] args){new Testing().setVisible(true);}  
}  

This code is functional and I can put the icon I want in the textfield. Now I already have my jformattedTextField and my label with the icon and tried the following:

jLabel31.setOpaque(true);  
jLabel31.setBackground(teste.getBackground());  
jLabel31.setPreferredSize(new Dimension(jLabel31.getPreferredSize().width,teste.getPreferredSize().height));  

this way the image does not appear in my textField 'test'. Any suggestions?

Update1:

public ConfEmpresa() throws SQLException {
...
        Testing1 teste = new Testing1();
        teste.setVisible(true);


    }

class Testing1 extends JFrame implements MouseListener {

    public Testing1() {


        JPanel jp = new JPanel();
        //Border border = BorderFactory.createLineBorder(Color.GRAY, 1);
        //jp.setBorder(border);
        //jp.setBackground(Color.WHITE);
        jp.addMouseListener(this);

        //nomeC = new JTextField(10);
        nomeC.setEditable(false);
        nomeC.setText("sdfasdf");
        //nomeC.setBorder(null);
        //tf.setBackground(Color.WHITE);
        nomeC.addMouseListener(this);

        JLabel lb = new JLabel(new ImageIcon("LogoIcon.png"));
        lb.addMouseListener(this);

        //jp.add(nomeC);
        jp.add(lb);
        jPanel3.add(jp);
        pack();
    }

The mouseclicked part is already as I want. Continue to open two windows and the icon does not appear in the field.

  • This icon has some functionality implemented or is just an indication that this is the date field?

  • 1

    This icon opens the jcalendar to choose the date.

  • From a read on that topical

  • https://www.youtube.com/watch?v=RA6don86wq0 Check out this video to see if it helps you, more precisely from minute 3...

  • 2

    Thank you both for the tips. Danilooliveira I saw the code you put and it’s functional, now I wanted to adapt it to mine. I’ll edit my question and see if anyone can help me adjust to what I have. jsantos1991 Thanks for the video, no doubt it can help me a lot because the goal is to click on the day, select the date right away, and in jCalendar this seems to me to be more complex to do. Let’s see. Thank you

1 answer

4

Your image does not appear because you set label.setOpaque(true); and that’s covering her up!

Suggestion:

I already implemented the click event for you, now just manipulate it to edit the JTextField according to the way it will use to capture the user’s date.

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

class Testing extends JFrame implements MouseListener {

    JTextField tf;

    public Testing() {
        super("Exemplo");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(200, 75);

        JPanel jp = new JPanel();
        Border border = BorderFactory.createLineBorder(Color.GRAY, 1);
        jp.setBorder(border);
        jp.setBackground(Color.WHITE);
        jp.addMouseListener(this);

        tf = new JTextField(10);
        tf.setEditable(false);
        tf.setText("___/___/______");
        tf.setBorder(null);
        tf.setBackground(Color.WHITE);
        tf.addMouseListener(this);

        JLabel lb = new JLabel(new ImageIcon("/home/anderson/Downloads/icon-calendar.png"));
        lb.addMouseListener(this);

        jp.add(tf);
        jp.add(lb);
        add(jp);
        pack();
    }

    public void mouseClicked(MouseEvent e) {
        System.out.println("Manipule a data aqui!");
        tf.setText("13/11/2014");
    }

    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
}

inserir a descrição da imagem aqui

After the mouse event:

inserir a descrição da imagem aqui

I hope I’ve helped! Any questions, I’m available.

  • Thanks for helping out. The main question I’m still uncomfortable with is this: In this example, you create textField and all that. But I already have my design set and the fields there. I can’t just take the names of the Abels and Textfields I have and adapt them to the code?

  • You can. Use the example above as a model that will work! Remember to implement Mouselistener and create your methods...

  • And it is necessary to be creating in my code, a panel, as you created there in this example, that already tested and worked perfectly, I already have the design done?

  • Yes. Create the dashboard and place your components inside it, just like I did. Then add the dashboard to your layout

  • Maybe my problem is right there, is that I’m not adding the panel to my layout. It’s just that when I run the code, it opens up my layout, plus your ! I just want to adapt to my code :s I’m really layy about this -.- Looking at my initial question, you can see a textField called 'nomeC' and the label that has the icon is 'jLabel33'. How can I adapt your example to these fields that I already have?

  • Your problem must be in the add() method; Use youJPanel_ou_Layout.add(meuJPanel);

  • I already managed to put the field I want clickable as in your code, but you are not putting the image in the field -.- and continue to open two windows, I will edit the question to see how you are.

  • I ran your code snippet and it worked! This jPanel3 is where all the other components of your program are added?

  • Yes that’s where all the components are. And you got the image in the field ?

  • Yes, I just decoded the lines, changed the image path and changed Tf.setBackground(...) to namC.setBackground(...);

  • I tested now uncomment the lines of my code snippet and it didn’t work. Anyway I appreciate the availability to help :)

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.