Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How does Java click OK to add the contents of one interface table to another interface table?
How does Java click OK to add the contents of one interface table to another interface table?
Realization method

1, initialization

First, add the instance JScrollPane 1, DefaultListModel.

Instance model 1, and then use model 1 to generate an instance of JList. Add jScrollPane 1 at an appropriate position in the dialog box, adjust it to an appropriate size, and then add jList 1 on jScrollPane 1. Therefore, a list box with a scroll bar is added.

Initializes a Vector instance to store the selected list element. Because the selected elements are uncertain, Vector, a data structure with dynamic storage space, is used. Vector cannot specify the initialization length when initializing an instance, so an integer variable is added to indicate the number of objects in the list in the Vector.

2. Add and delete list elements

Add and delete elements in the list box through the addElement method and removeElement method of model 1 Through the attribute selectionMode of the list box, you can set single selection, single discontinuous selection and multiple discontinuous selections of the list box. The object stored in the Vector corresponds to the selected element, and the addition and deletion of the object are realized by the addElement method and the removeElement method.

3. Get the selected element.

After clicking OK, you need to convert the object in the Vector instance to the required data type.

case analysis

Now take 1 as an example to describe the specific implementation method. Available elements are selected from the list box on the left of figure 1, and the selection method is set to intermittent selection for many times. Because the other two methods are relatively simple, you can modify the implementation method based on this example. After selecting an element, click the "> >" button, and the selected element will be displayed in the list box on the right. If there are multiple or incorrect selections, you can select the elements in the list box on the right, and then click.

The function code realized above is as follows

Figure 1 list box example

The specific program implementation method is as follows.

Import javax.swing.jpanel;

Import javax.swing.jframe;

Import javax.swing.jscrollpane;

Import java.awt.rectangle;

Import java.util.vector;

Import javax.swing.jlist;

Import javax.swing.jbutton;

Import javax.swing.defaultlistmodel;

Common class Llist extends JFrame {

private static final long serialVersionUID = 1L;

private JPanel jContentPane = null;

private JScrollPane JScrollPane = null;

private JScrollPane JScrollPane 1 = null;

private JList jList = null

private JList JList 1 = null;

private JButton jButton = null

private JButton JButton 1 = null;

Private DefaultListModel model;

Private DefaultListModel model 2;

/**

* This is the default constructor.

*/

Private vector VC = newvector (); // @jve:decl-index=0:

Private int number;

Public Llist() {

super();

initialize();

}

/**

* This method initializes this

*

* @ returns void

*/

Private void initialize() {

this.setSize(393,263);

this . setcontentpane(getJContentPane());

this . settitle(" JFrame ");

}

/**

* this method initializes jContentPane.

*

* @return javax.swing.JPanel

*/

Private JPanel getJContentPane() {

if (jContentPane == null) {

jContentPane = new JPanel();

jcontentpane . set layout(null);

jcontentpane . add(getJScrollPane(),null);

jcontentpane . add(getjscrollpane 1(),null);

jContentPane.add(getJButton(),null);

jcontentpane . add(getjbutton 1(),null);

jContentPane.add(getJButton2(),null);

jContentPane.add(getJButton3(),null);

}

Return to jContentPane

}

/**

* this method initializes jScrollPane

*

* @ return javax . swing . jscrollpane

*/

private JScrollPane getJScrollPane(){

if (jScrollPane == null) {

jScrollPane = new jScrollPane();

JScrollPane.setBounds (new rectangle (22,23, 1 10,152));

jscrollpane . setviewportview(get jlist());

}

Return to jScrollPane

}

/**

* this method initializes jScrollPane 1

*

* @ return javax . swing . jscrollpane

*/

private JScrollPane getjscrollpane 1(){

if (jScrollPane 1 == null) {

JScrollPane 1 = new JScrollPane();

JScrollPane 1.setBounds (new rectangle (223, 24, 1 16,150));

jscrollpane 1 . setviewportview(getj list 1());

}

Returns jscrollpane1;

}

/**

* This method initializes jList.

*

* @return javax.swing.JList

*/

String []

a={"Element 1 "," Element2 "," Element3 "," Element4 "," Element5 "," Element6 "," Element7 "," Element8 "," Element9 "," Element 10 ",};

private JButton jButton2 = null

private JButton jButton3 = null

private JList getJList() {

if (jList == null) {

jList = new jList();

model = new DefaultListModel();

JList.setModel (model);

for(int I = 0; I<a. Length; i++)

{

model . add element(a[I]);

}

}

Return jList

}

/**

* this method initializes jList 1

*

* @return javax.swing.JList

*/

Private JList getJList 1() {

if (jList 1 == null) {

JList 1 = new JList();

model 2 = new DefaultListModel();

jlist 1 . set model(model 2);

}

Returns jlist1;

}

/**

* this method initializes jButton.

*

* @return javax.swing.JButton

*/

Private JButton getJButton() {

if (jButton == null) {

jButton = new jButton();

JButton.setBounds (new rectangle (149, 52, 56, 29));

jbutton . settext(" & gt; >);

jbutton . addactionlistener(new Java . awt . event . action listener(){

public void action performed(Java . awt . event . action event e){

//system . out . println(" action performed()"); // TODO automatically generated events

Stub operation performed ()

object[]items = jlist . getselectedvalues();

for(int I = 0; I< project. Length; I++)// Select elements from the left and add them to the list on the right.

{

model 2 . add element(items[I]);

num = num+ 1;

VC . setsize(num);

}

}

});

}

Return to jButton

}

/**

* this method initializes jButton 1

*

* @return javax.swing.JButton

*/

Private JButton getJButton 1() {

if (jButton 1 == null) {

JButton 1 = new JButton();

JButton 1.setBounds (new rectangle (145,117,65,31));

jbutton 1 . settext(" & lt; & lt”);

jbutton 1 . addactionlistener(new Java . awt . event . action listener(){

public void action performed(Java . awt . event . action event e){

//system . out . println(" action performed()"); // TODO automatically generated events

Stub operation performed ()

for(int I = 0; I< jlist1.getselectedvalues (). Length; i++)

{

model 2 . remove element(jlist 1 . getselectedvalue()); //Delete the selected list element on the right.

num = num- 1;

VC . setsize(num);

}

}

});

}

Returns jbutton1;

}

/**

* this method initializes jButton2.

*

* @return javax.swing.JButton

*/

Private JButton getJButton2() {

if (jButton2 == null) {

JButton 2 = new JButton();

JButton2.setBounds (new rectangle (82, 193, 63, 27));

jbutton 2 . settext(" OK ");

jbutton 2 . addactionlistener(new Java . awt . event . action listener(){

public void action performed(Java . awt . event . action event e){

}

});

}

Return to jButton2

}

/**

* this method initializes jButton3.

*

* @return javax.swing.JButton

*/

Private JButton getJButton3() {

if (jButton3 == null) {

JButton 3 = new JButton();

JButton3.setBounds (new rectangle (196, 195, 66, 23));

jbutton 3 . settext(" cancel ");

jbutton 3 . addactionlistener(new Java . awt . event . action listener(){

public void action performed(Java . awt . event . action event e){

//system . out . println(" action performed()"); // TODO automatically generated events

Stub operation performed ()

model 2 . remove allements(); //Delete all elements in the list box on the right.

}

});

}

Return to jButton3

}

}