Why two internal frame is creating in my java code? - java

I am trying to learn how to use java swing components. I have been trying to build up something like a pop up window.Like facebook when we select a friend a window pops up.I have a list of friends. I wish to create a pop up window when the user selects one of his friend from his friend list.But the problem is , each time I run this code, two internal frames are popping up.I fail to sort out the problem.Here is the code snippet.
Thanks in advance.
private void list2ValueChanged(javax.swing.event.ListSelectionEvent evt) {
JInternalFrame f = new JInternalFrame((String)list2.getSelectedValue(),
false,true,false,true);
f.setSize(150,150);
f.setVisible(true);
desk.add(f,BorderLayout.SOUTH);
}
Here desk is the variable name for JDesktopPane.

A ListSelectionListener will generate multiple events every time the selection changes.
You need to check the ListSelectionEvent.getValueIsAdjusting() to make sure the selection is finished adjusting
if (!event.getValueIsAdjusting())
// create your internal frame.

Related

Java GUI Design view not showing completely but code works

I am new to Java GUI I deigned and window and menu item using java design tool.But when I want to create window for menu item say New contact I did not find an option to do that in event handler so I did it manually by coding it.But when I go to design part and click on New Contact it does not show the window I created via code.
Here is the screen shot of deign view -when I click on New Contact nothing happens.
Now in the source code when I run it I get the window I coded
Is there any possible way I can make it work in design part? I did not find any option to do it in Add Event Handler
You do not want a JFrame to show another JFrame -- that's a bad GUI design since it means that your application is actually two applications. Better to show a dialog window such as a JDialog. Please see The Use of Multiple JFrames, Good/Bad Practice?
If you want to design a 2nd window, create a new Java program in NetBeans, one that creates this second window (again, better for it to be a JDialog, not a JPanel)
Give it a constructor that allows passing in the parent window, and then pass that to the JDialog super constructor
And then in your ActionListener code above, create a new object of this new program, passing in the current JFrame.
In the future, please post code as code-formatted text, not as an image, since this way we can copy, paste, compile and run it if we want, allowing us to better understand your code and your problem.

Creating a frame class in order to make many InternalFrames

BACKGROUND
I'm creating a hostel booking software as a project to learn java. I have my main frame which holds buttons that open each room by setting the containing frame to visible. I have an add room button that asks me how many beds the room as and stores this as an int.
QUESTION
Would it be possible to have a room class that would allow me to create new rooms by pressing the button? That means creating an InternalFrame, JPanel and JButtons, adding the buttons to a grid based off the bed number int.
I feel like the idea is there but the implementation is proving tricky since im new to java. Any ideas are appreciated, thanks!
Read the section from the Swing tutorial on How to Use Internal Frames.
The demo in the tutorial shows how to dynamically create internal frames when clicking in a menu item. The concept is the same for buttons. You just add the ActionListener to the button.
Of course you will need to change the code to create your own custom panel. You will also probably want to use a JOptionPane to prompt the user for the number of rooms. Again the tutorial has a section on How to Use Dialogs for an example of using a JOptionPane.

Dispose method to close a JFrame (but not the entire program) from a separate class in NetBeans is not working. Why?

I have three classes I am working with here. The first is an admin page where the user can select to either add, update or delete an employee on the system via a drop down box. When the user selects one of the three options from the combo box, a JFrame (employees) comes up with the necessary fields to perform their task and the admin frame will still be displayed behind it. On this frame there is a "Cancel" button. When they click the cancel button only this frame must close but keep the admin frame open still. The button is generated from a separate class (empClass) to be displayed on the employee frame. My problem now is I am struggling to get the button to dispose of the employee frame, but out of the several ways of trying this it cannot work. One way produced an error each time I ran the application, another method caused the application to crash/freeze whenever I try select an option to perform on the employee frame and the code I have currently implemented performs no action at all. I think the problem is a an issue with communicating with the forms but I am not entirely sure. Please help as I have been struggling with this for hours and the internet is providing absolutely nothing useful. Most resources refer to the dispose() method which I have tried in various ways but all the ways I have tried do not work, crash the application or cause errors to occur. Even the other questions similar to this on here have not helped me out at all.
I have tried calling the button from the employee frame to try and link the function to the "Cancel" button. Here is the code I have implemented in the empClass:
public void disposeof()
{
employees empp = new employees();
empp.dispose();
}
private void cancelActionPerformed(java.awt.event.ActionEvent evt)
{
disposeof();
}
Here is the employee coding:
public class employees extends javax.swing.JFrame {
empClass ec = new empClass();
adminPage ap = new adminPage();
public employees() {
initComponents();
getContentPane().add(ec.getpanel());
this.add(ec.getpanel());
this.add(ec.lbltitle);
this.add(ec.cancel);
this.add(ec.bfunction);
this.add(ec.empList);
}
As you didn't provide the code of your JFrame so I guess problem is in the code of your JFrame. You might be setting setDefaultCloseOperation(JFrame .EXIT_ON_CLOSE) for your employee class as it is static property so it will close all JFrames. You should set it setDefaultCloseOperation(employees.DISPOSE_ON_CLOSE) OR setDefaultCloseOperation(employees.HIDE_ON_CLOSE). And after that while triggering your event you can call empp.dispose(); OR setVisible(flase).
First of all only use one JFrame and use JDialogs for the underlying other windows you may want to see appearing. On JDialog, use setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE).
Another way you can do this is to hide the frame using setVisible(false).

Creating Internal Wizards using Java Swing

I want to create an internal wizard in my Java GUI application such that the clicking of a menu item results in popping up of a wizard that guides the user through a series of steps. I have done lot of research and couldn't find anything with decent enough documentation. Can someone help me out please? Has someone worked on creating a wizard that pops up INSIDE a GUI application?
Thanks in advance!
You can use cjwizard. It can be embedded inside a JDialog as it is based in JPanel.
And you can see how to use it in https://github.com/cjwizard/cjwizard/blob/master/docs/quickstart.md for example:
// create the WizardContainer:
final PageFactory pageFactory = /* create a PageFactory, see the link */;
final WizardContainer wizard = new WizardContainer(pageFactory)
// stick the WizardContainer into a dialog:
final JDialog dialog = new JDialog();
dialog.getContentPane().add(wizard);
dialog.pack();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
Disclaimer: I'm part of the development team.
If there are no third-party libraries to satisfy the requirements you have then you can just write your own approach. A set of panels, each one having Previous, Next buttons and some of them Finish. The current panel just needs to know which are the previous and the next panels.

How to use setVisible in JFrames?

In my program I have two JFrame instances. When I click next button I want to show next frame and hide current frame. So I use this.setVisible(false) and new Next().setVisible(true). But in Next window if I click back button I want to set previous frame to be visible again and next frame must be ended (which means it must be exited).
Is there any special method(s) to do this? How can I do it?
Consider using CardLayout instead of hunting for how many JFrames there are. Then..
only one JFrame would be needed
any of Next/Back Actions will be only switching between cards
There are lots of examples in this forum - e.g. as shown here.
That is an odd & quirky GUI. I suggest instead to run a JFrame for the main GUI, and when the user wants to search, pop a JOptionPane (or modal JDialog) to accept the details to search for. This will not have the effect described above, but will follow the 'path of least surprise' for the end user.
If you want to destroy a JFrame releasing all associated resources you shold call dispose() method on it.
You may place your JFrames on a list data structure and keep a reference to current position according to the window you are displaying. In that way it will be easy to move to next and previous. But note that each frame added to the list will use memory and will have its state as you placed it in to the list.
If you are trying to create a wizard like UI, you should look up Sun(oracle)tutorial here.
create the instance of your main window in next() window.. and use same method which you chosed befoe to hide your main window, for example if your main window is named as gui then what we have to do is.
gui obj = new gui();
and if you click on back button now than do these also
this.setVisibility(false);
obj.setVisibility(true);
that's all you need.
good luck.

Categories

Resources