Probleme de remplissage dune Jlist via un fichier texte

tomski Messages postés 48 Date d'inscription mercredi 24 mars 2004 Statut Membre Dernière intervention 24 décembre 2009 - 18 févr. 2009 à 06:59
 Utilisateur anonyme - 25 févr. 2009 à 00:10
Salut!

Jessaye a partir dun fichier texte de creer un graphique.
Le fichier texte contient :
Etiquette;50
Love;20
Filin;3

Mais dans le futur il peut contenir autrechose.
Je charge ce fichier texte dans un array (un array de string pour la partie a gauche du ; et un array de int pour la partie droite de ;)

A partir de cet array de string, je cree une JList pour selectionner ce ke je veux afficher.
Mon prob, cest ke ma liste ne se charge pas,( ya 1h elle chargeait mais jsais plus ce ke javais fait)... et la jsuis paumé grave!

Si kelkun veut bien avoir la gentilesse de maider, ci dessous mon code en entier.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.Properties;
import java.util.*;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.Writer;
import java.math.*;




public class GRAF extends JFrame implements MouseListener{


           static Toolkit toolkit = Toolkit.getDefaultToolkit(); 
           static Dimension screenSize = toolkit.getScreenSize();
           static int centerscreenW= screenSize.width / 2;
           static int centerscreenH= screenSize.height / 2;
           static int screenW= screenSize.width / 2;
           static int screenH= screenSize.height / 2;
           static int relocx = centerscreenW +50;
           static int relocy = centerscreenH +50;
           static int x;
           static int y;
           static int centreFx;
           static int centreFy;
           static String chaine="";
           static String typedText;
           static JLabel[] labelString;
           static JLabel[] labelint;
           static String[] arrayString;
           static int[] arrayint;
           static String[] arrayTEMP;
           JColorChooser color= new JColorChooser();
           static Polygon polygon;
        JPanel titlePanel = new JPanel();
        JPanel colorPanel = new JPanel();
        JPanel ls1Panel = new JPanel();
        JPanel ls2Panel = new JPanel();
        JPanel lstPanel = new JPanel();
        JLabel ls1 = new JLabel("Add Items to the Graph");
        JLabel ls2 = new JLabel("Available Items");


        DefaultListModel model = new DefaultListModel();




        //JList lst1 = new JList(arrayString);
        //JList lst2 = new JList(arrayTEMP);
        JList lst1;
        final JList lst2 = new JList(model);
        //SortedSet model= new TreeSet();


        int pos;


 


     public GRAF(){
            getContentPane();
            System.out.println("in GRAF");
            addMouseListener(this);


     }//end GRAF()


 


     public static void main(String[] args) {
System.out.println("in MAIN");
         GRAF graf = new GRAF();
         graf.setSize(600,600);
         graf.setTitle("GRAPH");
         ImageIcon img= new ImageIcon("smiley.gif");
         graf.setIconImage(img.getImage());
         //ajout du panel daffichage graphique
         graf.getContentPane().add(new MyComponent());
         //graf.setBackground(Color.white);
         graf.setDefaultCloseOperation(EXIT_ON_CLOSE);


 


 


        //Get the screen size 
              //Toolkit toolkit = Toolkit.getDefaultToolkit(); 
              //Dimension screenSize = toolkit.getScreenSize();
         //Calculate the frame location
               x = (screenSize.width - graf.getWidth()) / 2; 
               y = (screenSize.height - graf.getHeight()) / 2; 




               centreFx= graf.getWidth() / 2;
               centreFy= graf.getHeight() / 2;
              //int x = (screenSize.width - graf.getWidth()) / 2; 
              //int y = (screenSize.height - graf.getHeight()) / 2; 




          /*LECTURE FICHIER TEXTE*/
              try{


                        BufferedReader br=new BufferedReader(new FileReader("input.txt"));
try{
   String ligne;
//creation arrays


arrayString = new String[10];
arrayint = new int[10];


//AJOUT
labelString = new JLabel[10];
labelint = new JLabel[10];


int indexLigne=0;//ajout DARK
arrayString = new String[10]; // a redimensionner avec le nombre de ligne exact !
arrayint = new int[10];


/*AJOUT*/


while ((ligne=br.readLine())!=null ){
String[] elements = ligne.split(";");
arrayString[indexLigne] = elements[0];
//AJOUT
System.out.println("arrayString[indexLigne] = "+arrayString[indexLigne]);


 


arrayint[indexLigne] = Integer.parseInt(elements[1]);
//AJOUT
System.out.println("arrayint[indexLigne] = "+arrayint[indexLigne]);


indexLigne++;


   }
                        }finally{
       br.close();
                        }
  }catch (Exception e){
   System.out.println(e.toString());
  }




/*FIN AJOUT*/


/*
        arrayString[i] = s1;
        System.out.println("Array1= "+arrayString[i]);
        arrayint[i] = Integer.parseInt(s2);
        System.out.println("Array2= "+arrayint[i]);


}//end for


*/


           /*FIN LECTURE FICHIER TEXTE*/


           System.out.println("screenW = "+screenSize.width);
           System.out.println("screenH = "+screenSize.height);
           System.out.println("centerscreenW = "+centerscreenW);
           System.out.println("centerscreenH = "+centerscreenH);
           System.out.println("relocx = "+relocx);
           System.out.println("relocy = "+relocy);


           System.out.println("frame w = "+graf.getWidth());
           System.out.println("frame h = "+graf.getHeight());
           System.out.println("x du paint = "+x);
           System.out.println("y du paint = "+y);
           System.out.println("relocx  = "+relocx);
           System.out.println("relocy = "+relocy);


           System.out.println("centre x = "+screenSize.width / 2);
           System.out.println("centre y = "+screenSize.height / 2);
           System.out.println("centreFx = "+centreFx);
           System.out.println("centreFy = "+centreFy);
 
         //Set the new frame location 
         graf.setLocation(x, y);
         graf.show();
     }


 


static class MyComponent extends JComponent {


       
        MyComponent() {
                  System.out.println("in MYCOMPONENT()");
        }


   
        // This method is called whenever the contents needs to be painted
        public void paint(Graphics g) {
System.out.println("in PAINT");
             //setBackground(Color.cyan);
            


if (typedText != null){          System.out.println("typedText !NULL "+typedText);
          g.drawString(typedText,300,200);
          g.setColor(Color.black);


}else{
          System.out.println("typedText==NULL = "+typedText);
}//end if     


          g.drawArc(centreFx-40, centreFy-40,80,80,0,360);
          g.setColor(Color.black);


/*LIGNES*/         
          //ligne horizontale vers la droite
          g.drawLine(centreFx,centreFy,centreFx+90,centreFy);
          g.setColor(Color.pink); 
          //ligne verticale vers le bas
          g.drawLine(centreFx,centreFy,centreFx,centreFy+90);
          g.setColor(Color.pink);     
          //ligne horizontale vers la gauche
          g.drawLine(centreFx,centreFy,centreFx-90,centreFy);
          g.setColor(Color.pink); 
          //ligne verticale vers le haut
          g.drawLine(centreFx,centreFy,centreFx,centreFy-90);
          g.setColor(Color.pink);
 
          //ligne oblique vers haut gauche
          g.drawLine(centreFx,centreFy,centreFx-90,centreFy-90);
          g.setColor(Color.pink); 
          //ligne oblique vers haut droite
          g.drawLine(centreFx,centreFy,centreFx+90,centreFy-90);
          g.setColor(Color.pink); 
          //ligne oblique vers bas gauche
          g.drawLine(centreFx,centreFy,centreFx-90,centreFy+90);
          g.setColor(Color.pink);
          //ligne oblique vers bas droite
          g.drawLine(centreFx,centreFy,centreFx+90,centreFy+90);
          g.setColor(Color.pink);


/*FIN LIGNES*/  




/*CREATION ET POSITIONNEMENT DES LABELS*/
/*         
          g.drawString("Etiquette verte",centreFx+90,centreFy);
          g.drawString("Etiquette verte",centreFx,centreFy+90);
          g.drawString("Etiquette verte",centreFx-90,centreFy);
          g.drawString("Etiquette verte",centreFx,centreFy-90);
          g.drawString("Etiquette verte",centreFx-90,centreFy-90);
          g.drawString("Etiquette verte",centreFx+90,centreFy-90);
          g.drawString("Etiquette verte",centreFx-90,centreFy+90);
          g.drawString("Etiquette verte",centreFx+90,centreFy+90);
*/
//for(int i=0; i<labelString.length; i++){


System.out.println("TRYING TO COPY ARRAYS....");
System.out.println("arrayString.toString() = ");




if (labelString != null){
System.out.println("labelString != NULL");
}else{
System.out.println("labelString == NULL");
}


 


//System.out.println("HERE OK");


/*BELOW NOT OK*/
/*
          g.drawString(labelString[0].getText(),centreFx+90,centreFy);
          g.drawString(labelString[1].getText(),centreFx,centreFy+90);
          g.drawString(labelString[2].getText(),centreFx-90,centreFy);
          g.drawString(labelString[3].getText(),centreFx,centreFy-90);
          g.drawString(labelString[4].getText(),centreFx-90,centreFy-90);
          g.drawString(labelString[5].getText(),centreFx+90,centreFy-90);
          g.drawString(labelString[6].getText(),centreFx-90,centreFy+90);
          g.drawString(labelString[7].getText(),centreFx+90,centreFy+90);


*/
//System.out.println("HERE OK");


 




       
/*FIN CREATION ET POSITIONNEMENT DES LABELS*/


/*CODE POLYGON*/
System.out.println("in POLYGON");
        polygon = new Polygon();
        polygon.addPoint(centreFx+45, centreFy);
        polygon.addPoint(centreFx-45, 350);
        polygon.addPoint(centreFx, centreFy-50);
        polygon.addPoint(centreFx+60, 250);
        polygon.addPoint(300, 150);
        // Add more points...
        g.setColor(Color.blue);
        g.drawPolygon(polygon);




setBackground(Color.yellow);
//setForeground(Color.white);


        }
    }//end class Component




/*
public void stateChanged(ChangeEvent e) {
    Color newColor = color.getColor();
    //.setForeground(newColor);
}
*/


 


public void mouseClicked(MouseEvent e) {
       int click = e.getClickCount();
       //test if coordinates of doubleclick event belongs to a polygon
       int gotx = e.getX();
       int goty = e.getY();
       //Point p = e.getPoint();
       if (click == 2){


//double clicked


arrayTEMP = new String[10];
System.arraycopy(arrayString, 0, arrayTEMP, 0, arrayString.length);


//System.out.println("HERE OK");




/*
if (polygon.contains(gotx, goty) == true){
System.out.println("we are in POLYGON!");
        g.setColor(Color.white);//A MODIFIER
        g.drawPolygon(polygon);
}else{
}
*/




//end test
       
        JDialog d = new JDialog(this, "Modify...", true);
        final JTextField txtTitel = new JTextField(20);
        //System.out.println("HERE OK");
        txtTitel.addActionListener( new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                        //typedText = txtTitel.getText();
                  }
         });




        JButton okTitBTN = new JButton("OK");
        JButton okTitel = new JButton("OK");
        JButton AddBTN = new JButton("<< ADD");
        JButton DeleteBTN = new JButton("REMOVE >>");


 


        JPanel titlePanel = new JPanel();
        JPanel colorPanel = new JPanel();
        JPanel ls1Panel = new JPanel();
        JPanel ls2Panel = new JPanel();
        JPanel lstPanel = new JPanel();
        JLabel ls1 = new JLabel("Add Items to the Graph");
        JLabel ls2 = new JLabel("Available Items");
        //JList lst1 = new JList(arrayString);
        //JList lst2 = new JList(arrayTEMP);
        lst1 = new JList(arrayString);
        //lst2 = new JList(arrayTEMP);
       
       
        lstPanel.setBorder(BorderFactory.createEtchedBorder());
        lstPanel.setLayout(new GridBagLayout());
        lstPanel.add(ls1, new GridBagConstraints(0, 0, 1, 1, 0, 0,
                                 GridBagConstraints.CENTER, GridBagConstraints.NONE,
                               new Insets(0, 0, 0, 0), 0, 0));
        lstPanel.add(new JScrollPane(lst1), new GridBagConstraints(0, 1, 1, 5, .5,
                              1, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                              new Insets(0, 0, 0, 0), 0, 0));
        lstPanel.add(AddBTN, new GridBagConstraints(1, 2, 1, 2, 0, .25,
                              GridBagConstraints.CENTER, GridBagConstraints.NONE,
                              new Insets(0, 0, 0, 0), 0, 0));
        lstPanel.add(DeleteBTN, new GridBagConstraints(1, 4, 1, 2, 0, .25,
                              GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
                             0, 5, 0, 5), 0, 0));
        lstPanel.add(ls2, new GridBagConstraints(2, 0, 1, 1, 0, 0,
                             GridBagConstraints.CENTER, GridBagConstraints.NONE,
                            new Insets(0, 0, 0, 0), 0, 0));
        lstPanel.add(new JScrollPane(lst2), new GridBagConstraints(2, 1, 1, 5, .5,
                             1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                             new Insets(0, 0, 0, 0), 0, 0));


 


        lst1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        lst1.setVisibleRowCount(-1);
        JScrollPane listScroller1 = new JScrollPane(lst1);
        listScroller1.setPreferredSize(new Dimension(250, 80));
        lst2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        lst2.setVisibleRowCount(-1);
        JScrollPane listScroller2 = new JScrollPane(lst2);
        listScroller2.setPreferredSize(new Dimension(250, 80));
       // JColorChooser color= new JColorChooser();
        color.getColor();




        colorPanel.add(color);


        titlePanel.add("North", new JLabel("Enter a Title for your GRAPH"));
        titlePanel.add("Center", txtTitel);
        titlePanel.add("South", okTitel);


        JTabbedPane tabbedPane = new JTabbedPane();
       
        tabbedPane.addTab("TITEL", null,
                          titlePanel,
                          "choose a title"); //tooltip text
        tabbedPane.addTab("LABELS", null,
                          lstPanel,
                          "choose a title"); //tooltip text
        tabbedPane.addTab("KOLORs", null,
                          colorPanel,
                          "choose a color"); //tooltip text


        d.add(tabbedPane, BorderLayout.CENTER);
       // d.setModal(true);
        d.setSize(450,450);
        d.show();


      
        okTitBTN.addActionListener( new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                       System.out.println("in OK");
                        typedText = txtTitel.getText();
                        System.out.println("HERE OK ! typedText = "+txtTitel.getText());
                        repaint();
                      
                  }
         });


        AddBTN.addActionListener( new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
System.out.println("in ADD");
                          Object selected[] = lst2.getSelectedValues();
                          pos = lst2.getModel().getSize();
                          for (int i=0; i<selected.length; i++) {
                                   model.add(pos, selected[i]);
                          }
                      
                  }
         });


        DeleteBTN.addActionListener( new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
 System.out.println("in REMOVE");
                         Object selected[] = lst2.getSelectedValues();
                         lst2.getSelectionModel().clearSelection();
                           // Remove the last item
                          pos = model.getSize()-1;
                          if (pos >= 0) {
                               model.remove(pos);
                          }


                  }
         });




       }else{
       }
}//end mouseClicked


public void mousePressed(MouseEvent e) {}


public void mouseReleased(MouseEvent e) {}


public void mouseEntered(MouseEvent e) {}


public void mouseExited(MouseEvent e) {}


 




}//end GRAF

(_.:~:._)  Tomski le newbie





(_.:~:._)

8 réponses

Utilisateur anonyme
18 févr. 2009 à 09:02
Salut,


Je ne vois pas l'affichage de la frame !

Cordialement,



...\ Dan /...
0
tomski Messages postés 48 Date d'inscription mercredi 24 mars 2004 Statut Membre Dernière intervention 24 décembre 2009
18 févr. 2009 à 11:22
salut Dan,

si si, la Frame principale (GRAF) saffiche bien
ci joint le nouveau code. La JDialog saffiche mais la Jlist est vide pourtant arrayString contient des valeurs....

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.Properties;
import java.util.*;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.Writer;
import java.math.*;




public class GRAF extends JFrame implements MouseListener{


           static Toolkit toolkit = Toolkit.getDefaultToolkit(); 
           static Dimension screenSize = toolkit.getScreenSize();
           static int centerscreenW= screenSize.width / 2;
           static int centerscreenH= screenSize.height / 2;
           static int screenW= screenSize.width / 2;
           static int screenH= screenSize.height / 2;
           static int relocx = centerscreenW +50;
           static int relocy = centerscreenH +50;
           static int x;
           static int y;
           static int centreFx;
           static int centreFy;
           static String chaine="";
           static String typedText;
           static JLabel[] labelString;
           static JLabel[] labelint;
           static String[] arrayString;
           static int[] arrayint;
           static String[] arrayTEMP;
           JColorChooser color= new JColorChooser();
           static Polygon polygon;
        JPanel titlePanel = new JPanel();
        JPanel colorPanel = new JPanel();
 //       JPanel ls1Panel = new JPanel();
 //       JPanel ls2Panel = new JPanel();
        JPanel lstPanel = new JPanel();
        JLabel ls1 = new JLabel("Add Items to the Graph");
        JLabel ls2 = new JLabel("Available Items");


        DefaultListModel model = new DefaultListModel();




        //JList lst1 = new JList(arrayString);
        //JList lst2 = new JList(arrayTEMP);
        JList lst1;
        JList lst2;
        //final JList lst2 = new JList(model);
        //SortedSet model= new TreeSet();


        int pos;


 


     public GRAF(){
            getContentPane();
            System.out.println("in GRAF");
            addMouseListener(this);


     }//end GRAF()


 


     public static void main(String[] args) {
System.out.println("in MAIN");
         GRAF graf = new GRAF();
         graf.setSize(600,600);
         graf.setTitle("GRAPH");
         ImageIcon img= new ImageIcon("smiley.gif");
         graf.setIconImage(img.getImage());
         //ajout du panel daffichage graphique
         graf.getContentPane().add(new MyComponent());
         //graf.setBackground(Color.white);
         graf.setDefaultCloseOperation(EXIT_ON_CLOSE);


 


 


        //Get the screen size 
              //Toolkit toolkit = Toolkit.getDefaultToolkit(); 
              //Dimension screenSize = toolkit.getScreenSize();
         //Calculate the frame location
               x = (screenSize.width - graf.getWidth()) / 2; 
               y = (screenSize.height - graf.getHeight()) / 2; 




               centreFx= graf.getWidth() / 2;
               centreFy= graf.getHeight() / 2;
              //int x = (screenSize.width - graf.getWidth()) / 2; 
              //int y = (screenSize.height - graf.getHeight()) / 2; 




          /*LECTURE FICHIER TEXTE*/
              try{


                        BufferedReader br=new BufferedReader(new FileReader("input.txt"));
try{
   String ligne;
//creation arrays


arrayString = new String[10];
arrayint = new int[10];


//AJOUT
labelString = new JLabel[10];
labelint = new JLabel[10];


int indexLigne=0;//ajout DARK
arrayString = new String[10]; // a redimensionner avec le nombre de ligne exact !
arrayint = new int[10];


/*AJOUT*/


while ((ligne=br.readLine())!=null ){
String[] elements = ligne.split(";");
arrayString[indexLigne] = elements[0];
//AJOUT
System.out.println("arrayString[indexLigne] = "+arrayString[indexLigne]);


 


arrayint[indexLigne] = Integer.parseInt(elements[1]);
//AJOUT
System.out.println("arrayint[indexLigne] = "+arrayint[indexLigne]);


indexLigne++;


   }
                        }finally{
       br.close();
                        }
  }catch (Exception e){
   System.out.println(e.toString());
  }




/*FIN AJOUT*/


/*
        arrayString[i] = s1;
        System.out.println("Array1= "+arrayString[i]);
        arrayint[i] = Integer.parseInt(s2);
        System.out.println("Array2= "+arrayint[i]);


}//end for


*/


           /*FIN LECTURE FICHIER TEXTE*/


           System.out.println("screenW = "+screenSize.width);
           System.out.println("screenH = "+screenSize.height);
           System.out.println("centerscreenW = "+centerscreenW);
           System.out.println("centerscreenH = "+centerscreenH);
           System.out.println("relocx = "+relocx);
           System.out.println("relocy = "+relocy);


           System.out.println("frame w = "+graf.getWidth());
           System.out.println("frame h = "+graf.getHeight());
           System.out.println("x du paint = "+x);
           System.out.println("y du paint = "+y);
           System.out.println("relocx  = "+relocx);
           System.out.println("relocy = "+relocy);


           System.out.println("centre x = "+screenSize.width / 2);
           System.out.println("centre y = "+screenSize.height / 2);
           System.out.println("centreFx = "+centreFx);
           System.out.println("centreFy = "+centreFy);
 
         //Set the new frame location 
         graf.setLocation(x, y);
         graf.show();
     }


 


static class MyComponent extends JComponent {


       
        MyComponent() {
                  System.out.println("in MYCOMPONENT()");
        }


   
        // This method is called whenever the contents needs to be painted
        public void paint(Graphics g) {
System.out.println("in PAINT");
             //setBackground(Color.cyan);
            


if (typedText != null){          System.out.println("typedText !NULL "+typedText);
          g.drawString(typedText,300,200);
          g.setColor(Color.black);


}else{
          System.out.println("typedText==NULL = "+typedText);
}//end if     


          g.drawArc(centreFx-40, centreFy-40,80,80,0,360);
          g.setColor(Color.black);


/*LIGNES*/         
          //ligne horizontale vers la droite
          g.drawLine(centreFx,centreFy,centreFx+90,centreFy);
          g.setColor(Color.pink); 
          //ligne verticale vers le bas
          g.drawLine(centreFx,centreFy,centreFx,centreFy+90);
          g.setColor(Color.pink);     
          //ligne horizontale vers la gauche
          g.drawLine(centreFx,centreFy,centreFx-90,centreFy);
          g.setColor(Color.pink); 
          //ligne verticale vers le haut
          g.drawLine(centreFx,centreFy,centreFx,centreFy-90);
          g.setColor(Color.pink);
 
          //ligne oblique vers haut gauche
          g.drawLine(centreFx,centreFy,centreFx-90,centreFy-90);
          g.setColor(Color.pink); 
          //ligne oblique vers haut droite
          g.drawLine(centreFx,centreFy,centreFx+90,centreFy-90);
          g.setColor(Color.pink); 
          //ligne oblique vers bas gauche
          g.drawLine(centreFx,centreFy,centreFx-90,centreFy+90);
          g.setColor(Color.pink);
          //ligne oblique vers bas droite
          g.drawLine(centreFx,centreFy,centreFx+90,centreFy+90);
          g.setColor(Color.pink);


/*FIN LIGNES*/  




/*CREATION ET POSITIONNEMENT DES LABELS*/
/*         
          g.drawString("Etiquette verte",centreFx+90,centreFy);
          g.drawString("Etiquette verte",centreFx,centreFy+90);
          g.drawString("Etiquette verte",centreFx-90,centreFy);
          g.drawString("Etiquette verte",centreFx,centreFy-90);
          g.drawString("Etiquette verte",centreFx-90,centreFy-90);
          g.drawString("Etiquette verte",centreFx+90,centreFy-90);
          g.drawString("Etiquette verte",centreFx-90,centreFy+90);
          g.drawString("Etiquette verte",centreFx+90,centreFy+90);
*/
//for(int i=0; i<labelString.length; i++){


System.out.println("TRYING TO COPY ARRAYS....");
System.out.println("arrayString.toString() = ");




if (labelString != null){
System.out.println("labelString != NULL");
}else{
System.out.println("labelString == NULL");
}


 


//System.out.println("HERE OK");


/*BELOW NOT OK*/
/*
          g.drawString(labelString[0].getText(),centreFx+90,centreFy);
          g.drawString(labelString[1].getText(),centreFx,centreFy+90);
          g.drawString(labelString[2].getText(),centreFx-90,centreFy);
          g.drawString(labelString[3].getText(),centreFx,centreFy-90);
          g.drawString(labelString[4].getText(),centreFx-90,centreFy-90);
          g.drawString(labelString[5].getText(),centreFx+90,centreFy-90);
          g.drawString(labelString[6].getText(),centreFx-90,centreFy+90);
          g.drawString(labelString[7].getText(),centreFx+90,centreFy+90);


*/
//System.out.println("HERE OK");


 




       
/*FIN CREATION ET POSITIONNEMENT DES LABELS*/


/*CODE POLYGON*/
System.out.println("in POLYGON");
        polygon = new Polygon();
        polygon.addPoint(centreFx+45, centreFy);
        polygon.addPoint(centreFx-45, 350);
        polygon.addPoint(centreFx, centreFy-50);
        polygon.addPoint(centreFx+60, 250);
        polygon.addPoint(300, 150);
        // Add more points...
        g.setColor(Color.blue);
        g.drawPolygon(polygon);




setBackground(Color.yellow);
//setForeground(Color.white);


        }
    }//end class Component




/*
public void stateChanged(ChangeEvent e) {
    Color newColor = color.getColor();
    //.setForeground(newColor);
}
*/


 


public void mouseClicked(MouseEvent e) {
       int click = e.getClickCount();
       //test if coordinates of doubleclick event belongs to a polygon
       int gotx = e.getX();
       int goty = e.getY();
       //Point p = e.getPoint();
       if (click == 2){


//double clicked


/*
//ON CHARGE LES ITEMS SELECTIONNES DANS ARRAYTEMP POUR ENSUITE
//CREER DES JLABELS CORRESPONDANTS


arrayTEMP = new String[10];
System.arraycopy(arrayString, 0, arrayTEMP, 0, arrayString.length);
//System.out.println("HERE OK");
*/


/*
if (polygon.contains(gotx, goty) == true){
System.out.println("we are in POLYGON!");
        g.setColor(Color.white);//A MODIFIER
        g.drawPolygon(polygon);
}else{
}
*/




//end test
       
        JDialog d = new JDialog(this, "Modify...", true);
        final JTextField txtTitel = new JTextField(20);
        //System.out.println("HERE OK");
        txtTitel.addActionListener( new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                        //typedText = txtTitel.getText();
                  }
         });




        JButton okTitBTN = new JButton("OK");
        JButton okTitel = new JButton("OK");
        JButton AddBTN = new JButton("<< ADD");
        JButton DeleteBTN = new JButton("REMOVE >>");
        JButton OKLABELBTN = new JButton("OK Label");


        JPanel titlePanel = new JPanel();
        JPanel colorPanel = new JPanel();
        JPanel lstPanel = new JPanel();
        JLabel ls1 = new JLabel("Add Items to the Graph");
        JLabel ls2 = new JLabel("Available Items");
        lst1 = new JList();
//        lst1 = new JList(arrayString);
        lst2 = new JList();
        lst2 = new JList(arrayTEMP);
/*       
JScrollPane listScroller1 = new JScrollPane(lst1);
listScroller1.setPreferredSize(new Dimension(250, 80));
JScrollPane listScroller2 = new JScrollPane(lst2);
listScroller2.setPreferredSize(new Dimension(250, 80));
lst1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
lst1.setVisibleRowCount(-1);
lst2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
lst2.setVisibleRowCount(-1);
*/      
        lstPanel.setBorder(BorderFactory.createEtchedBorder());
        lstPanel.setLayout(new GridBagLayout());
        lstPanel.add(ls1, new GridBagConstraints(0, 0, 1, 1, 0, 0,
                                 GridBagConstraints.CENTER, GridBagConstraints.NONE,
                               new Insets(0, 0, 0, 0), 0, 0));
        lstPanel.add(new JScrollPane(lst1), new GridBagConstraints(0, 1, 1, 5, .5,
                              1, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                              new Insets(0, 0, 0, 0), 0, 0));


/*        lstPanel.add(listScroller1, new GridBagConstraints(0, 1, 1, 5, .5,
                              1, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                              new Insets(0, 0, 0, 0), 0, 0));
*/
        lstPanel.add(AddBTN, new GridBagConstraints(1, 2, 1, 2, 0, .25,
                              GridBagConstraints.CENTER, GridBagConstraints.NONE,
                              new Insets(0, 0, 0, 0), 0, 0));
        lstPanel.add(DeleteBTN, new GridBagConstraints(1, 4, 1, 2, 0, .25,
                              GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
                             0, 5, 0, 5), 0, 0));
        lstPanel.add(ls2, new GridBagConstraints(2, 0, 1, 1, 0, 0,
                             GridBagConstraints.CENTER, GridBagConstraints.NONE,
                            new Insets(0, 0, 0, 0), 0, 0));
/*         lstPanel.add(listScroller2, new GridBagConstraints(2, 1, 1, 5, .5,
                             1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                             new Insets(0, 0, 0, 0), 0, 0));
*/
        lstPanel.add(new JScrollPane(lst2), new GridBagConstraints(2, 1, 1, 5, .5,
                             1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                             new Insets(0, 0, 0, 0), 0, 0));


        lstPanel.add(OKLABELBTN, new GridBagConstraints(1, 4, 1, 2, 0, .25,
                              GridBagConstraints.PAGE_END, GridBagConstraints.NONE, new Insets(
                             10, 0, 0, 0), 0, 0));


//System.out.println("REMPLISSAGE PAGE HERE OK");




        lst1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        lst1.setVisibleRowCount(-1);
        JScrollPane listScroller1 = new JScrollPane(lst1);
        listScroller1.setPreferredSize(new Dimension(250, 80));
        lst2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        lst2.setVisibleRowCount(-1);
        JScrollPane listScroller2 = new JScrollPane(lst2);
        listScroller2.setPreferredSize(new Dimension(250, 80));


       // JColorChooser color= new JColorChooser();
        color.getColor();




        colorPanel.add(color);


 


        okTitBTN.addActionListener( new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                       System.out.println("in OK");
                        typedText = txtTitel.getText();
                        System.out.println("HERE OK ! typedText = "+txtTitel.getText());
                        repaint();
                      
                  }
         });


        AddBTN.addActionListener( new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
System.out.println("in ADD");
/*
                          Object selected[] = lst2.getSelectedValues();
                          pos = lst2.getModel().getSize();
                          for (int i=0; i<selected.length; i++) {
                                   model.add(pos, selected[i]);
                          }
*/
                      // placer valeurs sélectionnées dans listeCopie.
                      lst2.setListData(lst1.getSelectedValues());
                     
                  }
         });


        DeleteBTN.addActionListener( new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
 System.out.println("in REMOVE");
                         Object selected[] = lst2.getSelectedValues();
                         lst2.getSelectionModel().clearSelection();
                           // Remove the last item
                          pos = model.getSize()-1;
                          if (pos >= 0) {
                               model.remove(pos);
                          }


                  }
         });


        OKLABELBTN.addActionListener( new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
 System.out.println("in OK LABEL");
                   Object selected[] = lst2.getSelectedValues();
                          pos = lst2.getModel().getSize();
                          for (int i=0; i<selected.length; i++) {
                                   model.add(pos, selected[i]);
                                  
                          }


                  }
         });


 


        titlePanel.add("North", new JLabel("Enter a Title for your GRAPH"));
        titlePanel.add("Center", txtTitel);
        titlePanel.add("South", okTitel);


        JTabbedPane tabbedPane = new JTabbedPane();
       
        tabbedPane.addTab("TITEL", null,
                          titlePanel,
                          "choose a title"); //tooltip text
        tabbedPane.addTab("LABELS", null,
                          lstPanel,
                          "choose a title"); //tooltip text
        tabbedPane.addTab("KOLORs", null,
                          colorPanel,
                          "choose a color"); //tooltip text  


        d.add(tabbedPane, BorderLayout.CENTER);
       // d.setModal(true);
        d.setSize(450,450);
        d.show();


System.out.println("HERE OK");  
  




       }else{
       }
}//end mouseClicked


public void mousePressed(MouseEvent e) {}


public void mouseReleased(MouseEvent e) {}


public void mouseEntered(MouseEvent e) {}


public void mouseExited(MouseEvent e) {}


 




}//end GRAF

si tu peux maider, ce serait cool
(_.:~:._)
0
Utilisateur anonyme
18 févr. 2009 à 17:35
Salut,

Bon j'ai compilé.
Mon IDE me signale 2 [deprecation] que je te signale (à toutes fins utiles).

Quelle est ta version Java ?

----jGRASP exec: javac -Xlint:deprecation C:\Mes programmes\GRAF.java
----at: 18 févr. 2009 09:36:42


----jGRASP wedge2: actual command sent ["C:\Program Files\Java\jdk1.6.0_07\bin\javac.exe" -Xlint:deprecation "C:\Mes programmes\GRAF.java"].
----JGRASP wedge2: pid for process is 5088.

C:\Mes programmes\GRAF.java:179: warning: [deprecation] show() in java.awt.Window has been deprecated
graf.show();
^
C:\Mes programmes\GRAF.java:517: warning: [deprecation] show() in java.awt.Dialog has been deprecated
d.show();
^
2 warnings

----jGRASP wedge2: exit code for process is 0.
----jGRASP: operation complete.

À l'execution, la fenêtre GRAF s'affiche bien avec le polygone, mais rien d'autre.
Tu dis que chez toi "La JDialog saffiche mais la Jlist est vide pourtant arrayString contient des valeurs...." :
C'est déjà mieux que chez moi...
La JList devrait s'afficher où ?

Penses à me dire quelle est ta version Java.
Je vais chercher depuis quelle version show() est deprecated.

Cordialement,


Dan
0
Utilisateur anonyme
18 févr. 2009 à 18:19
Voilà ce que j'ai trouvé:

Class Dialog:




show










@Deprecated


public void show()


<dir>
</dir>




Deprecated.



As of JDK version 1.5, replaced by
setVisible(boolean)

.

Makes the Dialog visible. If the dialog and/or its owner are not yet displayable, both are made displayable. The dialog will be validated prior to being made visible. If the dialog is already visible, this will bring the dialog to the front.


If the dialog is modal and is not already visible, this call will not return until the dialog is hidden by calling hide or dispose. It is permissible to show modal dialogs from the event dispatching thread because the toolkit will ensure that another event pump runs while the one which invoked this method is blocked.





 








Class Window:


show












@Deprecated


public void show()


<dir>
</dir>




Deprecated.



As of JDK version 1.5, replaced by
setVisible(boolean)

.

Makes the Window visible. If the Window and/or its owner are not yet displayable, both are made displayable. The Window will be validated prior to being made visible. If the Window is already visible, this will bring the Window to the front.












Cordialement,
...\ Dan /...
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
tomski Messages postés 48 Date d'inscription mercredi 24 mars 2004 Statut Membre Dernière intervention 24 décembre 2009
19 févr. 2009 à 06:59
Salut Dan,

ma version est la 1.6.12. Jai remplace show() par setVisible(true) comme tu mas conseille.
Ok, pour en rvenir a ta question, tu double clik nimporte ou sur mon graphique (la frame), et la, tas ma Jdialog qui saffiche, tu vas dans Labels et la tu vois mes deux Jlists. mon prog est cense remplir la Jlist de droite grace a ArrayString. jai teste ArrayString est != null et contient (Etiquette, Filin, love, null, null, null...) donc ma Jlist devrait contenir les 3 premiers elements.

Ca le faisait avant, mais jai du modifier un truc et je sais plus du coup ce ki bug...Si tu peux maider....

(_.:~:._)
0
Utilisateur anonyme
19 févr. 2009 à 10:25
Salut,

Je trouve anormal que :

        lst1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        lst1.setVisibleRowCount(-1);
        JScrollPane listScroller1 = new JScrollPane(lst1);
        listScroller1.setPreferredSize(new Dimension(250, 80));
        lst2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        lst2.setVisibleRowCount(-1);
        JScrollPane listScroller2 = new JScrollPane(lst2);
        listScroller2.setPreferredSize(new Dimension(250, 80));

Se trouve après les :

lstPanel.add( .......................

Qu'en penses tu ?

Je suis obligé de m'arreter, il est plus de 03 AM chez moi, je reprendrai demain....

Dan

...\ Dan /...
0
tomski Messages postés 48 Date d'inscription mercredi 24 mars 2004 Statut Membre Dernière intervention 24 décembre 2009
24 févr. 2009 à 07:34
salut Dan,

Ca y est je suis de nouveau en ligne!
Merci pour tes msg. ca y est jai trouve, ma Jlist se remplit. par contre jarrive pas a supprimer les items que je selectionne... jai essayer model.remove(pos), marche po! Jai aussi essayer le clearSelection() marche po non plus, jfinirai bien par y arriver :)

Sans indiscretion, tu habites ou pour quil soit 3am chez toi quand il est environ 10h chez moi?? Tes aux states?

A+
(_.:~:._)
0
Utilisateur anonyme
25 févr. 2009 à 00:10
Bonjour du Mexique,

"Sans indiscretion, tu habites ou pour quil soit 3am chez toi quand il est environ 10h chez moi?? Tes aux states?"

Donc je suis juste en dessous, pas loin de Mexico et ta question n'est pas indiscrète du tout...

Dis moi, c'etait quoi alors to pb?

Concernant ton probleme de suppression,j 'ai ici un exemple.

(On ne peut pas modifier une JList créée à partir d'un tableau, d'un vecteur...)

Celà t'aidera peut-etre:

// Cette source est une adaptation d'une demo SUN

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import java.io.*;

publicclass CreerJList2 extends JPanel

implements ListSelectionListener
{

private JList list;

private DefaultListModel listModel;

privatestaticfinal String hireString = "Hire";

privatestaticfinal String fireString = "Fire";

private JButton fireButton;

private JTextField prenomNom;

public CreerJList2()
{

super(new BorderLayout());

listModel =
new DefaultListModel();
listModel.addElement(
"Prénom1 Nom1");
listModel.addElement(
"Prénom2 Nom2");
listModel.addElement(
"Prénom3 Nom3");
listModel.addElement(
"Prénom4 Nom4");
listModel.addElement(
"Prénom5 Nom5");
listModel.addElement(
"Prénom6 Nom6");
listModel.addElement(
"Prénom7 Nom7");
listModel.addElement(
"Prénom8 Nom8");
listModel.addElement(
"Prénom9 Nom9");
listModel.addElement(
"Prénom10 Nom10");

// creer la liste et la mettre dans un scroll pane

list = new JList(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
list.addListSelectionListener(
this);
list.setVisibleRowCount(10);
JScrollPane listScrollPane =
new JScrollPane(list);

JButton hireButton =
new JButton(hireString);
HireListener hireListener =
new HireListener(hireButton);
hireButton.setActionCommand(hireString);
hireButton.addActionListener(hireListener);
hireButton.setEnabled(
false);

fireButton =
new JButton(fireString);
fireButton.setActionCommand(fireString);
fireButton.addActionListener(
new FireListener());

prenomNom =
new JTextField(10);
prenomNom.addActionListener(hireListener);
prenomNom.getDocument().addDocumentListener(hireListener);
String nom = listModel.getElementAt(
list.getSelectedIndex()).toString();

System.out.println(
"Nom : " + nom);

// creer le panel (BoxLayout)

JPanel panelBouton = new JPanel();
panelBouton.setLayout(
new BoxLayout(panelBouton,
BoxLayout.LINE_AXIS));
panelBouton.add(fireButton);
panelBouton.add(Box.createHorizontalStrut(5));
panelBouton.add(
new JSeparator(SwingConstants.VERTICAL));
panelBouton.add(Box.createHorizontalStrut(5));
panelBouton.add(prenomNom);
panelBouton.add(hireButton);
panelBouton.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

add(listScrollPane, BorderLayout.CENTER);
add(panelBouton, BorderLayout.PAGE_END);
}

class FireListener implements ActionListener
{

publicvoid actionPerformed(ActionEvent e)
{
String nom = listModel.getElementAt(
list.getSelectedIndex()).toString();

System.out.println(
"Nom : " + nom);

int index = list.getSelectedIndex();
listModel.remove(index);

int size = listModel.getSize();

if (size == 0)
{
fireButton.setEnabled(
false);
}
else
{

if (index == listModel.getSize())
{
index--;
}

list.setSelectedIndex(index);
list.ensureIndexIsVisible(index);
}
}
}

class HireListener implements ActionListener, DocumentListener
{

privateboolean alreadyEnabled = false;

private JButton bouton;

public HireListener(JButton bouton)
{

this.bouton = bouton;
}

publicvoid actionPerformed(ActionEvent e)
{
String nom = prenomNom.getText();
String nomSelec = listModel.getElementAt(
list.getSelectedIndex()).toString();

System.out.println(
"Nom : " + nom);

if (nom.equals("") || alreadyInList(nom))
{
Toolkit.getDefaultToolkit().beep();
prenomNom.requestFocusInWindow();
prenomNom.selectAll();

return;
}

int index = list.getSelectedIndex();

if (index == -1)
{
index = 0;
}
else
index++;

listModel.insertElementAt(prenomNom.getText(), index);

prenomNom.requestFocusInWindow();
prenomNom.setText(
"");

list.setSelectedIndex(index);
list.ensureIndexIsVisible(index);
}

protectedboolean alreadyInList(String nom)
{

return listModel.contains(nom);
}

publicvoid insertUpdate(DocumentEvent e)
{
enableButton();
}

publicvoid removeUpdate(DocumentEvent e)
{
handleEmptyTextField(e);
}

publicvoid changedUpdate(DocumentEvent e)
{

if (!handleEmptyTextField(e))
{
enableButton();
}
}

privatevoid enableButton()
{

if (!alreadyEnabled)
{
bouton.setEnabled(
true);
}
}

privateboolean handleEmptyTextField(DocumentEvent e)
{

if (e.getDocument().getLength() <= 0)
{
bouton.setEnabled(
false);
alreadyEnabled =
false;

returntrue;
}

returnfalse;
}
}

publicvoid valueChanged(ListSelectionEvent e)
{

if (e.getValueIsAdjusting() == false)
{

if (list.getSelectedIndex() == -1)
{
fireButton.setEnabled(
false);
}
else

fireButton.setEnabled(true);
}
}

publicstaticvoid main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(
true);

// creation et set up de la fenetre.

JFrame frame = new JFrame("CreerJList2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// creation et set up de content pane.

JComponent newContentPane = new CreerJList2();
newContentPane.setOpaque(
true);
frame.setContentPane(newContentPane);

// Affichage fenetre.
frame.pack();
frame.setVisible(
true);
}
}
Le clique du bouton HIRE, supprime l'élément selectionné.
une saisie dans le JTextFild suivi du clique du bouton FIRE ajoute ce que tu frappes, entre la ligne sélectionnée et la suivante, s'il y en a une. Si la ligne sélectionnée est la dernière, l'ajout est fait après celle ci.

J'espère que çà t'aidera à adapter ton programme si tu n'as pas déjà trouvé.
Je n'ai pas encore eu le temps de voir ce qui  "clochait" dans ta source...

Cordialement,

Dan

...\ Dan /...
0
Rejoignez-nous