1

I want to display many text in the same windows but only the last text is displayed, there is my 2 class :


import Test.Graph;
import javax.swing.JFrame;


public class InterfaceGraphique extends JFrame {

    private static final long serialVersionUID = 1L;


     public InterfaceGraphique() {
        this.setTitle("My first Window");
        this.setSize(800,1000);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setContentPane(new Graph("test 1", 150,300,"erreur"));
        this.setContentPane(new Graph("test 2 ", 250,400,"normal"));
        this.setContentPane(new Graph("test3", 350,500,"valide"));


        this.setVisible(true);


    }

}
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Font;

public class Graph extends JPanel {
    private static final long serialVersionUID = 1L;
    private String fichier;
    private int x;
    private int y;
    String etat;

    public Graph(String fichier, int x, int y, String etat) {
        this.fichier = fichier;
        this.x = x;
        this.y = y;
        this.etat= etat;
    }

    @Override
    public void paintComponent(Graphics g){

        System.out.println("Je suis exécutée !"); 

        if(etat.contentEquals("erreur")) {
            Font font = new Font("Courier", Font.BOLD, 20);
            g.setFont(font);
            g.setColor(Color.red); 
        }
        if(etat.contentEquals("normal")) {
            Font font = new Font("Courier", Font.BOLD, 20);
            g.setFont(font);
            g.setColor(Color.black); 
        }
        if(etat.contentEquals("valide")) {
            Font font = new Font("Courier", Font.BOLD, 20);
            g.setFont(font);
            g.setColor(Color.green); 
        }
        g.drawString(fichier, x, y);

      }

    public static void main(String[] args){
        @SuppressWarnings("unused")
        InterfaceGraphique ig = new InterfaceGraphique();



    }
}

When I compile the test class I have a window with only displayed "test 3" in green. Thanks for help and sorry for bad english. PS: I'm new in JAVA especially in GUI so you can tell me other error to fix thanks!

Yatsu
  • 9
  • 2
  • `this.setContentPane()` does what the name implies: sets the content pane. **There is only one `contentPane` per `JFrame`.** You are replacing the contentPane every time you set it, which is why you only see "test 3". You should instead, add new Graph()s to `JPanel`s and add those `JPanel`s to your `JFrame` using some layout manager. – sleepToken Dec 26 '19 at 17:00
  • Thanks for answer, how can I add new graph to JPanel ? – Yatsu Dec 26 '19 at 17:23

3 Answers3

0

The thing is that below lines of code are actually replacing the ContentPane:

this.setContentPane(new Graph("test 1", 150,300,"erreur"));
this.setContentPane(new Graph("test 2 ", 250,400,"normal"));
this.setContentPane(new Graph("test3", 350,500,"valide"));

The first line sets it to "erreur", then second replaces it to "normal", and thrid again replaces it by "valide"

And you see the result for "valide" only as it is current and last replaced value.

You will need a class that can hold the array or ArrayList of "Graph".

BTW: Welcome to Java..!!

Here are the classes:

import javax.swing.JFrame;

public class InterfaceGraphique extends JFrame{

private static final long serialVersionUID = 1L;


public InterfaceGraphique() {
    this.setTitle("My first Window");
    this.setSize(800, 1000);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // First Grapth Object
    Graph graph = new Graph();

    // Create points;
    Point point1 = new Point("test 1", 150, 300, "erreur");
    Point point2 = new Point("test 2 ", 250, 400, "normal");
    Point point3 = new Point("test3", 350, 500, "valide");

    graph.addPoint(point1);
    graph.addPoint(point2);
    graph.addPoint(point3);

    this.setContentPane(graph);

    this.setVisible(true);
}
}

public class Point {

private String fichier;
private int x;
private int y;
private String etat;

public Point(String fichier, int x, int y, String etat) {
    this.fichier = fichier;
    this.x = x;
    this.y = y;
    this.etat= etat;
}

public String getFichier() {
    return fichier;
}

public void setFichier(String fichier) {
    this.fichier = fichier;
}

public int getX() {
    return x;
}

public void setX(int x) {
    this.x = x;
}

public int getY() {
    return y;
}

public void setY(int y) {
    this.y = y;
}

public String getEtat() {
    return etat;
}

public void setEtat(String etat) {
    this.etat = etat;
}
}

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

public class Graph extends JPanel{

List<Point> points = new ArrayList<Point>();

@Override
public void paintComponent(Graphics g){

    System.out.println("Je suis exécutée !"); 

    for (Point point : points) {

        if(point.getEtat().contentEquals("erreur")) {
            Font font = new Font("Courier", Font.BOLD, 20);
            g.setFont(font);
            g.setColor(Color.red); 

        } else if(point.getEtat().contentEquals("normal")) {
            Font font = new Font("Courier", Font.BOLD, 20);
            g.setFont(font);
            g.setColor(Color.black); 

        }else if(point.getEtat().contentEquals("valide")) {
            Font font = new Font("Courier", Font.BOLD, 20);
            g.setFont(font);
            g.setColor(Color.green); 
        }

        g.drawString(point.getFichier(), point.getX(), point.getX());

    }  
  }

public void addPoint(Point point) {
    points.add(point);
}

public static void main(String[] args){
    @SuppressWarnings("unused")
    InterfaceGraphique ig = new InterfaceGraphique();

}

}

Here is the output: enter image description here

Let me know if I can help more. Thanks.

Mehul Gayate
  • 344
  • 3
  • 7
0

I have a last question, now I want to display files name, it work in command line but not in graphic interface, do you know what I have to add?

import java.io.File;

import java.io.IOException;

import javax.swing.JFrame;

public class InterfaceGraphique extends JFrame {

private static final long serialVersionUID = 1L;


 public InterfaceGraphique() {
    this.setTitle("My first Window");
    this.setSize(800,1000);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // First Graph Object
    Graph graph = new Graph();
    String Path = "./TEST";
    try {
        parcourir2(Path);
    } catch (IOException e) {
        e.printStackTrace();
    }
    this.setContentPane(graph);

    this.setVisible(true);


}

 public static  void parcourir2 (String Path) throws IOException {

     String[] Nom = {"Point1", "Point2", "Point3","Point4","Point5","Point6","Point7","Point8","Point9","Point10"};
     Graph graph = new Graph();
        File repertoire = new File(Path);
        File[] liste = repertoire.listFiles(); 
        int y = 150;
        if (liste != null) {         
            for (int i = 0; i < liste.length; i++) {    
                if (liste[i].isDirectory()) {
                    parcourir2(liste[i].getAbsolutePath());
                }

                if(liste[i].isFile()) {
                    Point Nom[i] = new Point(liste[i].getName(),150,y,"normal");
                    graph.addPoint(Nom[i]);

                    System.out.println(liste[i] + "\n");
                    Test.TestFile.Afficher(liste[i]);
                }
                y=+50;
            }
        } else {
            System.err.println( "Nom de repertoire invalide");
        }

    }

}

Yatsu
  • 9
  • 2
0

Modified the InterfaceGraphique.java

package com.java.graphics;

import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;

public class InterfaceGraphique extends JFrame{

    private static final long serialVersionUID = 1L;


    public InterfaceGraphique() {
        System.out.println("Craetion");

        this.setTitle("My first Window");
        this.setSize(800, 1000);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // First Grapth Object
        Graph graph = new Graph();

        try {
            parcourir2("./Test", graph, new Integer(150));
        } catch (IOException e) {
            e.printStackTrace();
        }       
        this.setContentPane(graph);

        this.setVisible(true);
    }

    public static  int parcourir2 (String Path, Graph graph, Integer xValue) throws IOException {

            File repertoire = new File(Path);
            File[] liste = repertoire.listFiles(); 
            if (liste != null) {         
                for (int i = 0; i < liste.length; i++) {    
                    if (liste[i].isDirectory()) {
                        xValue = parcourir2(liste[i].getAbsolutePath(), graph, xValue);
                    }

                    if(liste[i].isFile()) {
                        Point nom = new Point(liste[i].getName(),xValue, 150,"normal");
                        graph.addPoint(nom);
                       }

                    xValue = xValue + 50;

                }
            } else {
                System.err.println( "Nom de repertoire invalide");
            }

            return xValue;

        }
}

output: enter image description here

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Mehul Gayate
  • 344
  • 3
  • 7