Max
Max

Reputation: 59

Gui does not paint the panel

hi guys i have wird problem i have here my GUI class is working good at begining just by showing loggin screen. but i have second class called DataLayer that is responsible for reading from files and creating objets with infromaton. the problem is that when i try to create new DataLayer() in GUI class the panel doesn show until i resize screen and even after that the keylistener doesn't work.

`package View;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

import dto.DataLayer;
import dto.ProductDTO;

public class GUI extends JPanel {
    private DataLayer dt;
    private ComponentAbstract korzen;
    private GUI self;
    public GUI() {
        this.setFocusable(true);
        this.dt=new DataLayer();`

        self=this; 

        this.stworz_PanelLogowania();



        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                korzen.tryPressKey(e);
                repaint();
            }
        });


    this.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            super.mouseClicked(e);
            korzen.tryClick(e.getX(), e.getY());
            repaint();
        }
    });

     this.repaint();

    }

    @Override
    protected void paintComponent(Graphics g ) {
        super.paintComponent(g);
        korzen.repaint();
        System.out.println("omatko");
        korzen.draw((Graphics2D)g);

    }

    private void zmien_panel(ComponentAbstract newkorzen){
        korzen=newkorzen;
        self.repaint();
    }


    private void stworz_PanelLogowania(){

        LinearPanel lp=new LinearPanel(220, 10, 300, 300);

        lp.setOrientarion(Orientation.VERTICAL);

        LinearPanel labels_panel=new LinearPanel(220,0,50,80);
        labels_panel.setOrientarion(Orientation.VERTICAL);

        labels_panel.addComponent(new Label(0, 0, 350, 40, "Witamy w castorama APP"));
        lp.setPadding(6);
        LinearPanel textpanel1=new LinearPanel(0, 0, 350, 80);
        textpanel1.setPadding(0);
        textpanel1.addComponent(new Label(0,0,350,40,"Login:"));
        textpanel1.addComponent(new TextBox(0, 0, 350, 40));

        LinearPanel textpanel2=new LinearPanel(0, 0, 35, 80);
        textpanel2.setPadding(0);
        textpanel2.addComponent(new Label(0,0,350,40,"Hasło:"));
        textpanel2.addComponent(new TextBox(0, 0, 350, 40));

        lp.addComponent(labels_panel);
        lp.addComponent(textpanel1);
        lp.addComponent(textpanel2);

        LinearPanel buttons_panel=new LinearPanel(00, 00, 350, 40);
        buttons_panel.setOrientarion(Orientation.HORIZONTAL);
        buttons_panel.addComponent(new Button(170,40,"Zaloguj"){
            @Override
            public void onClick() {
                TextBox tlogin=(TextBox)korzen.getComponent(1).getComponent(1);
                TextBox tpass=(TextBox)korzen.getComponent(2).getComponent(1);
                if(dt.autoryzacja_uzytkownika(tlogin.getText(), tpass.getText())){
                    System.out.println("Puszczamy typa");
                }
            }
        });
        buttons_panel.addComponent(new Button(170,40,"Wyjdz"){
            @Override
            public void onClick() {
                System.exit(0);
            }
        });

        lp.addComponent(buttons_panel);

        korzen=lp;
        System.out.println("kuniec");
    }

    private void stworz_panelGlowny(){
        LinearPanel glowny=new LinearPanel(220,0,50,80);


    }




}

Upvotes: 0

Views: 38

Answers (1)

camickr
camickr

Reputation: 324207

the problem is that when i try to create new DataLayer() in GUI class the panel doesn show until i resize screen

When you add (or remove) components from a visible GUI the basic code is:

panel.add(...);
panel.revalidate(); // to invoke the layout manager
panel.repaint(); // to paint the components.

even after that the keylistener doesn't work.

Probably because some other component has focus and KeyEvents area only dispatched to the component with focus. Try using the requestFocusInWindow() method on the panel.

panel.requestFocus

Upvotes: 2

Related Questions