enabling/disabling a JFrame after hiding another JFrame - java

I have this 2 JFrame classes:
public class Frame1 extends javax.swing.JFrame {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
private JButton button1;
button1 = new JButton("Open Frame2");
private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
this.setEnabled(false); // disable Frame1 until Frame2 is showing
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
Frame2 obj2 = new Frame2 ();
obj2.setVisible(true);
}
});
}
}
public class Frame2 extends javax.swing.JFrame {
setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
public Frame2 () {
this.setVisible(false);
}
Frame1 obj1 = new Frame1 ();
private JButton button2;
button2 = new JButton("Hide Frame2 and go to Frame1");
private void button2ActionPerformed(java.awt.event.ActionEvent evt) {
this.setVisible(false);
}
}
As you can see, when I click on button1 Frame1 gets disabled and Frame2 obj2 is created.
First of all I'd to know whether what I have done is a correct way to disable/defocus one parent JFrame to show another on top with focus.
And then I just need to enable Frame1 after hiding Frame2, something like obj1.setEnabled(true). How can I do that?

All what you have to do is to create an ref of your first class frame right inside it:
public 1_class_name obj;
//call the second class method getobj(o)
getobj(obj);
and then create a method and a ref in the second class frame:
1_class_name a;
public void getobj(1_class_name o){
a=o;
}
After you do that, you create a listener in the second class frame:
public void windowClosing(WindowEvent e) {
a.setEnabled(true);
dispose();
}
};
addWindowListener(exitListener);
It worked for me, I hope it will work for you too ;)

Related

opening a frame by clicking on the button

In my project I am trying to open LoginFrame from WelcomeFrame by clicking a button and I want myWelcomeFrameto be closed as well.
I have successfully opened theLoginFrameby usingsetVisible(true).
To close theWelcomeFrameI have writtenframe.SetVisible(false)where frame is the object ofWelcomeFrame` but this line shows an error: frame cannot be resolved ....
Here's my code.. Please help
public class WelcomeFrame extends JFrame{
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WelcomeFrame frame = new WelcomeFrame(); //object of WelcomeFrame
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
JButton btnNewButton = new JButton("Librarian Portal\r\n");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LoginFrame l=new LoginFrame();
l.setVisible(true);
frame.setVisible(false); //error:frame cannot be resolved
}
});
Because object of WelcomeFrame is not accessible.
You need to create another object before trying to call setVisible method.
WelcomeFrame closing_frame = new WelcomeFrame();
closing_frame.setVisible(false);
public class WelcomeFrame extends JFrame{
private JPanel contentPane;
private WelcomeFrame frame;
after
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new WelcomeFrame();
I think it'll help you
a CardLayout is more adapted to your needs; the general idea is to create a jPanel instead of a JFrame, the CardLayout enables you to switch between them by showing only one at a time. if this is what you need have alook here

opening only one window on actionPerformed

I've looked through topics on how to open only one window when a button is clicked but none of the solutions there helped, perhaps because my code was structured a bit differently.
So I have a main window class extending JFrame and one of the buttons is supposed to open a new window when clicked. I have defined the widgets/panels etc for the new window in a separate class. At the moment, every time I click on the button a new window is opened. I want to make it so that if a window is already opened then it would switch to that window once the button is clicked again.
Here is a bit of my code:
public class MainWindow extends JFrame{
/*
* create widgets and panels
*/
Button.addActionListener(new ActionListener() { // the button that opens
//a new window
#Override
public void actionPerformed(ActionEvent e) {
Window2 ww = new Window2(); //creating the new window here
}
});
}
NB. The Window2 class is also extending JFrame, if that's of any help.
Thanks
pull out ojbect creation from actionPerformed method beacuse each time you click button it's create new object. below can help you :-
Make a Window2 class singalton for more detail about singalton click here.
2 . add null check as below :-
....
Window2 ww = null; // static or instence variable
......
#Override
public void actionPerformed(ActionEvent e) {
if(ww==null)
{
ww = new Window2();
ww.someMethod();
}
else
{
ww.someMethod();
}
}
});
Here is a full working example:
Window2.java
public class Window2 extends JFrame {
private static final long serialVersionUID = 7843480295403205677L;
}
MainWindow.java
public class MainWindow extends JFrame {
private static final long serialVersionUID = -9170930657273608379L;
public static void main(String[] args) {
MainWindow mw = new MainWindow();
mw.go();
}
private void go() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
private void createAndShowGUI() {
JButton button = new JButton("Click me");
button.addActionListener(new ActionListener() {
private Window2 ww = null;
#Override
public void actionPerformed(ActionEvent e) {
if (ww==null) {
ww = new Window2(); //creating the new window here
ww.setDefaultCloseOperation(HIDE_ON_CLOSE);
ww.setTitle("Window2 created on " + new Date());
ww.setSize(500, 200);
}
pack();
ww.setVisible(true);
}
});
setLayout(new BorderLayout());
add(button);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
setVisible(true);
}
}
What you can try is make two windows and put the actionPeformed method in the main class so that when the button is pressed it displays the second window

Passing values between classes

I have a JPanel form which contains a JList and some JButton.
The JPanel looks like this
When I click the Add List button, a separate JFrame form is displayed.
The JFrame form will look like this
When the add button on the JFrame is clicked, I need to add the value of the JTextfield (named List Name) to the JList on the previous JPanel. I wonder how to pass the value from the JFrame to the JPanel? Any suggestion would be appreciated.
Here is a code of the JPanel form (using Designer GUI)
package multimediaproject;
public class musicPlayerPanel extends javax.swing.JPanel {
public musicPlayerPanel() {
initComponents();
}
private void initComponents() {
//...here is the generated code by using designer GUI
}
// Variables declaration - do not modify
//..generated code
// End of variables declaration
}
Here is the code of JFrame form (using Designer GUI)
package multimediaproject;
public class addListFrame extends javax.swing.JFrame {
public addListFrame() {
initComponents();
this.setLocation(515, 0);
setVisible(true);
}
private void initComponents() {
//..here is the generated code by using Designer GUI
}
private void addBtnActionPerformed(java.awt.event.ActionEvent evt){
//some validation
if(...)
{
//validation
}
else
{
//IF VALUE IS CORRECT, ADD the List Name JTextfield value to the JList on the previous JPanel
errorMessage.setText("");
}
}
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new addListFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
//....generated code
// End of variables declaration
}
UPDATE with your code.
You can take advantage of PropertyChangeListener and PropertyChangeSupport (This classes implements Observer Pattern).
I give you an example you for guidance:
public class MusicPlayerPanel extends JPanel {
private JList list;
private JButton addButton;
private PropertyChangeListener listener = new MyPropertyChangeListener();
//..in some place
addButton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
JFrame form = new FrameForm();
form.addPropertyChangeListener(FrameForm.BUTTON_CLICKED,listener);
form.setVisible(true);
}
});
//in another place
private class MyPropertyChangeListener implements PropertyChangeListener{
#Override
public void propertyChange(PropertyChangeEvent evt){
if(evt == null)
return;
if(evt.getPropertyName().equals(FrameForm.BUTTON_CLICKED)){
String value = (String) evt.getNewValue();
((DefaultListModel)list.getModel()).addElement(value);
}
}
}
}
And the frame form like this:
public class AddListFrame extends JFrame{
private JTextField textfield;
private JButton submitButton;
public static final String BUTTON_CLICKED ="buttonClicked";
// in some place
submitButton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent evt){
firePropertyChange(BUTTON_CLICKED,null,textfield.getText());
}
});
}
Note: In java by convention, classes start with uppercase and follow a camel style. This is very important for readability.
OK.This is easy if i understand your problem.
Step 1:Create setter and getter for your JList object reference.
Step 2:On button click , when you open new JFrame pass your panel reference in constructor of class which inherits JFrame.
Step 3:By doing this you are able to call getter method on panel reference.
Step 4:Now you have reference of JList,do what you want.
I hope this is best solution of your problem
"when I click the add button, a separate jFrame form is displayed. The jFrame contain a jTextfield and a submit button."
Did you seriously create an entirely new JFrame for a JTextField and a JButton?!
Have you not heard of JOptionPane? That's exactly what you are trying to replicate. The only code you need is this:
String s = JOptionPane.showInputDialog(component, message);
model.addElement(s);
The first line will cover all your code for your custom JFrame.
Take a look at this example
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class JOPDemo extends JPanel {
JList list;
JButton button = new JButton("Add Name");
String name;
DefaultListModel model;
public JOPDemo() {
model = new DefaultListModel();
list = new JList(model);
setLayout(new BorderLayout());
add(list, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
name = JOptionPane.showInputDialog(this, "Enter a name");
model.addElement(name);
}
});
}
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
private static void createAndShowGui() {
JFrame frame = new JFrame();
frame.add(new JOPDemo());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Edit
What you can do is have an inner class which is your JFrame. Personally, I'd go with a JDialog though.
From comment: Have your frame as an inner class of your main class. That way it can access all the variables from your main class. You can have a String listName also in the GUI class. From the other frame when you hit add, it sets the listName in GUI class, then adds to the list.
public class GUI {
String listName;
JList list;
InnerFrame inner = new InnerFrame();
private class InnerFrame extends JFrame {
JButton addButton;
}
}

use actionlistener to call void function in same class

I have problem to use action listener to call function void in same class.
example..
code:
public class Product extends JPanel {
JButton add;
JPanel pAdd;
JLabel test;
JFrame frame;
public Product() {
add = new JButton("Add Product");
add.addActionListener(new ButtonListener());
add(add);
}
public void panelAdd(){
pAdd = new JPanel();
pAdd.add(new JLabel("try"));
add(pAdd);
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
panelAdd();
}
}
}
How to make call the panelAdd void method?
When you add components to visible JFrame/JPanel/other components, you neet to call revalidate() andrepaint() methods after adding. Change your panelAdd() like next:
public void panelAdd(){
pAdd = new JPanel();
pAdd.add(new JLabel("try"));
add(pAdd);
revalidate();
repaint();
}
If you put
System.out.println("hi");
to
public void panelAdd(){
System.out.println("hi");
pAdd = new JPanel();
pAdd.add(new JLabel("try"));
add(pAdd);
}
you will see hi printed to your console , your code are working, but you have problem in Layout .

switch between jframe and jinternalframe

Hi stackoverflow developers I have to design a form in swing having two menus on it . onclicking a menu i want to add a jinternal frame on it . and then after clicking a button on jinternalframe the jinternalframe should be removed and new control should be added on jframe form .
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.event.*;
class MainMenu extends JFrame implements ActionListener
{
JMenuBar mb;
Menu field,test;
MainMenu()
{
Container cp=this.getContentPane();
mb= new JMenuBar();
field1= new JMenu("field1");
test=new JMenu("test");
mb.add(field1);
mb.add(test);
setJMenuBar(mb);
field1.addActionListener(this);
test.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("field1");
{
jinternalframe1 frm= new jinternalframe();
cp.add(frm);
frm.setBounds(0,0,600,600);
}
}
public static void main(String args[])
{
MainMenu frm = new MainMenu();
frm.setSize(1000,1000);
frm.setVisible(true);
}
}
public class jinternalframe1 extends JInternalFrame implements ActionListener
{
JButton jb1,jb2;
jinternalframe1()
{
jb1= new JButton("1");
jb2=new JButton("2");
add(jb1);
add(jb2);
jb1.addActionListener(this);
jb2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("1"))
{
JButton nn= new JButton("back");
MainMenu frm= new MainMenu();
frm.cp.add(nn);
//this is creating new Form but i want to add the new button control instead of add //jinternal frame
}
}
}
Pass the instance of main form to your JInternalFrame
jinternalframe1 frm= new jinternalframe(this);
Declare an object of MainMenu in the jinternalframe1 class to point to the main class object.
MainMenu myParent;
Modify your jinternalframe1 constructor to accept the MainMenu instance
jinternalframe1(MainMenu parent){
myParent = parent;
//rest of your code
}
And then in your actionPerformed add the new button to myParent instance.
if(ae.getActionCommand().equals("1")){
myParent.add(new JButton("back"));
}
But let me tell you, this is not a good practice at all ! And as said by trashgod your code is not sscce
Hope that helps you.

Categories