Java Swing jlist usage examples

Source: Internet
Author: User

package so;

Import java.text.*;
Import java.util.*;
Import java.util.ArrayList;

Import java.awt.*;
Import java.awt.event.*;
Import javax.swing.*;
Import javax.swing.event.*;

Import util.*;
Import Com.borland.jbcl.layout.XYLayout;
Import com.borland.jbcl.layout.XYConstraints;

/**
* <p>title: </p>
* <p>description: </p>
* <p>copyright:copyright (c) 2006</p>
* <p>company: </p>
* @author by Samzheng
* @version 1.0
*/

Public class Pmmshipingspace
    extends JFrame {
  int cycle = 0;
  Private Boolean trigtable = true;
  JPanel ContentPane;
  JPanel jpnlTop2 = new JPanel ();
  Xylayout xYLayout1 = new Xylayout ();
  JPanel jPanel1 = new JPanel ();
  BorderLayout borderLayout1 = new BorderLayout ();
  BorderLayout borderLayout2 = new BorderLayout ();
  Buttongroup buttongroup = new Buttongroup ();
  Defaultlistmodel listMode1 = new Defaultlistmodel ();
  JList jList1 = new JList (LISTMODE1);
  JScrollPane jscrollList1 = new JScrollPane (JLIST1);
  JButton jButton1 = new JButton ();
  JButton jButton2 = new JButton ();
  JButton JButton3 = new JButton ();
  JButton jButton4 = new JButton ();
  Defaultlistmodel listMode2 = new Defaultlistmodel ();
  JList jList2 = new JList (LISTMODE2);
  JScrollPane jscrollList2 = new JScrollPane (JLIST2);

Public Pmmshipingspace () {
try {
Jbinit ();
This.setlocation (Toolkit.getdefaulttoolkit (). Getscreensize (). Width-this.getwidth ())/2,
(Toolkit.getdefaulttoolkit (). Getscreensize (). Height-this.getheight ())/2);
}
catch (Exception Exception) {
Exception.printstacktrace ();
}
}

private void Jbinit () throws Exception {
Getcontentpane (). setlayout (BORDERLAYOUT1);
ContentPane = (JPanel) getcontentpane ();
Contentpane.setlayout (BORDERLAYOUT2);
Jpnltop2.setlayout (XYLAYOUT1); //
Jpanel1.setlayout (BORDERLAYOUT1);
Jpanel1.setmaximumsize (New Dimension (32767, 32767));
Jpanel1.setminimumsize (New Dimension (706, 350));
Jpanel1.setpreferredsize (New Dimension (700, 220));
Jpnltop2.setminimumsize (New Dimension (690, 350));
Jpnltop2.setpreferredsize (New Dimension (130, 160));
Contentpane.setminimumsize (New Dimension (706, 662));
Jbutton1.setmaximumsize (New Dimension (49, 25));
Jbutton1.setminimumsize (New Dimension (49, 25));
Jbutton1.setpreferredsize (New Dimension (57, 25));
Jbutton1.settext ("->");
Jbutton1.addactionlistener (New Java.awt.event.ActionListener () {
public void actionperformed (ActionEvent e) {
Jbutton1_actionperformed (e);
}
});
Jbutton2.setpreferredsize (New Dimension (57, 25));
Jbutton2.settext ("->>");
Jbutton2.addactionlistener (New Java.awt.event.ActionListener () {
public void actionperformed (ActionEvent e) {
Jbutton2_actionperformed (e);
}
});
Jbutton3.setmaximumsize (New Dimension (49, 25));
Jbutton3.setminimumsize (New Dimension (49, 25));
Jbutton3.setpreferredsize (New Dimension (57, 25));
Jbutton3.settext ("<-");
Jbutton3.addactionlistener (New Java.awt.event.ActionListener () {
public void actionperformed (ActionEvent e) {
Jbutton3_actionperformed (e);
}
});
Jbutton4.setpreferredsize (New Dimension (57, 25));
Jbutton4.sethorizontalalignment (Swingconstants.center);
Jbutton4.settext ("<<-");
Jbutton4.addactionlistener (New Java.awt.event.ActionListener () {
public void actionperformed (ActionEvent e) {
Jbutton4_actionperformed (e);
}
}); Loading data
Contentpane.add (JPanel1, Borderlayout.center);
Jpanel1.add (JPNLTOP2, Borderlayout.center);
Jpnltop2.add (JscrollList1, New Xyconstraints (43, 22, 132, 193));
Jpnltop2.add (JButton4, New xyconstraints (178, 125,-1, 26));
Jpnltop2.add (JButton3, New xyconstraints (178, 92,-1, 26));
Jpnltop2.add (JButton2, New xyconstraints (177, 58,-1, 26));
Jpnltop2.add (JButton1, New Xyconstraints (176, 25,-1, 26));
Jpnltop2.add (JscrollList2, New Xyconstraints (235, 23, 129, 193));
This.setresizable (FALSE);
This.setdefaultcloseoperation (Jframe.exit_on_close);
This.setsize (480, 320);
This.settitle ("Jlisttest"); Booking Information Maintenance//save//close
Setuplist ();
}

private void Setuplist () {
Listmode1.addelement ("AAA");
Listmode1.addelement ("BBB");
Listmode1.addelement ("CCC");
Listmode1.addelement ("ddd");
Listmode1.addelement ("111");
Listmode1.addelement ("222");
Listmode1.addelement ("444");
Listmode1.addelement ("555");
Listmode1.addelement ("000");
Listmode1.addelement ("888");
}

void Jbutton1_actionperformed (ActionEvent e) {
if (jlist1.getselectedindices (). length > 1) {
int temp =-1;
int t[] = jlist1.getselectedindices ();
Jlist1.clearselection ();
for (int i = 0; i < t.length; i++) {
temp++;
Jlist1.setselectedindex (T[i]-temp);
Listmode2.addelement (Jlist1.getselectedvalue (). toString ());
Listmode1.remove (T[i]-temp);
}
JList sort
Listmodelsort (LISTMODE2);
if (listmode1.size () > 0) {
Jlist1.setselectedindex (0);
}
Return
}
if (Jlist1.getselectedindex () >-1) {
if (Jlist1.getselectedindex () > Listmode2.size ()) Listmode2.setsize (
Jlist1.getselectedindex ());
Listmode2.addelement (Jlist1.getselectedvalue (). toString (). Trim ());
Listmode1.remove (Jlist1.getselectedindex ());
Listmode1.removeelement (Jlist1.getselectedvalue (). toString (). Trim ());
}
JList sort
Listmodelsort (LISTMODE2);
if (listmode1.size () > 0) {
Jlist1.setselectedindex (0);
}
}

void Jbutton3_actionperformed (ActionEvent e) {
if (jlist2.getselectedindices (). length > 1) {
int temp =-1;
int t[] = jlist2.getselectedindices ();
Jlist2.clearselection ();
for (int i = 0; i < t.length; i++) {
temp++;
Jlist2.setselectedindex (T[i]-temp);
Listmode1.addelement (Jlist2.getselectedvalue (). toString ());
Listmode2.remove (T[i]-temp);
}
JList sort
Listmodelsort (LISTMODE1);
if (listmode2.size () > 0) {
Jlist2.setselectedindex (0);
}
Return
}
if (Jlist2.getselectedindex () >-1) {
if (Jlist2.getselectedindex () > Listmode1.size ()) Listmode1.setsize (
Jlist2.getselectedindex ());
Listmode1.addelement (Jlist2.getselectedvalue (). toString (). Trim ());
Listmode2.remove (Jlist2.getselectedindex ());
Listmode1.removeelement (Jlist1.getselectedvalue (). toString (). Trim ());
}
JList sort
Listmodelsort (LISTMODE1);
if (listmode2.size () > 0) {
Jlist2.setselectedindex (0);
}
}

  void jbutton2_actionperformed (ActionEvent e) {
    if (listmode1.size () > 0) {
       while (listmode1.size () > 0) {
       //for (int i = 0; I < listmode1.size (); i++) {
        jlist1.setselectedindex (0);
        listmode2.addelement (Jlist1.getselectedvalue (). toString (). Trim ());
        Listmode1.remove (Jlist1.getselectedindex ());
     }
     //jlist Sort
      listmodelsort (listMode2);
   }
 }

  void jbutton4_actionperformed (ActionEvent e) {
    if (listmode2.size () > 0) {
       while (listmode2.size () > 0) {
       //for (int i = 0; I < listmode1.size (); i++) {
        jlist2.setselectedindex (0);
        listmode1.addelement (Jlist2.getselectedvalue (). toString (). Trim ());
        Listmode2.remove (Jlist2.getselectedindex ());
     }
     //jlist Sort
      listmodelsort (listMode1);
   }
 }

public void Listmodelsort (Defaultlistmodel defaultlistmodel) {
Java.util.ArrayList list = new Java.util.ArrayList (Arrays.aslist (
Defaultlistmodel.toarray ()));
for (int i = 0; i < list.size (); i++) {
if (list.get (i) = = null) {
List.set (i, "");
}
}
Collections.sort (list);
Defaultlistmodel.removeallelements ();
for (int i = 0; i < list.size (); i++) {
Defaultlistmodel.addelement (List.get (i). toString ());
}
}

public static void Main (string[] args) {
Pmmshipingspace test = new Pmmshipingspace ();
Test.show ();
}
}

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.