Hand Cursor when hover on a button in Java AWT - java

I've created a button in AWT with the name "Reset". I want the cursor to be hand cursor when the mouse is hovered on this button.
I tried the mouseEntered method of the MouseAdapter class but no effect.
void createResetButton() {
Button resetButton = new Button("Reset");
resetButton.setBounds(300, 335, 100, 40);
add(resetButton);
resetButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
usernameTextField.setText(null);
passwordTextField.setText(null);
invalidMessage.setVisible(false);
}
#Override
public void mouseEntered(MouseEvent e) {
Cursor.getPredefinedCursor(HAND_CURSOR);
}
});
}
Thanks in advance.

Your statement Cursor.getPredefinedCursor(HAND_CURSOR);
in your mouseEntered method had no effect,
because you only got the cursor, but then did nothing with it.
The solution is simpler than you might have expected.
You don't need your mouseEntered method.
Just use the setCursor(Cursor) method of class Componenton your resetButton.
void createResetButton() {
Button resetButton = new Button("Reset");
resetButton.setBounds(300, 335, 100, 40);
add(resetButton);
resetButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
resetButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
usernameTextField.setText(null);
passwordTextField.setText(null);
invalidMessage.setVisible(false);
}
});
}
Then AWT will do the rest for you: showing the hand cursor when the mouse
enters the resetButton, and showing the normal cursor when leaving it.

I got this done this way after few hits and trials:
void createResetButton() {
Button resetButton = new Button("Reset");
resetButton.setBounds(300, 335, 100, 40);
add(resetButton);
resetButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
usernameTextField.setText(null);
passwordTextField.setText(null);
invalidMessage.setVisible(false);
}
#Override
public void mouseEntered(MouseEvent e) {
resetButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
});
}

Related

How to open new window by clicking label in java?

I want to open a new window when I click the label. By implementing a key pressed listener. But it is not working. Even nothing is happening.
JLabel lblNewLabel_2 = new JLabel("Create New Account!!!!");
lblNewLabel_2.setForeground(Color.GREEN);
lblNewLabel_2.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
Dosomething();
}
});
private void Dosomething() {
hide();
Account account=new Account();
account.visible();
}
protected static void hide() {
frame.hide();
}
You can use JButton as others say and remove its border to make it look like JLabel but if you really want to use JLabel and detect clicks I think you should write a MouseAdapter for it.
JLabel fooLabel = new JLabel("foo");
fooLabel.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
doSomething();
}
}
);
You can also override mouseClicked method but in that case the user must not move the cursor while clicking.

How to create a shortcut key for a JLabel?

enter image description here
I have a list JLabel. I want when click a label content display in JTextArea the same. Why when I click the label, the text area does not display?
The code:
jLabel0.setText(namelist.get(0));
jLabel1.setText(namelist.get(1));
jLabel2.setText(namelist.get(2));
jLabel3.setText(namelist.get(3));
jLabel4.setText(namelist.get(4));
jLabel5.setText(namelist.get(5));
//String b[]={"jLabel4","jLabel5","jLabel7","jLabel8","jLabel9","jLabel10"};
for (int i=0;i<k;i++){
String f=String.valueOf(i);
JLabel jlb = new JLabel("jLabel"+f);
String Af=file_list.get(i);
FileReader F=new FileReader(Af);
jlb.addMouseListener(new MouseListener(){
public void mouseReleased(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==1)
{
try {
jTextArea3.read(F,"");
} catch (IOException ex) {
Logger.getLogger(FAKENEWS.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
}
You can simply achieve it using JButton and just by making the button look like a label.
You will want to do the following once you have created the button:
setFocusPainted(false);
setMargin(new Insets(0, 0, 0, 0));
setContentAreaFilled(false);
setBorderPainted(false);
setOpaque(false);
You may want to exclude setFocusPainted(false) if you want it to actually paint the focus (e.g. dotted line border on Windows look and feel).
And after that you may use the button event handlers to do your desired action.

A JList rolls down when a JButton is clicked ? JAVA

I basically want to make a JList (whatToSearch) roll-down, or simply show its content, for selection, once a JButton (popDownButton) is clicked.
//SEARCH OPTIONS
popDownButton = new JButton(new ImageIcon(new ImageIcon("downArrow.png").getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT)));
whatToSearch = new JList(elementsToSearch);
whatToSearch.setVisibleRowCount(3);
whatToSearch.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scroll = new JScrollPane(whatToSearch);
popDownButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(null, scroll.getViewport());
}
});
add(popDownButton);
This bit of code works, but I'm looking for the content of the JList to be shown in the same interface, next to the button, rather than in another pop-up interface.
You can try this code, it's really easy:
popDownButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
JScrollBar vertical = scroll.getVerticalScrollBar();
vertical.setValue(vertical.getMaximum());
}
});
More ways: Scroll JScrollPane to bottom
Hope this helps:
popDownButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
whatToSearch.setSelectedIndex(elementsToSearch.getSize() - 1);
whatToSearch.ensureIndexIsVisible(elementsToSearch.getSize() - 1);
}
});

how to impliment mouse listener

Kind of a noob question, but then again, I am a noob. I'm trying to implement a sort of "universal" mouse listener. That is, when I click any of the objects on screen, it runs a specific amount of code. I have the current solution below, but the code I want to run is the same for 10 different objects, so this gets rather tedious.
difference2 = new JLabel(new ImageIcon("transparent.png"));
difference2.setBounds(645,490,10,10); //left, top, width, height
contentPane.add(difference2);
difference2.setVisible(true);
difference2.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e) {
//code
}
});
I am aware I can create a separate method such as the following
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null,"this would be nice");
}
But I can't figure out how to set up a mouse listener on every object for it. The JOptionPane currently does nothing.
I might have misread your question, but if you want the same mouselistener on various objects,you could store the instance of your listener in a variable once and then add it to whatever gui object you want it added to.
MouseListener ml = new MouseListener() {
#Override
public void mouseReleased(MouseEvent e) {//code}
#Override
public void mousePressed(MouseEvent e) {//code}
#Override
public void mouseExited(MouseEvent e) {//code}
#Override
public void mouseEntered(MouseEvent e) {//code}
#Override
public void mouseClicked(MouseEvent e) {//code}
};
JLabel j1 = new JLabel("Label1");
j1.addMouseListener(ml);
JLabel j2 = new JLabel("Label2");
j2.addMouseListener(ml);
You can create an instance of an anonymous class that extends MouseAdapter and assign it to a variable that you can reuse (myMouseListener in this case):
MouseListener myMouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null,"this would be nice");
}
};
difference2.addMouseListener(myMouseListener);
aSecondObject.addMouseListener(myMouseListener);
aThirdObject.addMouseListener(myMouseListener);
...

Change JButton's click color?

I've created some swing applications involving JButtons, and noticed whenever one is clicked, it turns white. Example here.
How would I change it so when, and only when, the button is clicked, it turns RED instead of the usual white, and when it is released, it goes back to its normal look? Is there a method for this?
Example code:
JButton b = new JButton("foo");
b.addMouseListener(new MouseAdapter(){
#Override
public void mousePressed(MouseEvent e) {
//turn red
}
#Override
public void mouseReleased(MouseEvent e) {
//go back to original state
}
});
change color of button text using setForeground method
like this
#Override
public void mousePressed(MouseEvent e) {
b.setForeground(Color.red); // button text color
// b.setBackground(Color.red); // button background color
}
#Override
public void mouseReleased(MouseEvent e) {
b.setForeground(Color.black); // button text color
}
JButton b = new JButton("foo");
b.addMouseListener(new MouseAdapter(){
#Override
public void mousePressed(MouseEvent e) {
b.setBackground(Color.red);
}
#Override
public void mouseReleased(MouseEvent e) {
//go back to original state
}
});
For more details look at this example

Categories

Resources