Advice: Java GUI Using JFrame - java

I Have created a form and designed it using Swing Components. It is linked to MySQL, so i have few buttons, like Submit (Which When Clicked validates and updates the database). But I also have buttons to view and edit database. When clicked i have worked on Delete Record using JOptionPane (YES_NO_OPTION etc), but when it comes to editing, i want to put Combo boxes and Text Fields etc, which might not be preferred in JOptionPane. Creating a new Window would help, but is there any other easier Default Classes like JOptionPane in which i can use Many components? Also to display Database records?

I believe what you are looking for are JDialogs, these are the "small" popup windows you are looking for. You may add any swing component you wish to it.
Here is an example of creating one and adding a JTextField to it.
JDialog popup = new JDialog();
//Set window title
popup.setTitle("Example");
//Set window size
popup.setWindowSize(300, 200);
//Force window to stay on top till exit
popup.setModal(true);
//Create and add a JTextField
JTextField input = new JTextField();
popup.add(input);
popup.setVisable(true);

Related

jtextfield retains value in jdialog after closing jdialog

I'm new to Java and Swing. I created a jframe and I added a menubar and MenuItem in it.
On clicking a menu item, a jdialog should open. Now the jdialog has a jtextfield in it and a jlabel. Now the problem for me is 'when dialog is opened for first time, the textfield is empty and thats correct. Now i close the jdialog and i open it again but now instead of getting an empty textfield in jdialog, i get the data entered previously' which is not what should happen as the jdialogs 'default close operation' property is set to 'dispose'. but that is not happening for me...
I dont know what i'm doing wrong. I have never tried applet/swing before in any other way (consider this as my first demo learning programme)
Second Image here
The JTextField is retaining it's value because it isn't being affected by the JDialog closing, instead it is being hidden as it's parent (the JDialog) is invisible
Setting the dialog to dispose isn't re-initialising the child components, so they keep their values. Some additional information on this behaviour is available here:
JDialog setVisible(false) vs dispose()
JDialog
One way you can prevent / control this is by "informing" the dialog to wipe the textfield as it is closing by adding a WindowEvent and providing the necessary functionality in the windowClosing() method
Netbeans gui-builder will generate this for you with the following:
Right click Dialog
Events
Window
WindowClosing
Providing:
private void jDialog1WindowClosing(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
}
In which you can add: textfield.setText(""); to clear the textfield
Another approach is to create your own dialog and setting up the components in the constructor. As creating a new instance will contain the components with their default values, effectively resetting it

JDialog layout issues

I'm using a JDialog to create a customized dialog box for my Java project. I'm having issues with the layout at the moment. It seems each JLabel I add to the dialog goes over the existing one. Do I need to add some sort of JPanel?
I also seem to have a issue with the size. I set it too 500x500 but why does it only goes as large as the text width?
JDialog processData = new JDialog(f1, "TItle goes here");
JLabel centretext = new JLabel("Look at me im centre!");
JLabel leftext = new JLabel("LOok at me im left");
JLabel righttext = new JLabel("LOok at me im right");
processData.setVisible(true);
processData.add(centretext);
processData.add(lefttext);
processData.add(rightext);
processData.toFront();
processData.setSize(500,500);
processData.setLocation(500,500);
processData.pack();
JDialog uses a BorderLayout by default, which means, it will only show a single component in any of the five available positions, all the others get ignored.
Consider using a different layout manager. See Laying Out Components Within a Container for more details

java swing jpanel display when clicking a button

i'm new to swing. i've created a jpanel form using the swing palette design. It has jlabels, jtextfields and a jbutton. when the button is clicked, the data entered in the jtextfields is inserted in the database through the model.
i don't want the the jpanel to be always displayed so i would like to add another button that displays this jpanel form when i click on it. is it possible to perform this using the palette design knowing that the source code is locked in the initComponent private function, or do i have to re-code all the components and layout myself?
thanks for your time!
"knowing that the source code is locked in the initComponent private function"
You can edit the auto-generated source code, as see here in this answer.
"that displays this jpanel form when i click on it. is it possible to perform this using the palette design"
Sounds like you want to swap views. You can do that using CardLayout and yes, it can be done from the design view. You can see more at How to Use CardLayout with Netbeans GUI Builder

Java Swing: Cannot Edit JComponents in JWindow Extension

I wrote a class that extends JWindow that serves as a kind of customizable dialog box in my application. When I need to invoke one of these windows, I create a new instance of the class; to remove the window, I call the method dispose().
The problem I am having is that the user cannot edit components that have a text box, such as JTextField and JSpinner. The user can click on components such as drop-down boxes and buttons, and this works fine, but when it comes to entering text in a text box, this does not work.
Has anyone else experienced this problem?
Thanks!
There are a bunch of conditions to meet until a window child can receive focus, see the api doc for window.isFocusableWindow().
for most contexts it's enough to set its focusableWindowState property to true, like
JFrame owner = new JFrame();
owner.setVisible(true);
JWindow window = new JWindow(owner);
window.setFocusableWindowState(true);
window.add(new JTextField("edit me"));
window.setSize(200, 200);
window.setVisible(true);
1) maybe you are mixing AWT with Swing drop-down box and button, are you sure that all Components definitions starts with J, drop-down boxes == JComboBox, Button == JButton etc
2) don't create lots of Top-Level Containers on the Fly/Runtinme
3) Has anyone else experienced this problem? no, never
4) for real and better help sooner, please edit you post and sent here code that demonstrate your problem here are simples rulles sscce

JFrame in a Java desktop application

I am developing a desktop Java application with GUI implemented through Swing. I have made a JFrame and have added three buttons on it - Add, Edit, Delete.
Now I want that whenever a user clicks on any of the button, the content specific to that button appears besides those three buttons.
So how to implement this? Should I need to add a JPanel besides those three buttons and then add the content specific to the button to that JPanel?
So far, I have taken a JFrame and have added 3 buttons on it. That's it.
For the Add button, I want to add some buttons and textfields to add information to the database.
For the Delete button, I want to add some buttons to find records in the database based on the information entered through the user in the textfield that appears when the user clicks on the Delete button.
Similar type of content for Edit button.
So how to implement this. Should I need to add a JPanel besides those three buttons and then add the content specific to the button to that JPane
That would be fine. When you push the button, you can call JPanel.removeAll() to remove all the controls currently in the control, and then just do the layout again, specific to whatever button you pushed.
If you have custom swing controls, just add your custom control the JPanel using a BorderLayout and putting in the center.
Another option would be to use a CardLayout, and flipping between the cards when a user presses one of the buttons. If the layouts for the buttons never change, that would probably be a better way to do it. Obviously if the content changes between button presses, you'll need to redo the layout each time.
Either of Chad's or Alex's answers would be fine. You will probably need to call a combination of revalidate() and repaint() on the panel that you've changed, as in the past I've noticed Swing doesn't always like panels being swapped out.
Also, have you considered using a JTabbedPane instead of manually coding the interaction with the add/edit/delete buttons?
I haven't done a lot of Java programming, but I think using 2-3 different JPanel, and make visible the one you need depending on the button that was clicked would do the trick.
I'm not sure if this is the right approach though.
I was using a JFrame to add all buttons and make a new JFrame for a new window and hide a previous one.
gven way are better. I will do that now.

Categories

Resources