if i have add a number and i print it in a new page(JFrame). now, i want to get back to
previous page to add another number.
how to write the code? because i want to keep thw new page open and back to previous page. the
new page is to display what the number that have been added by the user. totally no idea with
it.
i've try this
first frame called 'frame1'
second frame called 'frame2'
public static boolean isClicked = true;
if(btnOK.equals(isClicked))
{
frame2.setVisible(true);
frame1.setVisible(false);
}
1) The use of multiple JFrames in Java is a bad practice see here:
The Use of Multiple JFrames: Good or Bad Practice?
2) I would suggest using a LayoutManager like CardLayout which allows you to flip between containers like JPanels on a single JFrame:
How to Use CardLayout
3) You might also want to substitute your initial JFrame for a JDialog/JOptionPane, but that would depend on the usage of the initial JFrame.
Related
Hey I'm working on a java project, I'll try yo generalize my problem so...
I have a jpanelX that contains jbuttons1 to 5. All these jbuttons connect to the same actionlistener and the same action performed method. I also save the source of the button clicked into a global string variable.
I have another JpanelY. JpanelY contains arrays of strings.
I want to connect the two with this behavior:
user clicks button1 on JpanelX
JpanelY is shown instead of JpanelX. aka the user is taken to JpanelY
the string array in JpanelY will contain different values based on the clicked Jbutton. So if a user clicks Jbutton1, the array will be assigned values {"Value1 ","value 1b","value1c"}
i tried a lot of things and got different errors. please help me, thank you so much
It's a bad idea to save anything in a global string variable. Much better is to call a method in the panel you wish to display that assigns the correct values to your string array.
A user can be 'taken to' a panel in lots of different ways. You could hide the current panel and show the new one (setVisible(true/false)), you could use a layout manager that allows different panels to be displayed in the same space, you could change the contents of the containing panel. You'll need to give more details of what you want.
Depending on what you need, you might end up with code like:
button1.addActionListener(ae-> showValues("val1", "val2");
button2.addActionListener(ae-> showValues("val3", "val4", "val5");
private void showValues(String... values) {
setVisible(false);
arrayPanel.setArray(values);
arrayPanel.setVisible(true);
}
I have a problem about modify button background. I am using netbeans gui builder for build form. I am trying change button background when the second frame is open and turn it back when second frame close.
public void update(boolean x){
if(x==true){
circleButton.setOpaque(true);
circleButton.setBackground(new java.awt.Color(0, 0, 0));
System.out.println("testoutput");
}
}
this is my update method from first class.
I added window listener to second frame.
private void formWindowOpened(java.awt.event.WindowEvent evt) {
isitopen = true;
//this is first class which includes button
homework hwork = new homework();
hwork.update(isitopen);
System.out.println("testoutput2");
}
I got 2 testoutput but color of the button didn't change.
What can i do to fix this issue ?
You're creating a new homework object in your formWindowOpened(...) method, one completely unrelated to the homework object that is displayed, and changing the state of the new object will have no effect on the displayed one.
A simple and WRONG solution is to use static fields or methods.
Instead one simple solution is to give the calss with your formWindowOpened(...) method a valid reference to the displayed homework object, something that can be done with a constructor parameter or a setHomework(...) method.
A much better and even simpler solution:
Make the 2nd window a modal JDialog, not a JFrame
This way homework will know when the window is open and can set its own button colors. When the 2nd window opens, program flow in the calling class is put on hold, and only resumes when the 2nd window closes -- just like using a JOptionPane.
For more on this, please see The Use of Multiple JFrames, Good/Bad Practice?
As an aside, you will want to learn and use Java naming conventions. Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others.
I am making a vehicle_management system in java swings. I have a MDI(muti-document interface) Window in which there are more then one panels can be open.But when one panel is open after another the previous one is hides under new one, so all the JPanel are stacks. what i want is that if a panel is open and user trying to open another panel the previous opened panel get closed.how to do that.
Your application should maintain a List<JInternalFame> of open frames. In your open Action, see if the target frame is already open. If so, invoke setSelected(true) to bring the frame to the front; if not, open the frame as usual. A related example is cited here.
Addendunm:
I don't know how to make a list of open frame and check target frame is open or not.
This example illustrates how to compose and iterate a List<JInternalFrame>. Use the list's indexOf() method to search the list for an existing instance. A return value of -1 means that the list does not contain the element
First off, unless yours is an extremely unusual application, you don't "open" a JPanel. Since we're talking about an MDI application I'm guessing that what you really mean is that you're opening a new JInternalFrame.
Second, you said that you want to "close" the other panels (again, I'm assuming here that you really meant internal frames) but as someone else already pointed out, if you close them then it's not really an MDI application. I'm guessing that what you really meant or want is to MINIMIZE (not close) the other internal frames. If that's the case, then you should create a collection (e.g., a List) of JInternalFrame instances in the class where you create and add new internal frames. When you're about to add a new frame then just loop through the items in that collection and call setIcon(true) for each internal frame instance. Once that loop has completed, add the new internal frame to the collection and then also add to the desktop pane. At that point it will be the only one that's not minimized / iconified and any frame after that will likewise be (at least initially) the only one that's not minimized / iconified.
I'm looking to build out a Java GUI with a table area and an area that will display the data of a selected row of the table. I've never tried a multi-frame set up before so before I venture to do this I wanted to check with others. Is it difficult to have two frames and have them passing data back and forth? The idea would be that I could move the details frame anywhere I like on the screen or to a second monitor and allow the table to go full-screen if the user wants. Any input or examples are appreciated.
don't to create two of more JFrames use JDialog instead,
reuse this JDialog for another action(s)
create one JFrame and one JDialog for displaying details
have to determine if and which of JTables row(s) is selected
better would be to set ListSelectionMode to the SingleSelection
maybe would be better to invoke (show that already exist) JDialog from JPopupMenu Action
You should have no problem in doing what you are after. You can have public methods in each frame which expose properties and/or structures and you then pass the instance of one JFrame to the other. This should allow you to pass data back and forth.
That being said however, I think that this scenario is valid only when you have one, two, or at most three JFrames. Having a lot of frames calling each other could result a maintenance nightmare.
there are several possibilities to do so:
you can add one of the jframes as a listener to anothe, or both to each other. For this, you have to implement a listener mechanism, like in java.awt. You can pass the information contained in the event objects - this would be the most clean alternative
you can pass the instance of the detailframe directly in the constructor of the main frame and call operations from main frame on detail frame. this is the simplest way, but you will need lot of code changes if you have some new features to add
I am trying the save the state of a previous frame and then carry it over to the other frame. More like saving the data of the textfields and areas so that when i press next button some text fields,and variables will be initialized in the next forms Labels and when you press back your previous data will still remain in your form inteface.Pls help
More like data binding but across different forms
You can use any varible for that, and pass it as parameter to every JFrame you create.
But it sounds more like you want to use a CardLayout for your JFrame and use different cards that can be shown for the user. See How to use CardLayout.
If you are trying to create a wizard like UI, you should look up Sun(oracle)tutorial here.
Use something like Java's XMLEncoder.