0

I am trying to get a JTextField to show over a JButton when I click the button. I have that working, but when I click out of the button it still stays visible. I'm using a MouseListener event so once I exit the button I want JTextField to become transparent again, but it stays visible.

My code:

import java.awt.EventQueue;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;


public class magicalJtextField extends JFrame implements MouseListener{

private JPanel contentPane;
private JTextField textField;


public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                magicalJtextField frame = new magicalJtextField();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


public magicalJtextField() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);


    textField = new JTextField();
    textField.setBounds(78, 78, 89, 30);
    contentPane.add(textField);
    textField.setColumns(10);

    JButton button = new JButton("");
    //button transparent
//      button.setOpaque(false);
//      button.setContentAreaFilled(false);
//      button.setBorderPainted(false);
    button.setBounds(78, 78, 89, 23);
    button.addMouseListener(this);
    contentPane.add(button);

    textField.setVisible(false);

}
public void mouseEntered(MouseEvent e) 
{
//button.setText("Mouse Entered");
//button.setBackground(Color.CYAN);
//  textField.setVisible(true);
}

public void mouseExited(MouseEvent e) 
{
    textField.setVisible(false);    
}   
public void mouseClicked(MouseEvent e) 
{
    textField.setVisible(true);
}
public void mousePressed(MouseEvent e) 
{
    textField.setVisible(true);
}   
public void mouseReleased(MouseEvent e)
{
    textField.setVisible(true);
}
}
user1803551
  • 12,965
  • 5
  • 47
  • 74
javajoejuan
  • 323
  • 3
  • 9
  • 3
    Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Feb 22 '16 at 06:07
  • Try calling `repaint` – MadProgrammer Feb 22 '16 at 09:36
  • What do you mean by "*click out of the button*"? Loose focus on the text field? Submit the text in the text field? – user1803551 Feb 22 '16 at 23:39

2 Answers2

1

Use an ActionListener to react on the button click: If you receive a click, make the button invisible and the textField visible.

Then attach the MouseListener to the textField and not the button and only implement mouseExited (all others empty). When you receive this event make the textField invisible and the button visible again.

wero
  • 32,544
  • 3
  • 59
  • 84
  • 2
    *"If you receive a click, make the button invisible and the textField visible."* Perfect for a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Feb 22 '16 at 06:06
  • 2
    *"Then attach the MouseListener.."* Uggh.. what about users who prefer using the keyboard? Seems a focus listener would be better there. – Andrew Thompson Feb 22 '16 at 06:07
  • @AndrewThompson obviously the OP is trying to achieve some effects. Of course there are more efficient ways to do this (e.g. CardLayout). And the GUI is not very usable. But it is still make sense to try such things when you are a learner. – wero Feb 22 '16 at 06:10
  • 2
    *"But it is still make sense to try such things when you are a learner."* A better thing for a newbie to learn is how to make a **usable GUI.** This kind of nonsense is better left to experts with too much time on their hands (and even then it's usually a bad idea - just something they need to 'get out of their system'). – Andrew Thompson Feb 22 '16 at 06:24
1

I suggest a CardLayout for the Jbutton-JTextField magic trick (edit: I actually saw the recommendations in the comments only after I posted because it was so obvious and on an answer). Pressing the button will switch the card and then exiting the text field area with the mouse will switch it again.

enter image description here

public class Example extends JPanel {

    public static void main(String[] args) {

        EventQueue.invokeLater(() -> {
            JFrame frame = new JFrame();
            frame.add(new Example());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        });
    }

    public Example() {

        CardLayout cards = new CardLayout(5, 5);
        JPanel panel = new JPanel(cards);

        JButton button = new JButton("");
        JTextField textField = new JTextField(10);

        button.addActionListener(e -> {
            cards.next(panel);
            textField.requestFocusInWindow();
        });

        textField.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseExited(MouseEvent e) {

                cards.next(panel);
            }
        });

        panel.add(button);
        panel.add(textField);

        add(panel);
    }
}

As you were told by Andrew Thompson, don't use null layouts and don't specify bounds. Use a proper layout manager to do this for you.

Community
  • 1
  • 1
user1803551
  • 12,965
  • 5
  • 47
  • 74