-4

Here is my problem: I have a JPannel Order, in which I have different components; one JTable, and a few JLabel.

These components are filled with datas coming from an object "commande"(order)ex: idOrder, customer, Price...

This JPannel is inside an other Jpanel which contains also a jtable (orderlist)

When I click on an order in the jtable orderlist, I want the information about this order to be printed on the jpannel Order.

Now, I click, my object order is updated correctly but I cannot do the repaint() of the JPannel. Nothing happens.

Here is the code

   public class PanneauDetailCommande extends JPanel{
        Commande commande;
        PanneauDetailCommande(){}

        public void setCommande(Commande commande){this.commande=commande;}

        public void paintComponent(Graphics g){

          String colorPaye=commande.isPaye()? "GREEN" : "RED";

          this.setBackground(Color.white);

          this.setBorder(BorderFactory.createLineBorder(Color.black));

          this.setPreferredSize(new Dimension(600,300));

          JLabel labelDetail=new JLabel("<html><p style=\"padding-left:15px;\"><font  size=4 > Détail commande<font color=\""+colorPaye+"\" size=4 > No  "+(commande.getIdCommande())+"</font> :</font></p></html>");

         labelDetail.setPreferredSize(new Dimension(598,20));

         labelDetail.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.black));

         labelDetail.setOpaque(true);

         labelDetail.setBackground(Color.white );

         this.add(labelDetail);

 }}

Another class JPannel which calls JPannel detailCommande:

 historique.table.getSelectionModel().addListSelectionListener(new   ListSelectionListener(){

       @Override

public void valueChanged(ListSelectionEvent e) {

   // TODO Auto-generated method stub

       int newId=Integer.parseInt(historique.table.getValueAt(historique.table.getSelectedRow(), 0).toString());

       Commande newCommande = gestionCommande.getCommande(newId);

       detail.setCommande(newCommande);

       detail.repaint();

       }});
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Greg Artisi
  • 172
  • 2
  • 12

3 Answers3

1

In general you should not add your components in the paintComponent method. You should instead adding them in event handlers, basically when you call setCommande and then calling revalidate on your component.

See answers and code in

java - How would I dynamically add swing component to gui on click?

Community
  • 1
  • 1
Massimo Petrus
  • 1,881
  • 2
  • 13
  • 26
  • ok, so i did a method drawPannel() where i have all the components, setCommande() calls drawPannel(), and do Revalidate(), but nothing happens : public void setCommande(Commande commande){ this.commande=commande; this.drawPannel(); this.revalidate();} /*pour commencer ,on affiche la dernière commande*/ public void drawPannel(){ /*le panneau*/ this.setBackground(Color.white);.....ect....} – Greg Artisi Feb 05 '17 at 10:52
1

ok, so i did put all my component in a method called drawPannel(), my method setcommnade() calls drawPannel(), like this:

public void setCommande(Commande commande){
    this.commande=commande;
    this.drawPannel();
    }

    /*pour commencer ,on affiche la dernière commande*/
public void drawPannel(){
    /*le panneau*/
    this.setBackground(Color.white);
    ...ect}

.then , in my actionListener, i tried invalidate();validate();repaint();revalidate();but nothing happens at all.

i tried to change background color of a component and then to system.out.print my component color to see if the change is done, and it works finebut i cannot refresh the screen.

public void valueChanged(ListSelectionEvent e) {

                    System.out.println(detail.getComponents()[0].getBackground());//java.awt.Color[r=255,g=255,b=255]
                    detail.getComponents()[0].setBackground(Color.BLACK);
                    System.out.println(detail.getComponents()[0].getBackground());//java.awt.Color[r=0,g=0,b=0]
                    //detail.setCommande(newCommande);
                    detail.invalidate();
                    detail.validate();
                    detail.repaint();
               }});
Greg Artisi
  • 172
  • 2
  • 12
1

ok, i have the answer if it can help somebody:

i do:

public void valueChanged(ListSelectionEvent e) {
                    detail.setCommande(newCommande);
                    revalidate();
                    repaint();
               }});

instead of :

public void valueChanged(ListSelectionEvent e) {
                    detail.setCommande(newCommande);
                    detail.revalidate();
                    detail.repaint();
               }});

in conclusion , i repaint the entire main pannel and not the inside pannel the pannel

Greg Artisi
  • 172
  • 2
  • 12