Hy. I have problem when my first jframe button was set disable the frame then log on to another jframe.
Jframe1 , my code:
Private void.jbutton1actionperformed(java.awt.event.ActionEvent evt){
this.setEnabled(false);
new Jframe2().show();
}
I'm not make Jframe1 dispose, only disabled, and make Jframe2 showing. In Jframe2 I set button here:
Private void jbutton1actionperformed(java.awt.event.ActionEvent evt){
this.dispose()
new Jframe1().setEnabled(true);
}
Then my problem is, Jframe1 was still disabled. I dont know how to make enable from another jframe. Please help me!
When you use new that means you are creating a new jFrame1, not updating the old one.
Related
I've a JFrame and a JPanel is added in. When I click o a button in the panel, a JDialog (named choiceDialog) appears. When I click on a particular button on the dialog I just want it to close.
I would want the dialog closed and the frame usable. Is it possible?
I tried to hide the dialog with setVisible(false) but it hid both the dialog and frame. Then I tried to do choiceDialog.dispose() but I lost both the elements again. At that point I found a way to set the Frame again visible but not usable.
Can anyone help me please? I don't really know what to do.
Here's the relevant code:
if (dimField.isEnabled()){
String dimFieldText = dimField.getText();
if (dimFieldText.equals("") || !isNumeric(dimFieldText)){ //if there's an error when filling the options in the JDialog
errorLabel = new JLabel(noDim, SwingConstants.CENTER);
/*other stuff
...
*/
}else{ //if it's all ok: I want the JDialog close but the JFrame to be usable
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this); //to catch the JFrame istance
choiceDialog.dispose();
topFrame.setVisible(true); //to make the JFrame visible again
//choiceDialog.setVisible(false);
}
You are setting topFrame to be the window ancestor of the button, which is the dialog itself. You need to get the window ancestor of the dialog instead. That assumes that when you created the dialog you specified the main frame as its parent rather than using the no-argument JDialog constructor.
I have designed two JFrames in NetBeans.
When I click the "rules" button (i.e placed on JFrame1) then it opens a second JFrame (but JFrame2 opens over JFrame1's window, that's what I dont want).
In the second JFrame there is a "close" button. But when I click this button, I want JFrame1 to be opened and it is working too, but JFrame2 is actually not closed and JFrame1 is appearing over JFrame2.
In short the main form is JFrame1. When I click the "rules" button from JFrame1 it opens JFrame2 over JFrame1, and in JFrame2 there is a "close" button when it gets clicked the main form (i.e JFrame1) is lauched but it is launched over JFrame2.
The scenerio is JFframe1 -> JFrame2 -> JFrame1
Now my question is after clicking the "rules" button, JFrame1 should be closed and JFrame2 displayed on the screen and vice versa.
Assuming your button has an actionListener, after clicking the "rules button" put in:
JFrame1.dispose(); //Remove JFrame 1
JFrame2.setVisible(true) //Show other frame
And then reverese them for the opposite reaction
Somethig like this should be on the constructor or method which create JFrame2:
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//call another method in the same class which will close this Jframe
CloseFrame();
}
});
It's method which should close JFrame2
public void CloseFrame(){
super.dispose();
}
Well, if you already have a actionListener, you should add this:
JFrame1.dispose(); // This will close the frame
The JFrame1 is the name of your frame.
And if you want to open another frame that you have, add this:
JFrame2.setVisible(true); // This will put the other frame visible
I'm not an expert by any means, however, I ran into this problem as well. If you set your second JFrame to hidden, when you hit "Cancel", it will close the second JFrame.
//this is the code for the "cancel" button action listener
public void actionPerformed(ActionEvent e) {
setVisible(false);//hides the second JFrame and returns to the primary
this worked for me (Frame1 Called RegScreen and Frame2 Called MainScreen):
RegScreen.this.setVisible(false);
new MainScreen().setVisible(true);
Hope that this helps :) Regscreen was the original frame open at startup.
If this doesn't work, try this
JFrame1.dispose(); //Remove JFrame 1
JFrame2.setVisible(true) //Show other frame
JFrame2.setVisible(true);
this.dispose();
Have a MainClass with a main() method.
Have the MainClass that has the main() method encapsulate your JFrame1 and JFrame2 reference variables. Don't have JFrame1 or JFrame2 contain main() unless you have a specific reason.
After something is clicked in one of the JFrame objects, instantiate/make visible the other JFrame object and dispose of itself via your MainProgram.JFrame object methods.
Example:
//btn event inside 1st JFrame/window
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
MainProgram.openResultsForm(); //MainProgram opens 2nd window
MainProgram.queryEntryForm.dispose(); //MainProgam closes this,
//the 1st window
}
I created a "application window" in a Eclipse and i used the following code to dispose 'frame' and open another jframe 'TableData' which worked as i expected.
frame.dispose(); //private JFrame frame;
TableData td = new TableData();
td.setVisible(true);
Now, my problem is, I want to create 'go back' button in 'TableData' class which dispose 'TableData' and open 'frame' again.
i tried many ways but non of those work as i thought.
How can I do it????
Just hide the frame as:
frame.setVisible(false);
TableData td = new TableData();
td.setVisible(true);
and then in 'go back'
frame.setVisible(true);
I have a JFrame class and it was made in the design section on Netbeans. I am trying to make a log in button that takes closes the current frame and opens another, is there anyway I can do that?
I have tried:
JFrame frame = new JFrame();
But I want it to be editable in the design section!
Double Click the Login Button in the NETBEANS or add the Event Listener on Click Event (ActionListener)
btnLogin.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
this.setVisible(false);
new FrmMain().setVisible(true); // Main Form to show after the Login Form..
}
});
new SecondForm().setVisible(true);
You can either use setVisible(false) or dispose() method to disappear current form.
This link works with me: video
The answer posted before didn't work for me until the second click
So what I did is Directly call:
new NewForm().setVisible(true);
this.dispose();//to close the current jframe
JFrame.setVisible(true);
You can either use setVisible(false) or dispose() method to disappear current form.
1> I have a JButton in Jframe.
2> The click of JButton opens new instance of another JFrame.
The problem is when a Key is pressed very fast on the above Jbutton .Two instances of the same JFrame opens up.
I have to open these frames. I knows there are other options also not using the Jframes as I read.
I managed to solve this problem for Doulbl click of Mouce by setMultiClickThreshHold('time in miliseconds'). But it worked only for mouse.
Tried some other stuffs which I got in google, But none worked.
Is there any other way to solve this issue?
For full control of how often/quickly again an Action is triggered, implement it to disable itself in its actionPerformed. Something like:
singlePerform = new AbstractAction("DoSomthing") {
#Override
public void actionPerformed(ActionEvent e) {
setEnabled(false);
doSomething();
}
};
JButton button = new JButton(singlePerform);
When it's safe for doSomething to be triggered again, simply re-enable the Action:
singlePerform.setEnabled(true);