Bonjour,

Je suis occupé à développer un GUI pour un projet. C'est la première fois que je fais ça donc j'ai un peu du mal.

Pour le moment j'utilise une interface "JGridBagFrame" et j'ai une fenêtre avec des boutons et une liste de sélection de la forme "JComboBox". Seulement je suis plus intéressé par une multiple sélection de type "JList". Seulement quand je remplace les éléments de la ComboBox par ceux de la liste, je n'ai pas du tout le résultat souhaité.

Sur les images ci-dessous, j'ai juste remplacé le module de sélection d'une liste (le truc avec écris ALL avec une flèche) par une liste où on peut sélectionner plusieurs éléments. Mais comme vous voyez, la liste est très longue et elle s'étale sur toute la fenêtre. J'aimerais que seulement 10 éléments soient visible avec une scroll bar mais ça n'a pas l'air de marcher.

En fait je me suis grandement inspiré d'un livre de Java et j'ai trouvé que la structure qui me convenait le mieux parmi celles proposées était la GridBagFrame.
Indépendamment de ça, ils expliquent comment utiliser un JFrame d'où leur classe "MultipleSelectionFrame".

Je me suis contenté d'implémenter les deux ensemble. Mais si vous pensez que je ferais mieux d'utiliser d'autres fonctions existantes, je suis toute ouïe.

Merci d'avance.

---
Regardez plutôt :






MainRunGUI

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
import javax.swing.JFrame;   
 
 
public class MainRunGUI {   
 
 public static void main(String[] args) {   
  GridBagFrame gridBagFrame = new GridBagFrame();   
  gridBagFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  gridBagFrame.setSize(500,300); // set frame size   
  gridBagFrame.setVisible(true); //display frame;   
 }// end main   
}// end class GridBagDemo


GridBagFrame

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.awt.GridBagLayout;   
import java.awt.GridBagConstraints;   
import java.awt.Component;   
import javax.swing.JFrame;   
import javax.swing.JList;   
import javax.swing.JTextArea;   
import javax.swing.JTextField;   
import javax.swing.JButton;   
import javax.swing.JComboBox;   
 
public class GridBagFrame extends JFrame {   
 private GridBagLayout layout; //layout of this frame   
 private GridBagConstraints constraints; //constraints of this layout   
 
 //set up GUI   
 public GridBagFrame(){   
  super ("Scenario Builder for FIDO GUI");   
  layout = new GridBagLayout();   
  setLayout(layout); //set frame layout   
  constraints = new GridBagConstraints(); // instantiate constraints     
 
  // create GUI components   
  JTextArea textAreaNET = new JTextArea ("NETS", 1, 5);   
  JTextArea textAreaRFD = new JTextArea ("RFD", 1, 5);   
  JTextArea textAreaINFO = new JTextArea ("Data to be processed", 1, 20);   
 
  // create selection lists to copy   
  MultipleSelectionFrame NetListSelection = new MultipleSelectionFrame();   
  JList netList = NetListSelection.getInputList(); // list to hold all the nets   
  JList copyJList = NetListSelection.getCopiedList(); // list to copy net names into    
  JButton copyJButton = NetListSelection.getCopyButton(); // button to copy selected names   
 
 
     // USE OF LIST IN WINDOW   
  /*NetList netList = new NetList();   
  String list[] = netList.getList();    
     JComboBox comboBox = new JComboBox(list);*/     
 
  JButton buttonRFD1 = new JButton("RFD1");   
  JButton buttonRFD2 = new JButton("RFD2");   
  JButton buttonRFD3 = new JButton("RFD3");   
  JButton buttonRUN = new JButton("RUN");     
 
  JTextField textFieldComments = new JTextField ("Comments");   
  JTextField textFieldInfo = new JTextField ("TextInfo");   
 
  constraints.fill = GridBagConstraints.BOTH;   
  constraints.weightx = 10;   
  constraints.weighty = 10;   
 
  addComponent(textAreaNET, 0, 0, 1, 1);   
  addComponent(textAreaRFD, 0, 1, 1, 1);   
  addComponent(textAreaINFO, 0, 2, 1, 1);   
  addComponent(netList, 1, 0, 1, 1);   
  //addComponent(comboBox, 1, 0, 1, 1);   
  addComponent(copyJButton, 9, 0, 1, 1);   
  addComponent(buttonRFD1, 1, 1, 1, 1);     
  addComponent(buttonRFD2, 2, 1, 1, 1);   
  addComponent(buttonRFD3, 3, 1, 1, 1);   
  addComponent(buttonRUN, 10, 0, 1, 1);   
  addComponent(textFieldComments,10,1,3,3);   
  addComponent(copyJList,1,2,3,1);   
  addComponent(textFieldInfo,1,2,3,1);    
 
 } //end GridBagFrame constructor   
 
 // method to set constraints on   
 private void addComponent(Component component, int row, int column, int width, int height){   
  constraints.gridx = column; // set gridx   
  constraints.gridy = row; // set gridy   
  constraints.gridwidth = width; //set gridwidth   
  constraints.gridheight = height; //set gridheight   
  layout.setConstraints(component, constraints); // set constraints   
  add(component); // add component   
 }//end method addComponent   
 
 
}//end class GridBagFrame



MultipleSelectionFrame

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JList; 
import javax.swing.JScrollPane; 
import javax.swing.ListSelectionModel; 
 
 
public class MultipleSelectionFrame extends JFrame{ 
 private JList netList; // list to hold all the nets 
 private JList copyJList; // list to copy net names into  
 private JButton copyJButton; // button to copy selected names 
 
 NetList GeneratedList = new NetList(); 
 private final String list[] = GeneratedList.getList(); 
 
 public MultipleSelectionFrame(){ 
  super("Multiple Selection Lists"); 
  setLayout(new FlowLayout()); 
  netList = new JList(list); 
  netList.setVisibleRowCount(5); 
  netList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 
  add(new JScrollPane(netList)); 
  copyJButton = new JButton("Select"); 
  copyJButton.addActionListener( 
    new ActionListener() 
    { 
     public void actionPerformed(ActionEvent event){ 
      copyJList.setListData(netList.getSelectedValues()); 
     } 
    } 
   ); 
  add(copyJButton); 
  copyJList = new JList(); 
  copyJList.setVisibleRowCount(5); 
  copyJList.setFixedCellWidth(100); 
  copyJList.setFixedCellHeight(15); 
  copyJList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); 
  add(new JScrollPane(copyJList)); 
 } 
 
 public JList getInputList(){ 
  return netList; 
 } 
 public JList getCopiedList(){ 
  return copyJList; 
 } 
 public JButton getCopyButton(){ 
  return copyJButton; 
 } 
 
 public static void main(String args[]){ 
  MultipleSelectionFrame multipleSelectionFrame = new MultipleSelectionFrame(); 
  multipleSelectionFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  multipleSelectionFrame.setSize(500,300); 
  multipleSelectionFrame.setVisible(true); 
 } 
}



NetList

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.io.BufferedInputStream; 
import java.io.DataInputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
 
public class NetList { 
     // NET CODE LIST GENERATION 
  private String[] List; 
 
  public NetList(){ 
       List = new String[595]; 
       // Import from text file 
       File file = new File("NetCodeList.txt"); 
       FileInputStream fis = null; 
       BufferedInputStream bis = null; 
       DataInputStream dis = null; 
 
       try { 
        fis = new FileInputStream(file); 
        // Here BufferedInputStream is added for fast reading. 
        bis = new BufferedInputStream(fis); 
        dis = new DataInputStream(bis); 
 
        // dis.available() returns 0 if the file does not have more lines. 
        int i=0; 
        while (dis.available() != 0) { 
         // this statement reads the line from the file and print it to 
         // the string array. 
         List[i]=dis.readLine(); 
         i=i+1; 
        } 
        // dispose all the resources after using them. 
        fis.close(); 
        bis.close(); 
        dis.close(); 
 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
     } 
 
     public String[] getList(){ 
      return List; 
     } 
}


Le fichier NetCodeList.txt est un fichier texte de 595 lignes qui se trouve dans le dossier du projet et qui est de la forme suivante :

ALL
02ALS0
02AND0
02ASS0
02BER0
02BOS0
02BRA0
02BRC0
02CEN0
02DIL0
02DRO0
02ENG0
02EUR0
02EVE0
02FOR0
02GEN0
02GIL0
02GRI0
02HAL0
02HER0
02HOE0
02IXE0
02JET0
02KOR0
02LAS0
02LEN0
02LIN0
02MAR0
02MEL0
02MOL0
...