Auto refreshing a database and connection with java - java

I am using Frame with card layout and a few panels.
In the first panel, there is a textField object that reads name and a button, which on clicking adds the name to the database table. In the second panel, there is a dropdown box which reads the vales from the database which was inserted from the previous panel.
My problem is that, the vales from the db are not added to the dropdown box in the 2nd panel.
This will only work when I close the program and run it again.
Is there a way to refresh the tables and db and the database connection, so that, I will be able to see the values in the dropdown box without having to restart the application ?

Just bind an event to the button to make your dropdown menu to refresh.

Related

how to synchronize 2 tabs JTabbedPane Swing

I work with swing to build GUI. Hello everyone
I have 2 tabs (JTabbedPane) that I want to synchronize.
For example, on the image below, I would like that if I click on the Rent button, the line can be automatically added to the My leasing tab so that if I go to the My Leasing tab, I find the line that has just been added.
Currently, to see the line that has just been added to the My leasing tab, I must log out and log back in.
I tell myself that there may be a listener to put in place but I do not know which one.
You need to respond to the click of the "Rent" button.
Check out the Table Button Column. This class allows you to add an ActionListener which is invoked whenever the button is clicked (or activated by using the space bar).
The example code shows how to delete a row from the model. In your case you would want to copy the data from one model to the other.

Java: JButton that creates JTextField everytime it is pressed

Hy everyone,
I was just learning some new things with Java GUI, and I got stuck with a problem. This is what it looks like:
I want a button that adds the data from the second JPanel in to the first one.But not only once. I want it to go to the first JPanel, everytime it is pressed.
So, the button should create a new JTextField in the first JPanel, below their respective categories "name" and "age" everytime it is pressed. It means that I have to modify the "Y" field everytime, so all the new JTextFields created by the button "add data" dont get pilled up.
I don't know how to make the button "add data" Works, the other things, I know how to do. I know how to make the button creates a JTextField, with the data I want to store, only once (using getText() and setText() ), but not how to create new JTetFields with diferente "y" field everytime it is pressed.
Thank you for the help.
When you click on the button to add people you should not display a new JPanel. Instead you should display a modal JDialog. A JDialog is just like a frame but generally is only displayed for a specific function and then closed.
On this dialog you will probably want two buttons: 1) Save Data and 2) Close. This will allow you to enter information for multiple people before closing the dialog.
To display the "person" information you should probably be using a JTable. A table allows you to display data in a row/column format. See the section from the Swing tutorial on How to Use Tables for basic information to get you stated. Note, in this cause you will want to use a DefaultTableModel for your table and then you can just use the addRow(...) method to save the "person" information.
When you create the class for the dialog to get the "person" information you will want to pass the DefaultTableModel to this class as a parameter. Then when you click the "Save Data" button you get the information from the two text fields and update the TableModel. The table will be updated automatically.
So your first step is to create the main frame with the JTable with the two columns. Then you create a simple "Add Person" button. This button will then add hard coded data to the table each time it is clicked by using the addRow(...) method. Once you get this working then you change the logic of the button to display the dialog and then you can enter the "person" information on the dialog and save it.

Reload the page on button click in netbeans

I am making an accounting software using Netbeans and MySQL.
I need to reload the page on a button click.
As in, the page takes entry for a product. When Add button is clicked, the entry should go into the database and the JFrame that takes the entry should be reloaded to take next entry.
The entry gets entered into the database but the reloading fails and the project needs to be rerun in order to upload next entry.
The entry system takes an input from a combo box and 10 other text boxes. On clicking the add button, the entries get added to the databas and all the fields are reset. Indeed. But the problem is, now the fields arent editable anymore.
revalidate() and repaint() aren't working.
What other options are there ?
Clear the JTextField text elements after clicking the button and change the selected indexes for the JComboBox elements. For instance, in your button's ActionListener:
tfTextField.setText("");
cbComboBox.setSelectedIndex(-1);
this.dispose();
JFrame f = new Add();
f.setVisible(true);
This did it.
The form gets disposed and then it loads again.

JTable cell loses focus after run an action on selected cells if use keyboard shortcut and temporaily disable the JTable

In my JTable I have a number of actions that can accessed via popup menu or by selecting the configured shortcut. Selecting the action from the popup using mouse or keyboard is fine, and I can use the cursor keys to move to a field next to the original selection no problem. However if I use the shortcutkey instead it performs the action okay but I cannnot exit the selected fields afterwards using the cursor keys, because for some reason the focus is now with a component outside of the JTable.
EDIT:When I start the task I change the cursor and disable the JTable, when I complete the task I renable the table and reset the cursor. If I remove the disable code it works, but this then allow the user to make changes to the table which I dont want, and I cannot understand why it only fails when using keyboard shortcut.
Fixed the problem, after renabling the Jtable I needed to call requestFocusInWindow()

How do I make dynamic windows in Swing?

I have a general question. I would like to have a window containing some buttons, radio buttons, text fields and so on. So, user can do something (write text, select options and press buttons). As the result of the user activity window should change it structure/appearance some element should disappear and some appear.
How do I program such "updates"? Should I close an old window and open a new one or I can modify content of window without closing it?
After adding your components or such, calling revalidate() on your container will do the updates

Categories

Resources