0

I'm new in Java, especially the User Interface part, and I wanted to show the different problems that I come across in order to fix them.

1- When I start the application, that's what I want it to be displayed :

https://i.stack.imgur.com/6pnf4.png

However, this is what happen evry time I lauch the application:

https://i.stack.imgur.com/gQda6.png

I have to put my mouse where the button is to be able to display it

2- When I minimize the application and go back, this is what happen:

https://i.stack.imgur.com/VMCas.png

3- When I'm starting the processing of my data, I want the interface to display the current state of the application (how many percentage for example), like this:

https://i.stack.imgur.com/Pfhdo.png

However, this is only happening at the end of the operation. Meanwhile the process, I think my interface just freeze, and it's giving me this instead:

https://i.stack.imgur.com/ewRqj.png

I have no idea why all of this happen. Apart from this, my application works perfectly, and does what I'm programmed to do. This is my Interface class:

import javax.swing.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.filechooser.*;

import Etape.ExportCSV;
import Etape.PlanTransport;
import Etape.PlanTriTCC;
import Etape.StatsTCC;

@SuppressWarnings("serial")
public class Fenetre extends JFrame {

  boolean valid1;
  boolean valid2;
  boolean valid3;
  boolean valid4;

  String emplac1;
  String emplac2;
  String emplac3;
  String emplac4;

  String cheminactuel;

  public Fenetre(){
      valid1=false;
      valid2=false;
      valid3=false;
      valid4=false;
    this.setTitle("Traitement des données Statistiques du Trieur TCC");
    this.setSize(600, 350);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setResizable(false);
    Panel pan = new Panel();
    this.setContentPane(pan);
    this.setVisible(true);

    FileNameExtensionFilter filterCSV = new FileNameExtensionFilter("CSV files (*csv)", "csv");
    FileNameExtensionFilter filterAutre = new FileNameExtensionFilter("403 files (*403)", "403");

    JTextField tfbegin=new JTextField();
    tfbegin.setBounds(150,260,300,20);
    tfbegin.setEditable(false);
    pan.add(tfbegin);

    JButton begin=new JButton("Commencer");
    begin.setBounds(248,230,105,20);
    pan.add(begin);
    begin.setEnabled(false);

    JTextField tf=new JTextField();
    tf.setBounds(25,100,150,20);
    tf.setEditable(false);
    pan.add(tf);

    JButton b=new JButton("Selection");
    b.setBounds(53,130,87,20);
    pan.add(b);
    b.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
          JFileChooser jfc;
          if(valid1==true || valid2==true || valid3==true || valid4==true) {
              jfc = new JFileChooser(cheminactuel);
          }
          else {
        jfc = new JFileChooser();
          }
        jfc.setFileFilter(filterCSV);
        int returnValue = jfc.showOpenDialog(null);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File selectedFile = jfc.getSelectedFile();
          emplac1=selectedFile.getAbsolutePath();
          tf.setText(emplac1);
          valid1=true;
          int test= emplac1.lastIndexOf("\\");
          cheminactuel=emplac1.substring(0,test);
          if (valid1==true && valid2==true && valid3==true && valid4==true) {
            begin.setEnabled(true);
          }
        }
      }
    });

    JTextField tf2=new JTextField();
    tf2.setBounds(225,100,150,20);
    tf2.setEditable(false);
    pan.add(tf2);

    JButton b2=new JButton("Selection");
    b2.setBounds(255,130,87,20);
    pan.add(b2);
    b2.addActionListener(new ActionListener(){
        JFileChooser jfc2;
      public void actionPerformed(ActionEvent e){
          if(valid1==true || valid2==true || valid3==true || valid4==true) {
              jfc2 = new JFileChooser(cheminactuel);
          }
          else {
       jfc2 = new JFileChooser();
          }
        jfc2.setFileFilter(filterAutre);
        int returnValue2 = jfc2.showOpenDialog(null);
        if (returnValue2 == JFileChooser.APPROVE_OPTION) {
            File selectedFile2 = jfc2.getSelectedFile();
          emplac2=selectedFile2.getAbsolutePath();
          tf2.setText(emplac2);
          valid2=true;
          int test= emplac2.lastIndexOf("\\");
          cheminactuel=emplac2.substring(0,test);
          if (valid1==true && valid2==true && valid3==true && valid4==true) {
            begin.setEnabled(true);
          }
        }
      }
    });

    JTextField tf3=new JTextField();
    tf3.setBounds(425,100,150,20);
    tf3.setEditable(false);
    pan.add(tf3);

    JButton b3=new JButton("Selection");
    b3.setBounds(455,130,87,20);
    pan.add(b3);
    b3.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
          JFileChooser jfc3;
          if(valid1==true || valid2==true || valid3==true || valid4==true) {
             jfc3 = new JFileChooser(cheminactuel);
          }
          else {
        jfc3 = new JFileChooser();
          }
        jfc3.setFileFilter(filterCSV);
        int returnValue3 = jfc3.showOpenDialog(null);
        if (returnValue3 == JFileChooser.APPROVE_OPTION) {
            File selectedFile3 = jfc3.getSelectedFile();
          emplac3=selectedFile3.getAbsolutePath();
          tf3.setText(emplac3);
          valid3=true;
          int test= emplac3.lastIndexOf("\\");
          cheminactuel=emplac3.substring(0,test);
          if (valid1==true && valid2==true && valid3==true && valid4==true) {
            begin.setEnabled(true);
          }
        }
      }
    });

    JTextField tf4=new JTextField();
    tf4.setBounds(25,190,400,20);
    tf4.setEditable(false);
    pan.add(tf4);

    JButton b4=new JButton("Selection");
    b4.setBounds(475,190,87,20);
    pan.add(b4);
    b4.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
          JFileChooser jfc4;
          if(valid1==true || valid2==true || valid3==true || valid4==true) {
              jfc4 = new JFileChooser(cheminactuel);
          }
          else {
        jfc4 = new JFileChooser();
          }
        jfc4.setFileFilter(filterCSV);
        int returnValue4 = jfc4.showOpenDialog(null);
        if (returnValue4 == JFileChooser.APPROVE_OPTION) {
            File selectedFile4 = jfc4.getSelectedFile();
            emplac4=selectedFile4.getAbsolutePath();
            tf4.setText(emplac4);
          valid4=true;
          int test= emplac4.lastIndexOf("\\");
          cheminactuel=emplac4.substring(0,test);
          if (valid1==true && valid2==true && valid3==true && valid4==true) {
            begin.setEnabled(true);
          }
        }
      }
    });
    begin.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){

        StatsTCC con = new StatsTCC();
        tfbegin.setText("Creation de la base de donnée.");
        con.insertion(emplac1);
        con.modification();

        PlanTriTCC reg = new PlanTriTCC(emplac2);
        tfbegin.setText("Recherche et intertion des regles.");
        reg.initialisation();
        reg.ajoutTable();
        reg.selection();
        reg.trouvRegle();
        reg.suppression();

        PlanTransport db = new PlanTransport(emplac3);
        tfbegin.setText("Recherche et intertion des horaires.");
        db.initialisation();
        db.ajoutTable();
        db.selection();
        db.trouvLiaison();
        db.suppression();

        @SuppressWarnings("unused")
        ExportCSV fichier = new  ExportCSV(emplac4);

        b.setEnabled(false);
        b2.setEnabled(false);
        b3.setEnabled(false);
        b4.setEnabled(false);
        begin.setEnabled(false);
        tfbegin.setText("Operation termine. Fichier csv disponible");
      }
    });
  }
}

Please can someone help me ? Thanks in advance. (Sorry I'm french so my english is not perfect)

Piko
  • 11
  • 4
  • 4
    You should use a layout instead of using absolute positionning – jhamon Jul 25 '18 at 13:45
  • 1) *"Multiple problems with the user interface of my application"* What is your **one** question for this thread? Split the others out to their own thread. This is not a help desk. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jul 25 '18 at 13:46
  • For status update, have a look at https://stackoverflow.com/questions/3551542/swingutilities-invokelater-why-is-it-needed – jhamon Jul 25 '18 at 13:48
  • Thanks for your advices jhamon, I will look up your links. Sorry Andrew Thompson. – Piko Jul 25 '18 at 13:57

1 Answers1

1
Panel pan = new Panel();
this.setContentPane(pan);

Panel is an AWT component. Don't mix AWT components in a Swing application.

Instead you should be using JPanel.

I have to put my mouse where the button is to be able to display it

this.setVisible(true);

Components should be added to the frame BEFORE the frame is made visible. The setVisible(true) statement should be the last statement of the constructor.

camickr
  • 321,443
  • 19
  • 166
  • 288