How to check if a button is clicked in another Jframe - java

So i have two simple jframes, one is the main frame and the other is visible only when a button is pressed.
What I'm trying to do now is to display which button is being pressed in the second jframe, whether its toy or food in the jlabel in the first jframe.
The button launch selection in the first jframe will link to the second jframe, then the user clicks one of the two button and the button that was clicked will be displayed in the jlabel such as "Toy button was clicked"
I implemented how the two jframes linked by:
class SelectionListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
Object_Selection object_select = new Object_Selection(); //launch the second jframe
object_select.setVisible(true);
}
}
But I'm having issue on displaying which button was pressed in the second jframe in the jlabel of the first jframe.

Here an one-file mcve (copy paste the entire code into one file OpenDialogWindow.java, and run) demonstrating what you want to achieve:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class OpenDialogWindow {
public static void main(String[] args) {
JFrame frame = new JFrame("Main Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(400,250);
frame.add(new Pane());
frame.pack();
frame.setVisible(true);
}
}
class Pane extends JPanel{
private static int WIDTH = 300, HEIGHT = 100, GAP = 5;
private final JLabel label;
Pane() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setLayout(new BorderLayout(GAP,GAP));
label = new JLabel("", JLabel.CENTER);
add(label, BorderLayout.PAGE_START);
JButton show = new JButton("Show Dialog");
show.addActionListener(e-> new Diag(new DiagButtonListener()));
add(show, BorderLayout.PAGE_END);
}
class DiagButtonListener implements ActionListener{
#Override
public void actionPerformed(ActionEvent e) {
label.setText("Diag button clicked !");
}
}
}
class Diag extends JDialog {
public Diag(ActionListener listener) {
setTitle("Dialog window");
setSize(300, 150);
setLocation(450,400);
JButton btn = new JButton("Click");
btn.addActionListener(listener);
add(btn, BorderLayout.NORTH);
JLabel help = new JLabel("Click button and see parent frame updted", JLabel.CENTER);
add(help, BorderLayout.SOUTH);
setVisible(true);
}
}

Related

Click a button to open a new JFrame / GUI,

I seem to not be able to find a way to get my code to work.
I am making a program and until now everything was working, i have some buttons and they do what they should.
But now i added a button that when a user click it, it should close the current GUI and open a new one.
I also want to point out that i created a new class for this new GUI.
The other GUI class that i want to call is the GuiCrafting, in that class the GUI is also all coded, and works if i call it on the Main.
My question is what do i type here (I tried a lot of things like dispose() etc but i just get error messages) :
public void actionPerformed(ActionEvent event) {
if( str.equals("Crafting")){
//insert code to call the GuiCrafting class and open his GUI
}
Thanks in advance and if you need something more please let me know.
Multiple JFrames are frowned upon as you can read about here and here
Perhaps what you want to use is a CardLayout which manages two or more components (usually JPanel instances) that share the same display space.
After clicking the button "Goto Card 2"
TestApp.java:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestApp {
final static String CARD1 = "Card1";
final static String CARD2 = "Card2";
public TestApp() {
initComponents();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(TestApp::new);
}
private void initComponents() {
JFrame frame = new JFrame("TestApp");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create the panel that contains the "cards".
JPanel cards = new JPanel(new CardLayout());
// card 1 components
JButton buttonGotoCard2 = new JButton("Goto Card 2");
buttonGotoCard2.addActionListener((ActionEvent e) -> {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, CARD2);
});
// create card 1
JPanel card1 = new JPanel();
card1.add(new JLabel("Card 1"));
card1.add(buttonGotoCard2);
// card 2 components
JButton buttonGotoCard1 = new JButton("Goto Card 1");
buttonGotoCard1.addActionListener((ActionEvent e) -> {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, CARD1);
});
// create card 2
JPanel card2 = new JPanel();
card2.add(new JLabel("Card 2"));
card2.add(buttonGotoCard1);
// add cards to cards panel
cards.add(card1, CARD1);
cards.add(card2, CARD2);
frame.getContentPane().add(cards, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
There is also a JDialog which could be what you want.
HOWEVER
You can easily do something like that (Open a JFrame from another If you must):
TestApp.java:
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class TestApp {
public TestApp() {
initComponents();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(TestApp::new);
}
private void initComponents() {
JFrame mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
JLabel label = new JLabel("JFrame 1");
JButton button = new JButton("Open JFrame 2");
button.addActionListener((ActionEvent e) -> {
this.showNewJFrame(new WindowAdapter() {
#Override
public void windowClosing(java.awt.event.WindowEvent e) {
// here we listen for the second JFrame being closed so we can bring back the main JFrame
mainFrame.setVisible(true);
}
});
// hide the main JFrame
mainFrame.setVisible(false);
});
panel.add(label);
panel.add(button);
mainFrame.add(panel);
mainFrame.pack();
mainFrame.setVisible(true);
}
private void showNewJFrame(WindowAdapter windowAdapter) {
JFrame frame2 = new JFrame();
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // we dont wnat to exit when this JFrame is closed
JPanel panel2 = new JPanel();
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel2.setBorder(new EmptyBorder(10, 10, 10, 10));
JLabel label2 = new JLabel("JFrame 2");
panel2.add(label2);
frame2.add(panel2);
frame2.addWindowListener(windowAdapter);
frame2.pack();
frame2.setVisible(true);
}
}
This produces:
and when the "Open JFrame 2" is clicked:
and when JFrame 2 is closed it brings back the main JFrame via the WindowAdapter#windowClosing.

how to get jpanel components in another jframe class

I have a JFrame call availabilty details. In this frame I have a JPanel and a button. In that JPanel I have two toggle buttons. When I click the button it goes to a new JFrame call reservation.
I need java code for this. That when I select two toogle buttons and press the button, it wants to display the toggle button name and JPanel name in reservation..
here is my availabiltydetails frame.
I want that toogle button name and jpanel name wants to display in reservation frame when I clicked the button.
and here this is the code i already typed in availabilitydetails on add button event
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Reservation r = new Reservation();
Reservation.bf.getSelectedItems(this.b.getValue());
r.setVisible(true);
this.dispose();
}
is the code is wrong??
The Code Like This:
JFrameT.java:
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JFrameT extends JFrame {
private JButton testButton;
private JPanel panelMain;
private JPanelOne panel;
public JFrameT() {
// setting up JFrame
setLayout(null);
setPreferredSize(new Dimension(420, 90));
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// creating main JPanel (white)
panelMain = new JPanel();
panelMain.setBackground(Color.WHITE);
panelMain.setBounds(0, 0, 420, 90);
panelMain.setPreferredSize(new Dimension(200, 40));
add(panelMain);
// creating JButton in the main JPanel (white)
testButton = new JButton("Button from main class");
panelMain.add(testButton);
// creating new JPanelOne object from JPanelOne class containing black JPanel
panel = new JPanelOne();
// adding black JPanel to main JPanel (white)
panelMain.add(panel);
pack();
}
public static void main(String[] arguments) {
//Creat JFrame object and setting it visible
JFrameT frame = new JFrameT();
frame.setVisible(true);
}
}
JPanelOne.java:
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class JPanelFirst extends JPanel
{
public JPanelFirst()
{
// setting up black JPanel
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(220, 40));
panel.setBackground(Color.BLACK);
// creating button on external JPanel
JButton button = new JButton("Button (+JPanel) from external class");
// adding button to the black JPanel
panel.add(button);
// adding blackJPanel
add(panel);
}
}
You can find it helpful more in this problem.

Text in JLabel doesn't get updated on pressing a JButton

I am working on a project but the program seems to have a bug that I can not find.
Here is an MCVE which reproduces the problem:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class SO{
JLabel label;
JButton button;
JPanel panel;
JFrame frame;
public static void main(String[] args){
new SO().start();
}
public void start()
{
label = new JLabel("Button not pressed");
button = new JButton("Press me");
frame = new JFrame();
panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
panel.add(label);
panel.add(button);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Button was pressed");
label = new JLabel("Button is pressed"); //Doesn't work
frame.repaint();
}
});
}
}
The above program has a JLabel with some text and a JButton both of which are added into a JPanel which in turn is added to a JFrame.
When the button is pressed, I want the text in the JLabel to change. But the text doesn't get changed despite the println executing every time I press the button.
What is the problem here?
You are creating a new object of the JLabel on clicking on the button but not adding it to the JPanel or JFrame after that.
In spite of creating new object i.e.
label = new JLabel("Button is pressed")
do something like,
label.setText("Button is pressed");
More Info
change
label = new JLabel("Button is pressed");
to
label.setText("Button is pressed");
you don't need to create and assign new lable each time.just change the text
You can change that line to label.setText("Button is pressed"); to make this work.
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class SO{
JLabel label;
JButton button;
JPanel panel;
JFrame frame;
public static void main(String[] args){
new SO().start();
}
public void start()
{
label = new JLabel("Button not pressed");
button = new JButton("Press me");
frame = new JFrame();
panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
panel.add(label);
panel.add(button);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Button was pressed");
label.setText("Button is pressed"); //Doesn't work
frame.repaint();
}
});
}
}

Swing Card Layout change panel displayed on action by user

I'm currently writing an interface where a have a JFrame class and two JPanel classes. When the script is first executed, Panel A is shown. I have a JButton in Panel A which I would like, when clicked, to display Panel B instead of Panel A.
Is there any way I could do this?
Read tutorial for that.
You can use next() method of CardLayout for showing next card,
or you can use show(...); for showing specific card.
Simple example:
import java.awt.BorderLayout;
import java.awt.CardLayout;
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 Example {
public static void main(String[] args){
JFrame frame = new JFrame();
final JPanel panel = new JPanel(new CardLayout());
JLabel l1 = new JLabel("1");
JLabel l2 = new JLabel("2");
JLabel l3 = new JLabel("3");
panel.add(l1,"l1");
panel.add(l2,"l2");
panel.add(l3,"l3");
JButton btn = new JButton("next");
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
CardLayout layout = (CardLayout) panel.getLayout();
layout.next(panel);
}
});
JButton btnSpec = new JButton("l3");
btnSpec.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
CardLayout layout = (CardLayout) panel.getLayout();
layout.show(panel, "l3");
}
});
frame.add(panel);
frame.add(btn,BorderLayout.SOUTH);
frame.add(btnSpec,BorderLayout.NORTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

how to display different screen

I'm writing a simple program using CardLayout. The main screen should display a button which when pressed would go to the next screen which contains another button for another screen. My problem is that when I run my program the screen is black. I tried following tutorials online to write my own program but I don't seem to find the problem with my code. I don't get any errors when run. Here is my code
//using CardLayout to change screen when action is performed
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Popup;
import javax.swing.JOptionPane;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
public class CL extends JFrame {
JPanel cardPanel;
JPanel cardPanelA;
JPanel cardPanelB;//to set different screens
CardLayout cl;
private JButton button1;
private JButton button2;
private JButton change;
private JLabel label;
private JTextField textField1;
private JTextField textField2;
JButton button;
public CL() {
super("This is a sample");
cardPanel = new JPanel();
cardPanelA = new JPanel();
cardPanelB = new JPanel();
cl = new CardLayout();
cardPanel.setLayout(cl);
button1 = new JButton("button1");
button2 = new JButton("button2");
change = new JButton("change screen");
label = new JLabel("this is a label");
textField1 = new JTextField(10);
textField2 = new JTextField("enter text", 6);
cardPanelA.add(change);
cardPanelA.add(label);
cardPanelA.add(textField1);
cardPanelA.add(textField2);
cardPanelB.add(button1);
cardPanelB.add(button2);
cardPanel.add(cardPanelA);
cardPanel.add(cardPanelB);
JPanel panel1 = new JPanel();
button = new JButton("initial button");
panel1.add(button);
theHandler handler = new theHandler();//make action listener
change.addActionListener(handler);
button1.addActionListener(handler);
button2.addActionListener(handler);
button.addActionListener(handler);
/*
getContentPane().add(panel1, BorderLayout.NORTH);
getContentPane().add(cardPanelA, BorderLayout.NORTH);
getContentPane().add(cardPanelB, BorderLayout.NORTH);
*/
}
private class theHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button) {
cl.show(cardPanel, "Panel A");
}
if (event.getSource() == change) {
cl.show(cardPanelB, "panelB");
}
if (event.getSource() == button2) {
cl.show(cardPanel, "PanelA");
}
if (event.getSource() == button1) {
JOptionPane.showMessageDialog(null, "this is the second screen");
}
}
}
}
/*way to use CardLayout: create a CardLayout manager and create a bunch of different JPanels which
* would each be a different screen. Make a panel that stores the CardLayout as the layout.
* Add the different elements to each Panel(buttons, textfields) and then add the panels to the JPanel that stores
* the CardLayout
*/
import javax.swing.JFrame;
public class CardTest {
public static void main(String[] args) {
CL object = new CL();
object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
object.setSize(400, 400);
object.setVisible(true);
}
}
It might be something simple but I'm not sure of what it is. Some advice would be appreciated.
Make sure you add your panels to the frame
add(cardPanel);
Without that no components will be shown

Categories

Resources