Open another panel inside another panel after pressing button - java

I have been getting an error saying that i didn't add some methods(the action performed) but i already did. I'm having trouble opening the panel2.
public class panel1 extends JPanel implements ActionListener(){
private panel2 p2=new panel2();
private JButton button;
public panel1(){
button=new JButton("open panel2");
add(button,BorderLayout.BEFORE_FIRST_LINE);
button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
add(p2);
}
});
}
}

Consider these changes:
public class panel1 extends JPanel implements ActionListener(){
private panel2 p2=new panel2();
private JButton button;
public panel1(){
button=new JButton("open panel2");
add(button,BorderLayout.BEFORE_FIRST_LINE);
button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
//Add readability: Where to add?
panel1.this.add(p2);
}
});
}
//THIS HERE makes your panel1 implment ActionListener
#Override
public void actionPerformed(ActionEvent ae) {
}
}
Please also note commom Java naming conventions - Class names should start with uppercase letter (Panel1 extends JPanel)

You can check the below code for replacing jpanel without switching jframe.
contentPanel.removeAll();
contentPanel.repaint();
contentPanel.revalidate();
contentPanel.add(//add your panel here);
contentPanel.repaint();
contentPanel.revalidate();

Related

How to call JButton from other class java swing

I have a GUI class which contains a JButton:
public class GUI {
static JFrame mainFrame;
static JLabel headerLabel;
static JLabel statusLabel;
static JPanel controlPanel;
static JButton sub;
public GUI(){
prepareGUI();
}
public static void prepareGUI(){
mainFrame = new JFrame("Service 2 - Evaluate Sensor Values");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
sub = new JButton("Send Subscribe to Service 1");
showButtonDemo();
mainFrame.setVisible(true);
}
public static void showButtonDemo(){
headerLabel.setText("Configuration Phase");
headerLabel.setFont(new Font("Serif", Font.PLAIN, 14));
sub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Monitor.createWaterSubscriptionResources();
Monitor.createTemperatureSubscriptionResources();
}
});
controlPanel.add(sub);
mainFrame.setVisible(true);
}
}
In other class, I will process something, and when the process done, I would like to disable the button in the GUI class, something like:GUI.sub.setEnabled(false). How can I do that?
Thank you in advanced!
In your other class event, you call Frame.getFrames() that returns an array of all frames. Then you get your GUI frame and call GUI.sub.setEnabled(false)
You should put a getter method for your JButton in the Gui class, Like this:
public static JButton getSubButton(){
return this.sub;
}
and to disable the Button from another class you do this:
Gui.getsubButton().setEnabled(false);

How do i add an action to a JFrame JButton?

I have a JFrame gui im making and I need help.
Im trying to find out how to add a action to my button.
as in using it in a "if" statement or make i print something out when you push it.
thanks!
code:
package agui;
import javax.swing.*;
public class Agui extends JFrame {
public Agui() {
setTitle("My Gui");
setSize(400, 400);
JButton button = new JButton("click me");
JPanel panel = new JPanel();
panel.add(button);
this.getContentPane().add(panel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Agui a = new Agui();
}
}
You want the "addActionListener" method, something like:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("You clicked the button");
}
});

How to change CardLayout panels from a separate panel?

My software layout is kinda wizard-base. So the base panel is divided into two JPanels. One left panel which never changes. And one right panel that works with CardLayout. It has many sub-panels and show each one of them by a method.
I can easily go from one inner panel to another one. But I want to have a button in left panel and change panels of the right side.
Here is a sample code which you can run it:
BASE:
public class Base {
JFrame frame = new JFrame("Panel");
BorderLayout bl = new BorderLayout();
public Base(){
frame.setLayout(bl);
frame.setSize(800, 600);
frame.add(new LeftBar(), BorderLayout.WEST);
frame.add(new MainPanel(), BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
new Base();
}
}
Left side
public class LeftBar extends JPanel{
JButton button;
MainPanel mainPanel = new MainPanel();
public LeftBar(){
setPreferredSize(new Dimension(200, 40));
setLayout(new BorderLayout());
setBackground(Color.black);
button = new JButton("Show Second Page");
button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
mainPanel.showPanel("secondPage");
}
});
add(button, BorderLayout.NORTH);
}
}
Right Side
public class MainPanel extends JPanel {
private CardLayout cl = new CardLayout();
private JPanel panelHolder = new JPanel(cl);
public MainPanel(){
FirstPage firstPage = new FirstPage(this);
SecondPage secondPage = new SecondPage(this);
setLayout(new GridLayout(0,1));
panelHolder.add(firstPage, "firstPage");
panelHolder.add(secondPage, "secondPage");
cl.show(panelHolder, "firstPage");
add(panelHolder);
}
public void showPanel(String panelIdentifier){
cl.show(panelHolder, panelIdentifier);
}
}
Inner panels for right side:
public class FirstPage extends JPanel {
MainPanel mainPanel;
JButton button;
public FirstPage(MainPanel mainPanel) {
this.mainPanel = mainPanel;
setBackground(Color.GRAY);
button = new JButton("Show page");
button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
mainPanel.showPanel("secondPage");
}
});
add(button);
}
}
public class SecondPage extends JPanel{
MainPanel mainPanel;
JButton button;
public SecondPage(MainPanel mainPanel){
this.mainPanel = mainPanel;
setBackground(Color.white);
add(new JLabel("This is second page"));
}
}
And this is a picture to give you the idea:
As I explained, I can travel "from first" page to "second page" by using this method: mainPanel.showPanel("secondPage"); or mainPanel.showPanel("firstPage");.
But I also have a JButton in the left bar, which I call the same method to show the second panel of the CardLayout. But it does not work. It doesnt give any error though.
Any idea how to change these CardLayout panels from outside of panels?
The problem is that LeftBar has mainPanel member that is initialized to a new instance of MainPanel. So you have two instances of MainPanel, one allocated in Base and added to the frame, the other one allocated in LeftBar.
So LeftBar executes mainPanel.showPanel("secondPage"); on a second instance of MainPanel which is not even a part of a visual hierarchy. To fix this just pass an existing instance of MainPanel to the constructor of LeftBar. You already do this in FirstPage and SecondPage.

How to declare just one

I have the following working code with two buttons as event listeners. One button to get and display a Panel panel1 and the other to get and display another Panel panel2 removing the existing displayed panel. I created two actionPerformed methods for each button to carry out each other's tasks. I just want to make one to shorten the code but I don't know how to detect which button in a panel is displaying at compile time. Any help will be appreciated.
//Switching between panels (screens)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ScreenDemo extends JFrame {
private JPanel panel1, panel2;
private JButton btn1, btn2;
JLabel label1 = new JLabel("Screen 1");
JLabel label2 = new JLabel("Screen 2");
public ScreenDemo() {
createPanel(); //Created at Line 14
addPanel(); //Created at Line 28
}
private void createPanel() {
//Panel for first screen
panel1 = new JPanel(new FlowLayout());
btn1 = new JButton("Move to Screen 2");
btn1.addActionListener(new addScreen1ButtonListener());
//Panel for second screen
panel2 = new JPanel(new FlowLayout());
btn2 = new JButton("Move to Screen 1");
btn2.addActionListener(new addScreen2ButtonListener());
}
private void addPanel() {
panel1.add(label1);
panel1.add(btn1);
panel2.add(label2);
panel2.add(btn2);
add(panel1); //Add the first screen panel
}
class addScreen1ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
getContentPane().removeAll();
getContentPane().add(panel2);
repaint();
printAll(getGraphics()); //Prints all content
}
}
class addScreen2ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ea) {
getContentPane().removeAll();
getContentPane().add(panel1);
repaint();
printAll(getGraphics()); //Prints all content
}
}
public static void main(String[] args) {
ScreenDemo screen = new ScreenDemo();
screen.setTitle("Switching Screens");
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setSize(500, 500);
screen.setVisible(true);
}
}
implement ActionListener for your class, and try this code
ScreenDemo extends JFrame implements ActionListener
...
btn1.addActionListener(this);
btn2.addActionListener(this);
...
#override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource().equals(btn1)) {
...
}
else if(ae.getSource().equals(btn1)) {
...
}
}
you have to implements the class from ActionListener
than register JButton like : btn.addActionListener(this);
so close now, You just need to capture the event and get the source from where it is called like:
public void actionPerformed(ActionEvent ae) {
if(ae.getSource == btn1){
//shuffle panel from btn1
}
if(ae.getSource == btn2) {
//shuffle panel from btn2
}
}

Add JComponent to applet's pane

I am trying to add an JComponent (label, for instance) to the applet pane when the button is pressed. I have the following piece of code:
public class TestApplet extends JApplet
{
#Override
public void init()
{
setLayout(new FlowLayout());
JButton bt = new JButton("hit it");
bt.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent ae)
{
// getContentPane().setFont(null);
getContentPane().add(new JLabel("to the right"));
}
});
add(bt);
}
}
This does not make the label visible, unless I uncomment getContentPane().setFont(null);
Please advise how should I display the label properly. Thanks in advance.
getContentPane().add(new JLabel("to the right"));
this.revalidate();

Categories

Resources