I have five buttons in my current GUI named Browse, Unzip, Convert, Copy and Zip. First event is browse and by using that button I upload a zipped file and then click on unzip to unzip that file followed by convert and copy buttons and finally Zip the file again by clicking on Zip button. I have kept them in a sequential order and click on them one by one but I want to put these actions as Browse, Next, Next, Next and Finish button. So please help me out in achieving this task.
My Java Swing (Netbeans) code looks like:
public class MyClass extends javax.swing.JFrame {
private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {
//Code here
}
private void unzipButtonActionPerformed(java.awt.event.ActionEvent evt) {
//Code here
}
private void convertButtonActionPerformed(java.awt.event.ActionEvent evt) {
//Code here
}
private void copyButtonActionPerformed(java.awt.event.ActionEvent evt) {
//Code here
}
private void zipButtonActionPerformed(java.awt.event.ActionEvent evt) {
//Code here
}
}
Thanking you!
This sounds like a classic wizard application with CardLayout.
You could create a base panel in the BorderLayout.SOUTH position of your JFrame that would have your action buttons and have a number of panels added to your main panel being managed by CardLayout.
See Creating Wizard Dialogs with Java Swing
I would suggest using CardLayout and adding multiple JPanels to it. Typically, I create my own JPanel subclasses to hold the controls then add them to a JFrame, in this case with a CardLayout set. Your "Next" button can change which JPanel is shown...in fact, with some ingenuity and multiple layout managers, you only need one "Next" button.
In that case you just need one JFrame and one JPanel, Every click or step will change the content of JPanel. But the the main class containing the JFrame or the one extends the JFrame will have reference to the file which you are working with.
P.S. This cannot be achieved efficiently by any pre made GUI builder. But, the programatic solution is pretty simple.
Something like this : http://java.sun.com/developer/technicalArticles/GUI/swing/wizard/
Related
Im using NetBeans to do a work for school. There i have an huge JPanel that contains a huge JFrame. That JFrame as 5 small JFrames, 1 is the menu with buttons, the other ones are boxes with text that will swap when i choose in the buttons.
When one box is showing the other ones are invisible im using the following code (dont know if it is the best):
public ConversorUI() {
initComponents();
PanelVazio.setVisible(true);
PanelTemp.setVisible(false);
PanelComp.setVisible(false);
PanelMoedas.setVisible(false);
this.pack();
}
My problem is, when i run my program i have a big space with nothing and only below it the components appear. I want them to appear in the top of my window. What can I do ?
ANSWER
After some time searching i just realized i could Set Layout from JPanel to Card Layout and create JPanels over each other activating them with the code:
private void DinheiroButtonActionPerformed(java.awt.event.ActionEvent evt) {
//Remove Panels
CAIXA.removeAll();
CAIXA.repaint();
CAIXA.revalidate();
//Add Panels
CAIXA.add(DinheiroBox);
CAIXA.repaint();
CAIXA.revalidate();
}
Looks like you are using Java Swing , right ?
Any way you do something wrong if you have JPanel that contains JFrames. To build correct UI you have to add JPanels inside JFrame.
Also, to reach correct component order and placing you need configure corresponded layout, here is description.
You can load one jframe at e time
Because every jframe you added have own place and visibility doesnt do anything to remove
Try to save every jframe and for changing
Delete old one and add new one
Why are you using multiple JFrames to do this? From what I can see it would be a better idea to use JPanels that can take care of the individual tasks, such as the menu and buttons etc.
I'm relatively new to using javax.swing myself, but from my knowledge you can only have 1 frame at a time per window (like the other person said).
From what i've been able to discern from your project, you possibly don't even need multiple panels. You just need one panel for the menu with buttons, and multiple Labels or JLabels that display text according to the button. You can use the setText method in writing your addActionListener, something like this:
buttonName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
labelName.setText("blah blah blah");
//and whatever else you may need to do
}
});
I have been working on swings framework in which there is a card layout panel in mainframe.
I have a total of 20 jpanels added to my card layout panel.Among these 20 panels,there are 3 panels which consist of a button.clicking on this button loads up another jframe. When I run this GUI using IDE, it works perfectly, but, when I run jar file, it doesn't shows up the frames when clicked on button in two of the three cases.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JFrame f11=new JFrame("DEADLOCK SIMULATION-1");
f11.add(new puzzle6());
f11.setSize(1100,620);
f11.setLocation(0,40);
f11.setVisible(true);
}
code for other two buttons is also similar except that i have added puzzle7 and puzzle8 panels in those jframes.
This is the code I am using for buttonlistener. This button loads up the new jframe only in one case and doesnt shows up frame in other two cases.Each java file which I have added in these three new jframes extends jpanel.The problem arises only when I run jar file, but works perfectly with IDE.
Have you registered your events? using a Listener Class?
My program looks like this!
I want to have the bottom part dynamically load a frame into the bottom frame depending on the item selected in the ComboBox. For example, if the first item is selected I want a panel from the PresentValue.java file displayed. The idea is that I have one java file for each selection that displays what I design in its respective java file.
These two java files should be put into the "bottom" box from my first screenshot, depending on the selection from the combobox.
I'm more used to Android programming and there I would simple call the replace method from fragments to swap out the fragment loaded... looking for the analogy here.
final JComboBox selectorBox = new JComboBox(selection);
selectorBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int selectionID = selectorBox.getSelectedIndex();
}
});
but cant find a way to do what I want to do. Please explain.
For each Java file that you have, the output of that Java file should be a JPanel. Not a JFrame.
Before you display anything, execute all of the Java files you have. Create all of the possible JPanels.
Create your JFrame in your GUI, then use the remove and add methods of JFrame to remove or add the desired JPanel.
Here's an example from one of my GUI's.
public void updatePartControl() {
Thread thread = new CountdownThread(model, this, displayPanel);
thread.start();
frame.remove(alarmPanel.getPanel());
frame.add(displayPanel.getPanel());
frame.validate();
frame.pack();
frame.setBounds(getBounds());
}
The setBounds method resets the bounds if the display JPanel is bigger or smaller than the alarm JPanel.
Your application should have one JFrame. You use multiple JPanels to create your GUI.
Changing the bottom component will depend on the layout manager that are using. CardLayout is purpose designed for swapping panels.
public void actionPerformed(ActionEvent arg0) {
int selectionID = selectorBox.getSelectedIndex();
if (selectionID == 0) {
cardLayout.show(basePanel, SELECTED_1);
}
// handle other selections
}
I am learning java and building one project to test basics.
I have one menu item FILE and then sub menu item like
1)Front
2)Admin
3)Booking
I have separate gui made in separate files but i want that they should be visible in one area , with click on submenus
I am using swing , JmenuBar . Also the other guis are using Jframe
I have separate gui made in separate files but i want that they should be visible in one area
Most applications should only ever have a single JFrame, which indeed is your requirement since you want the separate GUI to be visible in the same area.
Therefore your other GUI, should not extend JFrame but instead should extend JPanel. Then you can just use a CardLayout on your real GUI to swap the panels in/out depending on which panel is selected from your menu. All these basic are covered in the Swing tutorial. I guess you would start with the section on:
How to Use Card Layout
How to Use Menus
Other people have already talked about ActionListeners and stuff, so that's half of the problem. The other half is how to actually deal with the multiple windows. I would probably not use one JFrame per different GUI, given that the spirit of the JFrame suggests you should only have one instance of it per application. Instead, I would look at using either JDialog or JInternalFrame. I'm not sure what you mean by
...should be visible in one area...
but JInternalFrame will allow you to implement something like a multiple document interface, where all the sub-GUIs would be contained within the frame of the main UI. JDialog would be give you independent windows like JFrame does.
If with "they should be visible in one area" you mean modal, then you should change all your JFrames to JDialogs and leave only the JFrame that contains your main-menu.
To do this, you need an ActionListener for each of the menu items. Then have each listener pass the instance of the JFrame you want to a method that controls where you want to position the window and show it.
//Make menu items
JMenuItem font = new JMenuItem();
font.addActionListener(new ActionListener() {
showWindow(new FontFrame());
});
JMenuItem admin = new JMenuItem();
admin.addActionListener(new ActionListener() {
showWindow(new AdminFrame());
});
...
//define frame handling method
void showWindow(JFrame f) {
...
f.setVistible(true);
}
I have GUI created using netbeans GUI builder. I want to add there an object (let's try with button) after pressing a JButton
The very simple code which I wrote in ActionListener and works:
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel2.add(new JButton("X"));
panel2.validate();
}
});
Howewer in Gui created by netbeans this doesnt work:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jPanel1.add(new JButton("X"));
jPanel1.validate();
System.out.print("aa");
}
But it prints "aa" so action listener works.
It looks like a similar problem to one showed here:
http://bytes.com/topic/java/answers/857720-adding-icon-jpanel
but I can't see even that rectangle about which JosAH wrote.
I'll appreciate any suggestion.
Create a placeholder panel in your editor, and then add the panel to that. I think the problem is to do with layouts and such, since some layouts require some layout data to be present on adding the component (i.e., the second field in add(c, ...) is set). So, create a placeholder JPanel using the GUI creator tool, name it so you have a reference to it, and then add the component to that panel. Give it a layout such as FlowLayout which requires no layout data. You may also want to remove all the insets on the panel so you don't get massive spacing, which you can probably do in the UI editor.