setVisible() method not working for JButton placed on JPanel - java

In my Swing application I have MainFrame with "Add Customer" Button. When I click on the "Add Customer" Button I want Customer form to appear while MainFrame disappearing. Customer form has only JTabbedPane. AddCustomerPanel is a separate class which has only cancel button. AddCustomerPanel has added to Customer form's JTabbedPane as a tab. When I click on cancel button I want Customer frame disappear and mainframe appear again.I tried using setVisible() method. But it didn't work. Please help me to do this.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MainFrame extends JFrame{
private JButton btnMain;
MainFrame(){
setSize(400,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
btnMain = new JButton("Add Customer");
btnMain.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new Customer().setVisible(true);
this.setVisible(false); // Not working
}
});
add(btnMain);
}
public static void main(String args[]){
new MainFrame();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Customer extends JFrame{
private JTabbedPane tabMain;
Customer(){
setSize(500,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
tabMain = new JTabbedPane();
tabMain.setPreferredSize(new Dimension(490,290));
tabMain.add("Add Customer",new AddCustomerPanel());
add(tabMain);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class AddCustomerPanel extends JPanel{
private JButton btnCancel;
AddCustomerPanel(){
setSize(400,200);
setVisible(true);
setLayout(new FlowLayout());
btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
this.setVisible(false); // Not working
new MainFrame().setVisible(true);
}
});
add(btnCancel);
}
}

I found the solution.
SwingUtilities.getWindowAncestor(this).setVisible(false);
new MainFrame().setVisible(true);

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

JPanel class is not adding to JFrame

I can't get my JFrame from main class to display JPanel from another class. Everything compiles without errors
This is my main class code which extends JFrame:
public OnlineCarSalesSystem(){
setTitle("Online Car Sales System");
setVisible(true);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(new Login());
}
public static void main(String[] args) {
new OnlineCarSalesSystem();
}
In the above code i have added add(new Login()); but it is not displaying that panel on my JFrame. And in the below code i extended my class with the JPanel. And this is the JPanel class code:
import java.awt.Color;
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.JPasswordField;
import javax.swing.JTextField;
public class Login extends JPanel{
JLabel loginLabel = new JLabel("Login ID");
JLabel passwordLabel = new JLabel("Password");
JTextField loginTextField = new JTextField();
JPasswordField passwordTextField = new JPasswordField();
JButton submitButton = new JButton("Submit");
JButton registration = new JButton("new Registration");
JLabel noaccountLabel = new JLabel("No Account yet!!!");
public void Login(){
setBounds(0,0,500,500);
setBackground(Color.red);
setVisible(true);
loginLabel.setBackground(Color.cyan);
passwordLabel.setBackground(Color.cyan);
loginTextField.setBounds(680, 103,90,20);
add(loginTextField);
loginLabel.setBounds(600, 100,90,30);
add(loginLabel);
passwordTextField.setBounds(680, 153,90,20);
passwordTextField.setEchoChar('*');
add(passwordTextField);
passwordLabel.setBounds(600, 150,90,30);
add(passwordLabel);
add(submitButton);
submitButton.setBounds(640,200,90,30);
submitButton.addActionListener(new ActionListener() { //////Submit Button
public void actionPerformed(ActionEvent e) {
}
});
add(registration);
registration.setBounds(638,270,96,30);
add(noaccountLabel);
noaccountLabel.setBackground(Color.cyan);
noaccountLabel.setBounds(640,250,90,30);
registration.addActionListener(new ActionListener() { //////registration Button
public void actionPerformed(ActionEvent e) {
}
});
}
}
The problem is that the Login()-function isn't executed at any point in code. You might want to change
public void Login() { ... }
to
public Login() { ... }
so the code gets executed on object initialization

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

Refresh JFrame? Java Swing

I don't know how to resolve this case:
I have a JFrame with JPanel on it. I added two JButtons to this JPanel.
Class MainFrame
import java.awt.Color;
import javax.swing.JFrame;
public class MainFrame extends JFrame{
public MainFrame(){
this.setSize(100,100);
MainPanel panel = new MainPanel();
this.add(panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
and MainPanel with two buttons
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class MainPanel extends JPanel implements ActionListener{
JButton button, example;
public MainPanel(){
this.setLayout(new BorderLayout());
JButton button = new JButton("New");
button.addActionListener(this);
JButton example = new JButton("example");
this.add(button, BorderLayout.NORTH);
this.add(example, BorderLayout.CENTER);
}
#Override
public void actionPerformed(ActionEvent event) {
if (event.getSource().equals(button)){
example.setEnabled(false);
example.setBackground(Color.yellow);
}
}
}
and start class Main
public class Main {
public static void main (String[] args){
MainFrame frame = new MainFrame();
}
}
What should I do to change background color second button?
You have your button variables defined twice, once as an instance variable and once as a local variable.
Get rid of the local variable:
//JButton example = new JButton("example");
example = new JButton("example");
Now your ActionListener code can reference the instance variable.
In your example:
JButton button, example; // <-- Here, you create your two (protected) variables
public MainPanel(){
this.setLayout(new BorderLayout());
JButton button = new JButton("New"); // <-- And here, you create a local variable
button.addActionListener(this);
JButton example = new JButton("example"); // <-- Here, another local variable
this.add(button, BorderLayout.NORTH);
this.add(example, BorderLayout.CENTER);
}
#Override
public void actionPerformed(ActionEvent event) {
if (event.getSource().equals(button)){
example.setEnabled(false);
example.setBackground(Color.yellow);
}
}

Removing panel from JTabbedPane via button on panel

I have 2 classes, mainFrame and panel. By clicking the button on mainFrame I call panel from another class and set it in tabbed pane which is in JFrame (mainFrame class). Now, I have another button (btnRemove) on my panel in panel class. So when I click that button I want to remove my panel from tabbed pane in mainFrame class. How do I write my listener properly?
mainFrame class:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class MainFrame extends JFrame {
JTabbedPane tPane = new JTabbedPane();
JButton btn = new JButton("Add panel");
public MainFrame(){
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(new BorderLayout());
add(tPane, BorderLayout.CENTER);
add(btn,BorderLayout.NORTH);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel p = new panel();
tPane.add("Panel",p);
}
});
}
public static void main(String[] args){
new MainFrame();
}
}
panel class:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Panel extends JPanel{
JButton btnRemove = new JButton("Remove panel");
public Panel(){
setLayout(new FlowLayout());
add(btnRemove);
btnRemove.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
}
}
panel.this.getParent().remove(panel.this);
If you want the code to keep working even if you nest the button inside a sub-panel, you should use the follwoing:
SwingUtilities.getAncestorOfClass(JTabbedPane.class, panel.this).remove(panel.this);
Side note: please respect Java naming conventions: classes start with upper-case letters.

Categories

Resources