how to get jpanel components in another jframe class - java

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.

Related

Jpanel textbox no output

I'm working in java using Jpanel and my work is compiling fine however is showing no output. hopefully, someone could tell me why this is. I'm using jscrollpane and I'm calling it at the end idk if it's something to do with the listener or what.
FileDemoPanel.java
package Tutoiral03Task01;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FileDemoPanel extends JPanel implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton openBtn, saveBtn;
JTextArea workTa;
openBtn = new JButton ("Open");
openBtn.setEnabled (false);
openBtn.setMnemonic('g');
openBtn.setToolTipText("open button");
setLayout(new BorderLayout());
saveBtn = new JButton ("Save");
saveBtn.setEnabled (false);
saveBtn.setMnemonic('f');
saveBtn.setToolTipText("Save button");
JTextArea logTA = new JTextArea (5, 100);
logTA.setEditable(false);
logTA.setBackground(Color.lightGray);
logTA.setMargin(new Insets(5,5,5,5));
JScrollPane logScrollPane = new JScrollPane(logTA);
add(logScrollPane);
}
}
FileDemo.java
package Tutoiral03Task01;
import javax.swing.*;
public class FileDemo {
public static void main (String[] args){
JFrame frame = new JFrame("Working with files");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new FileDemoPanel());
frame.pack();
frame.setVisible(true);
}
}
The problem is that you create all the buttons and others in a actionperformed method.
This is wrong, because that is used as a ButtonListener, so if you dont press a button nothing will happened. We use to write the GUI frame in the constructor of the class.Then we create an object type of the GUI class. So i think i fixed it and i did some extra changes to make the program more simple. The step i didnt do is to add a ButtonListener, so Buttons do nothing.
i wish it will helps you.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FileDemoPanel extends JFrame {
private JPanel panel = new JPanel();
private JButton openBtn = new JButton("Open");
private JButton saveBtn = new JButton ("Save");
private JTextArea workTa;
public FileDemoPanel(){
openBtn.setEnabled (false);
openBtn.setMnemonic('g');
openBtn.setToolTipText("open button");
setLayout(new BorderLayout());
saveBtn.setEnabled (false);
saveBtn.setMnemonic('f');
saveBtn.setToolTipText("Save button");
JTextArea logTA = new JTextArea (5, 100);
logTA.setEditable(false);
logTA.setBackground(Color.lightGray);
logTA.setMargin(new Insets(5,5,5,5));
JScrollPane logScrollPane = new JScrollPane(logTA);
panel.add(openBtn);
panel.add(saveBtn);
panel.add(logTA);
panel.add(logScrollPane);
this.setContentPane(panel);
this.setVisible(true);
this.setResizable(true);
this.setSize(350,150);
this.setTitle("Κεντρική Σελίδα");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
And the mainclass.As you can see is too small.
public class FileDemo {
public static void main (String[] args){
new FileDemoPanel();
}
}

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 check if a button is clicked in another Jframe

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);
}
}

Adding an action listener to JButtons created by a loop

I am having trouble finding a way to invoke an action listener that returns the value of the button clicked in the text area at the bottom.
I made the buttons using a for loop and did not expressly give the buttons a name so I do not know how to reference them when trying to incorporate an ActionListener.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class buttoner implements ActionListener {
//JFrame
JFrame frame = new JFrame("Button Game");
//Make JPanels
JPanel panelLabel = new JPanel();
JPanel buttonGrid = new JPanel(new GridLayout(0,10));
JPanel bottomPanel = new JPanel();
//JLabel
private JLabel label1 = new JLabel("The Button Game");
public buttoner() {
//set layout
frame.setLayout(new BorderLayout());
frame.add(panelLabel, BorderLayout.NORTH);
frame.add(buttonGrid, BorderLayout.CENTER);
frame.add(bottomPanel, BorderLayout.SOUTH);
//Set stuff
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,700);
frame.setVisible(true);
//Change label color
label1.setForeground(Color.RED);
//add Label
panelLabel.add(label1);
//add Buttons
for (int i = 1; i <= 60; i++) {
String val = Integer.toString(i);
buttonGrid.add(new JButton(val));
}
//Add JText Area to bottom JPanel
String num = "value";
JTextArea jta = new JTextArea(num, 1, 1);
bottomPanel.add(jta);
frame.pack();
}
public static void main(String args[]){
buttoner gui = new buttoner();
}
public void actionPerformed(ActionEvent a) {
}
}
I created an action listener to put the value in the text area at the bottom of the GUI.
I fixed a few problems with your code.
In the main method, I called the SwingUtilities invokeLater method to put the Swing GUI on the Event Dispatch thread (EDT). Swing components must be created and updated on the EDT.
The name of a Java class must start with a capital letter.
It's safer to put your Swing components on a JPanel, rather than add them directly to a JFrame.
I separated the code that creates the JFrame from the code that creates the JPanels. It should be easier for any reader of your code, including yourself, to understand what's going on.
In the createMainPanel method, I grouped the code so that everything having to do with the buttonGrid JPanel, to take one instance, is in one place in the code.
I added the action listener to the code that creates the buttonGrid JPanel.
I wrote action listener code that updates the JTextArea with the left clicked button label.
Here's the corrected code.
package com.ggl.testing;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
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;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class Buttoner implements ActionListener {
// JFrame
private JFrame frame = new JFrame("Button Game");
// Make JPanels
private JPanel panelLabel = new JPanel();
private JPanel buttonGrid = new JPanel(new GridLayout(0, 10));
private JPanel bottomPanel = new JPanel();
// JLabel
private JLabel label1 = new JLabel("The Button Game");
private JTextArea jta;
public Buttoner() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
// Change label color
label1.setForeground(Color.RED);
// add Label
panelLabel.add(label1);
panel.add(panelLabel, BorderLayout.NORTH);
// add Buttons
for (int i = 1; i <= 60; i++) {
String val = Integer.toString(i);
JButton button = new JButton(val);
button.addActionListener(this);
buttonGrid.add(button);
}
panel.add(buttonGrid, BorderLayout.CENTER);
// Add JText Area to bottom JPanel
String num = "value";
jta = new JTextArea(num, 1, 1);
jta.setEditable(false);
bottomPanel.add(jta);
panel.add(bottomPanel, BorderLayout.SOUTH);
return panel;
}
public static void main(String args[]) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new Buttoner();
}
};
SwingUtilities.invokeLater(runnable);
}
public void actionPerformed(ActionEvent a) {
JButton button = (JButton) a.getSource();
jta.setText(button.getText());
}
}
Try creating an array of buttons and add the newly created button to the array. See comments.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class buttoner implements ActionListener {
//JFrame
JFrame frame = new JFrame("Button Game");
//Make JPanels
JPanel panelLabel = new JPanel();
JPanel buttonGrid = new JPanel(new GridLayout(0,10));
JPanel bottomPanel = new JPanel();
//JLabel
private JLabel label1 = new JLabel("The Button Game");
private JButton buttons[] = new JButton[60]; //create an array of button for future reference
public buttoner() {
//set layout
frame.setLayout(new BorderLayout());
frame.add(panelLabel, BorderLayout.NORTH);
frame.add(buttonGrid, BorderLayout.CENTER);
frame.add(bottomPanel, BorderLayout.SOUTH);
//Set stuff
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,700);
frame.setVisible(true);
//Change label color
label1.setForeground(Color.RED);
//add Label
panelLabel.add(label1);
//add Buttons
for (int i = 1; i <= 60; i++) {
String val = Integer.toString(i);
JButton btn = new JButton(val);
btn.addActionListener(this); //add an actionListener right away
buttons[i] = btn; //add the button in the array for future reference
buttonGrid.add(btn);
}
//Add JText Area to bottom JPanel
String num = "value";
JTextArea jta = new JTextArea(num, 1, 1);
bottomPanel.add(jta);
frame.pack();
}
public static void main(String args[]){
buttoner gui = new buttoner();
}
public void actionPerformed(ActionEvent a) {
}
}

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