Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java
Hi, everyone, please explain displaying JPanel, ON button click in the same Frame
 
i've displayed a new JFrame with respect to button click but not JPanel
 
here's my code but it didn't work!
 

import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
 

public class buttons extends JPanel{
    public static void main (String args []) {
    
        JFrame frame = new JFrame();
        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EX…
        frame.setTitle("BULLS EYE");
        JPanel panel = new JPanel();
        JButton button1 = new JButton("click me");
        JButton button2 = new JButton("second button");
        
        
        panel.add(button1);
        panel.add(button2);
        
        frame.add(panel);
        
        button1.addActionListener( new Action1());
        button2.addActionListener(new Action2());
    }
    static class Action1 implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            JFrame Frame1 = new JFrame();
            JPanel panel1 = new JPanel();
            JLabel label1 = new JLabel("OM NAMA SHIVAYA");
            Frame1.setVisible(true);
            Frame1.setSize(300,300);
            Frame1.setDefaultCloseOperation(JFrame.E…
            panel1.add(label1);
            Frame1.add(panel1);
        }
    
    }
        
    static class Action2 implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            JPanel panel2 = new JPanel();
            
            JLabel label2 = new JLabel(" NARAYANAYA GANAPATHI ");
            GridBagConstraints c = new GridBagConstraints();
            c.gridx=0;
            c.gridy=0;
            panel2.add(label2,c);
            panel2.setSize(50,50);
            
        }
    }
    
}
Posted 23-Sep-12 1:16am
Edited 23-Sep-12 2:05am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This code is total chaos.
 
- you are extending JPanel and then using JFrame to build your GUI.
delete the "extends JPanel" or use "extends JFrame" and refer to it via "this".
 
public class MyFrame extends JFrame{
 
  public myFrame(){
    // create GUI
    this.setSize(500, 500);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EX…
    this.setTitle("BULLS EYE");
 
    JButton oButton = new JButton("Click me hard!");
    oButton.addActionListener(new ButtonAction());
    this.add(oButton);
  }
 
  public static void main (String args []) {
    // start action here by creating the JFrame
    MyFrame oFrame = new MyFrame();
    oFrame.setVisible(true);
  }
 
}
 
- you are creating static classes for the Actions because you are too lazy to move the JFrame out of the main method.
Change that.
 
public class MyFrame extends JFrame{
  // ...

  public ButtonAction implements ActionListener{
    public void actionPerformed(ActionEvent e) {
 
    }
  }
}
 
For the Action:
You can refer to the original object (the JButton) via oEvent.getSource(). You will need to cast that given Object that to get the JButton back:
 
public ButtonAction implements ActionListener{
  public void actionPerformed(ActionEvent oEvent) {
      if(oEvent.getSource instanceOf JButton){ // check if Action is triggered by JButton
        JButton oButton = (JButton)oEvent.getSource(); // get Button (if needed)
        
        MyFrame.this.doFunnyThings(); // call function of the JFrame
      }
    }
  }
}
 
You can also define a ActionCommand for the button to identify the trigger of the action.
Please do a little search on the web for that.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You are creating new instances of objects within your action listeners. However, as soon as these methods terminate those objects go out of scope and are destroyed. You need to make the objects part of the main class.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 400
1 Jochen Arndt 200
2 Richard MacCutchan 135
3 DamithSL 95
4 Maciej Los 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 24 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100