Double-click events in Java Swing

Source: Internet
Author: User
Tags date exit
Today, I wrote a small program encountered this problem, in the online search for someone else's ideas, sorting out the code can run, feel good!
A new application is created directly in JBuilder, as the specific steps do not describe, this code does not run directly without the main function. Below I post the code in FRAME1:
Import java.awt.*;
Import java.awt.event.*;
Import javax.swing.*;
Import com.borland.jbcl.layout.*;
Import Java.util.Date;

public class Frame1
Extends JFrame {
JPanel ContentPane;
BorderLayout borderLayout1 = new BorderLayout ();
JPanel JPanel1 = new JPanel ();
Xylayout xYLayout1 = new Xylayout ();
JScrollPane jScrollPane1 = new JScrollPane ();
JList jList1 = new JList ();
The data in the initialized JList
String[] Strdata = {
"One", "Tow", "Three"};
Time to save the click button
Long clicktime = 0;

Construct the Frame
Public Frame1 () {
EnableEvents (Awtevent.window_event_mask);
try {
Jbinit ();
}
catch (Exception e) {
E.printstacktrace ();
}
}

Component initialization
private void Jbinit () throws Exception {
ContentPane = (JPanel) this.getcontentpane ();
Contentpane.setlayout (BORDERLAYOUT1);
This.setsize (New Dimension (532, 468));
This.settitle ("Frame Title");

Jpanel1.setlayout (XYLAYOUT1);
Jlist1.addmouselistener (This) (new Frame1_jlist1_mouseadapter);
Contentpane.add (JPanel1, Borderlayout.center);
Jpanel1.add (JScrollPane1, New Xyconstraints (18, 34, 209, 326));
Jscrollpane1.getviewport (). Add (JList1, NULL);
Jlist1.setlistdata (strdata);
}

Overridden so we can exit as window is closed
protected void processWindowEvent (WindowEvent e) {
Super.processwindowevent (e);
if (e.getid () = = windowevent.window_closing) {
System.exit (0);
}
}

The implementation of the button to double-click the function of the method, very simple algorithm, do not explain the
public Boolean checkclicktime () {
Long Nowtime = (new Date ()). GetTime ();

if ((Nowtime-clicktime) < 300) {
Clicktime = Nowtime;
return true;
}
Clicktime = Nowtime;
return false;

}

void jlist1_mousereleased (MouseEvent e) {
To decide whether to double-click, is to write the function you want to implement
if (Checkclicktime ()) {
System.out.println ("click Double");

}
}
}

Class Frame1_jlist1_mouseadapter
Extends Java.awt.event.MouseAdapter {
Frame1 adaptee;

Frame1_jlist1_mouseadapter (Frame1 adaptee) {
This.adaptee = Adaptee;
}

public void mousereleased (MouseEvent e) {
Adaptee.jlist1_mousereleased (e);
}
}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.