Probleme de remplissage dune Jlist via un fichier texte
tomski
Messages postés48Date d'inscriptionmercredi 24 mars 2004StatutMembreDernière intervention24 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.
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;
// 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
/*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);
/*
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
tomski
Messages postés48Date d'inscriptionmercredi 24 mars 2004StatutMembreDernière intervention24 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....
// 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
/*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);
/*
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
/*
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
----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.
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.
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 /...
Vous n’avez pas trouvé la réponse que vous recherchez ?
tomski
Messages postés48Date d'inscriptionmercredi 24 mars 2004StatutMembreDernière intervention24 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....
tomski
Messages postés48Date d'inscriptionmercredi 24 mars 2004StatutMembreDernière intervention24 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?
// 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));
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...