Put icon inside jLabel in a jTextField

2

How do I change the way the date is inserted into my jTextField ?

What I have is the following:

IwanttoinsertthisiconthatisinjLabelwithinmyjTextField.Isthispossible?

Update:

importjava.awt.*;importjavax.swing.*;classTestingextendsJFrame{publicTesting(){setDefaultCloseOperation(EXIT_ON_CLOSE);JPanelp=newJPanel(newBorderLayout());JTextFieldtf=newJTextField(5);JLabellabel=newJLabel(newImageIcon("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 get the icon that I want in 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));  

so the image does not appear in my 'test' textField. 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.

    
asked by anonymous 13.11.2014 / 12:25

1 answer

4

Your image does not appear because you set label.setOpaque(true); and this is covering it!

Tip:

I have already implemented the click event for you, now you just have to manipulate it to edit JTextField according to the form you will use to capture the user 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) {}
}

Afterthemouseevent:

I hope I have helped! Any questions, I'm available.

    
13.11.2014 / 16:49