I am learning Java and trying to implement a MouseListener for the first time. I have read the java doc
MouseListener but my code doesnt work, as in nothing happens when i press the button. Here is a jbutton with a pressed and released event. Can someone explain where i have gone wrong?
JButton upButton_1 = new JButton("Up");
upButton_1.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent pevt) {
upButtonPressPerformed(pevt);
}
public void mouseReleased(MouseEvent revt) {
upButtonReleasePerformed(revt);
}
public synchronized void upButtonPressPerformed(
MouseEvent pevt) {
resultsTextArea.setText("Up Button Activated, String: " + downString);
try{
//See Above comments for sending ASCII String
byte[] bytes = DatatypeConverter.parseHexBinary(upString);
TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
twoWaySerCom.serialPort.getOutputStream());
sw.out.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
public synchronized void upButtonReleasePerformed(
MouseEvent revt) {
resultsTextArea.setText("Up Button released, String: " + downString);
try{
//See Above comments for sending ASCII String
byte[] bytes = DatatypeConverter.parseHexBinary(upString);
TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
twoWaySerCom.serialPort.getOutputStream());
sw.out.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
});
ActionListener is what you are looking for if you want to work with buttons.
JButton button = new JButton("SomeButton");
button.addActionListener(this);
void ActionPerformed(ActionEvent e) {
if(e.getSource() == button) {
// do whatever you want if button is clicked
}
}
Or you can use anonymous inner class:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//do whatever you want
}
});
//or the Java8 version
button.addActionListener((e) -> {
//do whatever you want
});
Whit MouseListener you can listen to events like:
MouseClicked, MouseEntered, MouseExited, MousePresse, MouseReleased.
You could use these, but for button click its more logical to listen to your buttons not your mouse.
Related
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.
I have problem, i dynamically create the buttons (users write text in JtextArea then is create the new button) and when user clicked in button this text is writing in JtextPane.
I dont know why doing this?
The buttons is created but when user clicked on button doing nothing.
DODAJNOWYButton.addMouseListener(new NewMouseListener(textPane1) {
#Override
public void mouseClicked(MouseEvent e) {
String text = textArea2.getText();
bar.add(new JButton(""+text));
bar.validate();
bar.repaint();
try{
doc.insertString(doc.getLength(),""+text,null);
} catch (BadLocationException e1) {
System.out.println(e);
}
super.mouseClicked(e);
}
});
You can use addClickHandler():
JButton yourButton = new JButton(""+text);
yourButton.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
//do something
}
}
I am creating a virtual piano in Java. So far I have action listeners for two of the keys which work for the most part, just not after one another. For example, I hit q on the keyboard and it presses the c key and plays a c, which is what it's supposed to do. But then I want to hit the d key on the piano by hitting w on the keyboard, and it won't do it if I've already hit the q key.
// c key
JButton btnC3 = new JButton("");
btnC3.addKeyListener(new KeyAdapter()
{
#Override
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_Q)
{
btnC3.doClick();
}
}
});
btnC3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// play c
try
{
keys.playNote(Notes.c3.getValue());
}
catch (InterruptedException e1)
{
e1.printStackTrace();
}
catch (InvalidMidiDataException e2)
{
e2.printStackTrace();
}
}
});
// d key
JButton btnD3 = new JButton("");
btnD3.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_W)
{
btnD3.doClick();
}
}
});
btnD3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// play d
try
{
keys.playNote(Notes.d3.getValue());
}
catch (InterruptedException e1)
{
e1.printStackTrace();
}
catch (InvalidMidiDataException e2)
{
e2.printStackTrace();
}
}
});
btnD3.setBackground(Color.WHITE);
btnD3.setBounds(wKeyWidth*1, 0, wKeyWidth, wKeyHeight);
frame.getContentPane().add(btnD3);
Focus is the problem. It will work when the q is pressed because that button has the focus. It wont work if w is pressed no matter if you press q first because the "q" button has the focus throughout. You should use KeyBindings instead. They work despite whatever component has focus.
Another solution would be to add the q and w button presses to a keylistener added onto the JFrame and use requestFocus() and grabFocus().
But you would need to add implementation for both the keylistener in the JFrame and the button press and release for the button.
Here is the perfect link to see how focus affects how your key presses work in swing
http://www.javaworld.com/article/2076720/core-java/focus-on-swing.html
The problem is related to key board focus, a KeyListener will only generate events when the component the listener is registered to IS focusable and HAS focus.
Obviously, when a button is "clicked" it gains focus, meaning that none of the other components can responds.
The basic answer is, use is the Use Key Bindings API.
There are a number of choices you can make, you can use the current container to register the key bindings against, for example...
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
JButton btnC3 = new JButton("");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), "c3");
am.put("c3", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
btnC3.doClick();
}
});
btnC3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// play c
try {
keys.playNote(Notes.c3.getValue());
} catch (InterruptedException e1) {
e1.printStackTrace();
} catch (InvalidMidiDataException e2) {
e2.printStackTrace();
}
}
});
JButton btnD3 = new JButton("");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), "d3");
am.put("d3", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
btnD3.doClick();
}
});
or you can register them against in the individual components. The choice will mostly come down to how reusable you want the solution to be.
For example, you could create a Action which can be applied to both the JButton and key binding which means you don't need to programmatically click the button.
For example...
public class NoteAction extends AbstractAction {
private Note note;
private Keys keys;
public NoteAction(Note note, Keys keys) {
this.note = note;
this.keys = keys;
}
#Override
public void actionPerformed(ActionEvent e) {
keys.playNote(note.getValue());
}
}
(I don't have your code so I'm just make some of the class names up)
Then you could simply use...
NoteAction noteAction = new NoteAction(Notes.d3, keys);
JButton btnC3 = new JButton(noteAction);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), "c3");
am.put("c3", noteAction);
to set it up.
See How to Use Actions for more details
how can I know in main method that one button is click to continue with the execution because the code doesn't work inside the actionperfomed button.
For example this is my main method
public static void main(String args[]) {
jwindows jw = new jwindows ();
//stop until a button inside the jwindows is clicked
// codeExecuteAfterButtonClick
}
The wait and notify methods is Object exist to handle this sort of situation
final JButton button = new JButton('click to continue');
button.setAction(new AbstractAction() {
public void actionPerformed(ActionEvent ae) {
synchronized (button) {
button.notify();
}
}
});
JFrame jf = new JFrame("Window");
jf.getContentPane().add(button, BorderLayout.CENTER);
jf.setDefaultCloseOperation(...);
jf.pack();
jf.setVisible();
synchronized(button) {
try {
button.wait();
} catch (InterruptedException ex) {
System.out.println("Interrupted");
}
}
System.out.println("After button clicked");
i'm trying a very simple GUI in java.
i've just created a small GUI with buttons and when we click each button, it opens a website.
So i have have 3 buttons:
button1 = gmail
button2 = google
button3 = yahoo
when i click on button1 sometimes it opens gmail or google or yahoo.
The same problem with other button too.
Why?
Here is my very simple code:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Gui extends Frame implements WindowListener,ActionListener {
//TextField text = new TextField(20);
Button a, b, c;
Process p1, p2, p3;
//private int numClicks = 0;
public static void main(String[] args) {
Gui myWindow = new Gui("Miquelon's");
myWindow.setSize(350,100);
myWindow.setVisible(true);
}
public Gui(String title) {
super(title);
setLayout(new FlowLayout());
addWindowListener(this);
a = new Button("Gmail");
b = new Button ("Google");
c = new Button ("Yahooooo");
add(a);
add(b);
add(c);
//add(text);
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
try
{
{
p1 = Runtime.getRuntime().exec("cmd /c start https://mail.google.com");
p2 = Runtime.getRuntime().exec("cmd /c start https://google.com");
p3 = Runtime.getRuntime().exec("cmd /c start https://yahoo.com");
}
}
catch (IOException ex)
{
Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
}
Thank you
Your actionPerformed is running all three. You need to use actionPerformed to determine which button was pressed, and then run the corresponding command.
public void actionPerformed(ActionEvent e)
{
String address = "";
if(e.getSource() == a) address = "https://mail.google.com";
else if(e.getSource() == b) address = "https://google.com";
else if(e.getSource() == c) address = "https://yahoo.com";
else return; // not one of the three buttons, get out!
try
{
// only NEED to store the process if you want to do something with it later
// I just let mine dangle :) it works for me!
Runtime.getRuntime().exec("cmd /c start " + address);
}
catch (IOException ex)
{
Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
}
}
You add the same ActionListener to all three buttons. And in the ACtionListener you open yahoo google and gmail. So what else did you expect?
If you don't differ in your actionPerformed method which button was pressed, then this is the correct behaviour.
There are various possibilities to solve this issue... use a ACtionListener for each button (for example anonymoous)
Or use e.getSource() to determine which button was pressed in the actionPerformed method.
For example:
if(e.getSource().equals(a)) {
address = "https://mail.google.com";
}
You don't identify each button in your actionPerformed event.
Every time the event is executed all three commands are executed
One consider naming your a to gmail so it's more descriptive.
a.addActionListener(new ActionListener() {
void actionPerformed(ActionEvent e) {
p1 = Runtime.getRuntime().exec("cmd /c start https://mail.google.com");
}
});
But in short it's running all three.
If you want to do three different things, you either need three different action listeners (one for each button) which each do one thing, or one action listener (one for all buttons) which makes an attempt to determine which button was pressed and does something based on which button called it.
What you have right now is one action listener that does all three things without regard as to which button was pressed.
You could also try setting the ActionCommand for each button so that they can all use the same event listener. This would also improve maintainability if/when you want to add a new button.
a = new Button("Gmail");
a.setActionCommand( "https://gmail.com" );
b = new Button ("Google");
b.setActionCommand( "https://google.com" );
c = new Button ("Yahooooo");
c.setActionCommand( "https://yahoo.com" );
and reimplement your listener method as such:
public void actionPerformed(ActionEvent e)
{
try
{
{
Runtime.getRuntime().exec("cmd /c start " + e.getActionEvent());
}
}
catch (IOException ex)
{
Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
}
}