MDI Application in java - java

I am making a MDI application in java using netbeans.
the issue is that i have two buttons: Add employee and search employee. When i click Add employee, the internal frame for add employee opens up in the desktop pane, and when i click search employee it gets behind the earlier frame and is not visible until i exit the first frame. I want that if desktop pane is not empty then earlier internal frame should be disposed on click of the other button. Plese help me out
This is the code: Here JP is variable name for desktop pane.
private void BAddEmpActionPerformed(java.awt.event.ActionEvent evt) {
o=new EntryEmp();
JP.add(o);
o.setVisible(true);
}
private void BSearchEmpActionPerformed(java.awt.event.ActionEvent evt) {
Employee_search ob1=new Employee_search();
JP.add(ob1);
ob1.setVisible(true);
}

I think you should be able to set the first panes visibility to false:
private void BSearchEmpActionPerformed(java.awt.event.ActionEvent evt) {
Employee_search ob1=new Employee_search();
JP.add(ob1);
ob1.setVisible(true);
if (o != null && o.getVisible == true){
o.setVisible(false);
//and possibly kill it:
o = null;
}

After you've added the new JInternalFrame and made it visible call JInternalFrame#toFront

Related

jInternalFrame not brought to the front

I have a JDesktopPane which contains a number of JInternalFrames. The first time I press one button to visible jinternalframe1 and second button to visible jinternalframe2, it appear above the main window without problems. However, if I press one of the buttons to Reopen jinternalframe1 or jinternalframe2, they are not brought in front of the main window... EDIT: actually, i can't do anything with jinternalframe on a button click...i can only click once on the button and then no operation can be perform on the jinternalframe through the button..why it doesn't work!!
this is the coding of button1...
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
jinternalframe1 frame1 = new jinternalframe1();
try {
if(Allow.flag == false) {
desktopPane.add(frame1);
frame1.setVisible(true);
Allow.flag = true;
} else if(Allow.flag == true) {
frame1.setSelected(true);
}
} catch(PropertyVetoException e) {
System.out.println(e);
}
}
Allow.java
public class Allow {
static boolean flag = false;
}
Every time you click on the button you create a new JInternalFrame object, but you only ever add the first internal frame that you create to the desktop pane.
Don't keep creating new internal frame objects. I would guess you should only create the internal frame if your "frame1" variable is null.
If you need more help then post a proper SSCCE that demonstrates the problem.

How to connect two JFrames in netbeans without using swing?

I am make a project on cars. How can I make distributor frame popup and cars frame not visible and close automatic? Kindly send any solution in simple and effective way.
I have done coding this way:-
{
Cars frm1=new Cars();
Distributor frm2=new Distributor();
frm2.setVisible(true);
frm1.setVisible(false);
frm1.setDefaultCloseOperation(frm1.DISPOSE_ON_CLOSE);
}
".Please help me to how I can make distributor frame popup and cars frame is not visible and close automatic."
Ok so in Netbeans GUI Builder, you may want to do the following (this is assuming you have created two separate JFrame form files
In the frame that is the launching program (we'll call it MyFrame1) add a button to it (we'll call it jButton1)
Add a listener to the button, then the following code should be auto-generated
public void jButton1ActionPerforemd(javax.swing.ActionEvent evt) {
}
In that actionPerformed, just instantiate the second frame (we'll call it MyFrame2) and setVisible(false) to MyFrame1. MyFrame2 should already be visible upon instantiation, so you wouldn't have to setVisisble(true) on it
public void jButton1ActionPerforemd(javax.swing.ActionEvent evt) {
MyFrame2 frame2 = new MyFrame2();
MyFrame1.this.setVisible(false);
// You can also use MyFrame1.this.dispose(); dependind if you ever need to use that frame again
}
I think this should work
you need to setVisible Jframe2 as true...so it can apear on output sceen
public void jButton1ActionPerforemd(javax.swing.ActionEvent evt)
{
myFrame2 frame2=new myframe2();
myframe1.this.setVisible(false);
frame2.setVisible(true);
}
create action event for the button such that when when you click will take
you
to the next page for my case next page is nextjFrame
private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {
setVisible(false);
nextjFrame ob=new nextjFrame();
ob.setVisible(true);
}
private void BTNConvertActionPerformed(java.awt.event.ActionEvent evt) {
/*
This is the action performed event for my Button "BTNConvert"
*/
java.awt.EventQueue.invokeLater
(new Runnable()
{
public void run()
{
new JFrame2().setVisible(true);
}
});
/*
This will set the second Frame Visible.
*/
JFrame1.this.setVisible(false);
/*
This should set the first frame invisible or whatever. Any other code should be
written before the curly brace below.
*/
}
//You're Welcome.

How to check if a jframe is opened?

My code below create a new array and sends it to chat(jFrame).
String info1[]=new String[3];
// username , userid , userid2 are variables
info1[0]=username4;
info1[1]=""+userid;
info1[2]=""+userid2;
chat.main(info1);
But i need to modify this code to work such a way that it , if the chat jframe was opened,
then dont open a new jFrame .But instead open a new tab in chat jframe . The code for chat frame is :
private void formWindowActivated(java.awt.event.WindowEvent evt) {
JScrollPane panel2 = new JScrollPane();
JTextArea ta=new JTextArea("");
ta.setColumns(30);
ta.setRows(19);
panel2.setViewportView(ta);
jTabbedPane1.add("Hello", panel2);
}
I wonder if you shouldn't be using JDialogs instead of JFrames, if the window is dependent on another window.
A solution is to use a class field to hold a reference to the window (JFrame or JDialog) and checking if it is null or visible, and if so, then lazily create/open the window,
public void newChat(User user) {
if (chatWindow == null) {
// create chatWindow in a lazy fashion
chatWindow = new JDialog(myMainFrame, "Chat", /* modality type */);
// ... set up the chat window dialog
}
chatWindow.setVisible(true);
addTabWithUser(user);
}
but that's about all I can say based on the information provided. If you need more specific help, then you will need to provide more information.
If using JFrames it can be simply done like this:
if (Frame1.component != null) {
Frame1 is opened
} else if (Frame2.component == null) {
Frame2 is closed
}
Component ex.JTextField, JComboBox etc.

Java Swing - Keeping all JFrames together

I've got an application in which sometimes I use "modal", smaller windows. To simulate such modal behaviour what I do is defining them as extensions of JFrames, disabling the main application frame and setting the default close operation to do nothing. The problem of this is that JFrame can't have a parent so, when the user clicks the button said to close the window, it in fact closes and the system goes back to the last used application, which is not always the desired effect (e.g. the user opens the modal window, then he/she Alt+Tabs into another application, then switches back into the modal window and closes it, resulting this in being presented this last application, instead the main frame of mine). Is there any way to do this (binding JFrame to a parent or something like that...?). Note that JOptionPane is not an option because these smaller windows need to have like twenty different and custom Components inside each.
Why don't you use a JDialog? :-)
You could add a JFrame parameter to the constructor and internally save it as parent, and save a reference of the child in a list in the parent. Then you can override the dispose method and close all children/parent before closing the window.
For example:
public class ParentWindow extends JFrame {
//Here you add children when you create them
List<JFrame> children = new ArrayList<JFrame>();
#Override
public void dispose() {
for(JFrame f : children) {
f.dispose();
}
super.dispose();
}
public void randomMethod() {
JFrame newWindow = new JFrame(this);
children.add(newWindow);
}
}
public class ChildWindow extends JFrame {
//Here you save the parent window
JFrame parent;
public ChildWindow(JFrame parent) {
this.parent = parent;
}
#Override
public void dispose() {
parent.dispose();
super.dispose();
}
}

Java Project - how to freeze Frame

Good day!
I am doing a game in Java. My menu button includes New Game, HighScore, About and Quit.
But before the user can proceed to the main game, he needs to type his name first. I used this code as follows:
private void btnNewGameMouseClicked(java.awt.event.MouseEvent evt) {
Player p1 = new Player();
this.setVisible(false); // I must replace this code
p1.setVisible(true);
}
My problem is, I don't want to make the main menu hidden. I want it to freeze and cannot be accessed when the player name is being asked.
My Main menu frame is bigger than the player frame.. Of course, I can just delete the code this.setVisible(false) but the problem is I can still access the main menu when clicked... I want the main menu to freeze and cannot be accessed when the player frame pops up.. (See image below) Please help me. Thank you.
What you want to do is make your player frame a modal dialog. You would want to make it a subclass of JDialog rather than JFrame or whatever you're using and set it to be modal either using its setModal method or with one of JDialog's constructors. For example:
public Player(JFrame owner) {
super(owner, true); // makes the dialog modal
// ...
}
Then you could create the dialog from the main frame like:
Player p1 = new Player(this);
When you call p1.setVisible(true), the main frame will be blocked and unclickable.
private void btnNewGameMouseClicked(java.awt.event.MouseEvent evt)
{
Player p1 = new Player();
p1.setVisible(true);
setEnabled(false);
}
/*
setEnabled(boolean b) (java.​awt.​Component)
Enables or disables this component, depending on the value of the parameter b. An enabled component can respond to user input and generate events. Components are enabled initially bydefault.enter code here
*/

Categories