Elfoc
Elfoc

Reputation: 3689

Several similar panels GUI Java

I'm creating GUI and I don't know how to resolve my problem. What I'd like to do is to create several panels from PanelClass like i did in Main.

I don't know how:

  1. Name buttons in my Panels and gave them some functionality (Like i was trying with button b1)
  2. Add to panel3 additional labels, and buttons.

My main class

public class Main {

    JFrame f;
    PanelClass panel1, panel2, panel3;
    JButton b1, b2;

    public Main() {

        b1 = new JButton("asasa");

        f = new JFrame();
        f.setSize(300, 300);
        f.setLayout(new GridBagLayout());
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel1 = new PanelClass(b1, b2, panel1);
        panel2 = new PanelClass(b1, b2, panel2);
        panel3 = new PanelClass(b1, b2, panel3);

        f.add(panel1);
        f.add(panel2);
        f.add(panel3);
    }

    public static void main(String[] args) {    
        Main m = new Main();    
    }    
}

My Panel class

public class PanelClass extends JPanel {

    public PanelClass(JButton btn, JButton btn1, JPanel p) {

        super();
        p = new JPanel(new GridBagLayout());
        btn = new JButton();
        btn1 = new JButton();

        GridBagConstraints c = new GridBagConstraints();

        c.gridx = 0;
        c.gridy = 0;
        p.add(btn, c);
        c.gridx = 0;
        c.gridy = 1;
        p.add(btn1, c);
        add(p);
    }
}

Upvotes: 3

Views: 191

Answers (3)

Andrew Thompson
Andrew Thompson

Reputation: 168815

  1. A component can only exist in one visible container at a time (unless it is being used as a flyweight renderer). So putting the same buttons into 3 panels will not work.
  2. The code passes a button(s) in the constructor of PanelClass which is(are) ignored. Instead 2 new button instances are created. Just assign the passed buttons to the ..I was going to say 'class level attributes' when I noted they were not. Entirely remove btn = new JButton(); and the text passed in the button constructor will appear.
  3. For events, see How to Write an Action Listener.

Upvotes: 5

Aniket Inge
Aniket Inge

Reputation: 25695

To add "events" like click and mouse hovers etc - you must implement the correct "Listener" for the Widget. Go through any good tutorial on Swing and it will tell you everything about it. Widgets on screen are regular objects as well, so they can be added to "Collections", iterated and played around with, like regular objects. Take note of THREAD complexities and warning 'Cross-Thread invocation is Injurious To Your Program'.

Upvotes: 0

logoff
logoff

Reputation: 3446

Try to read the official Swing tutorial. It explains how to add panels, labels, etc.

To edit label names you could use setText(String name) method. To add functionalities to buttons you must implement a listener in each one. Add labels like you do in other panels, I don't see the problem.

Upvotes: 0

Related Questions