I have a main window called MainFrame which is a jForm to which I update the data depending on a timer, but the problem is that I cannot update the data in the same MainFrame after using the jdialog, since I end up creating another duplicate window, but with the data changed, one with the original timer and the other with the new timer, I know that I can close the first window with dispose() and then keep the second, but I would like to avoid changing windows so much
the code with which I create another window when pressing the jDialog button is the following
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: String textoFieldTimer = jTextField1.getText(); int timeUserConfig = Integer.parseInt(textoFieldTimer); Timer timeDefault = new Timer(timeUserConfig, null); TokenAccess token = new TokenAccess(); token.access_code = code; MainFrame mainFrame = new MainFrame(token); mainFrame.setVisible(true); mainFrame.timeDefault.stop(); mainFrame.timeDefault = timeDefault; mainFrame.setUpdateTime(timeUserConfig); this.dispose(); }//GEN-LAST:event_jButton1ActionPerformed
Is there any alternative to update the window? something like mainFrame.update();
or maybe send the value of the jTextField from the jDialog to mainFrame? since the previous code creates another MainFrame for me.
Method main setLabel and Timer.start/stop
public void setUpdateTime(int timeUserConfig) { this.timeUserConfig = timeUserConfig; if (timeUserConfig == 0) { timeDefault.start(); timeDefault.addActionListener(new java.awt.event.ActionListener() { @Override public void actionPerformed(java.awt.event.ActionEvent evt) { setLabelText(); String timeUserConfigStr = Integer.toString(timeDefaultInt); tiempoActualizado.setText("Tiempo de Actualizado: " + timeUserConfigStr+"ms"); } }); } else { timeDefault.stop(); timeDefault = new Timer(timeUserConfig, null); timeDefault.start(); timeDefault.addActionListener(new java.awt.event.ActionListener() { @Override public void actionPerformed(java.awt.event.ActionEvent evt) { setLabelText(); String timeUserConfigStr = Integer.toString(timeUserConfig); tiempoActualizado.setText("Tiempo de Actualizado: " + timeUserConfigStr+"ms"); } }); } }
setLabelText is a method set of label
public void setLabelText() { String humedadStr = String.valueOf(humedad); String temperaturaStr = String.valueOf(temperatura); String presionStr = String.valueOf(co2); temporalHum.setText(humedadStr); temporalTemperatura.setText(temperaturaStr); temporalPresion.setText(presionStr); }
Any help would be appreciated.
Advertisement
Answer
Thanks for the update, and I found another solution without using an OptionPane
from this question: programmatically close a JPanel which is displayed in JDialog.
I cannot replicate your codings
Start with the MainFrame
, assuming you opened the JDialog
by clicking on a button and wants to setText()
to label lbSomething
:
private void btInputActionPerformed(java.awt.event.ActionEvent evt) { // Open new JDialog when button is clicked NewJDialog dialog = new NewJDialog(new javax.swing.JFrame, true); dialog.setVisible(true); // Get user input from JDialog String temp = dialog.getInput(); if (temp != null) { /* * Perform jButton1ActionPerformed() content here * Including timeUserConfig, timeDefault and setUpdateTime() here * so that you don't have to access mainFrame in the JDialog. */ lbSomething.setText(temp); } }
Then about the JDialog
(with simple input detection):
public class NewJDialog extends javax.swing.JDialog { // Set the variable as class variable private String textTOFieldTimer; public NewJDialog(java.awt.Frame parent, boolean modal) { // default contents } @SupressWarinings("unchecked") private void initComponents() { // default contents } private void btSaveAction Performed(java.awt.event.ActionEvent evt) { // Check if input correct and whether to disable JDialog if (tfInput.getText.length() != 0) { input = tfInput.getText(); // Connect to the whole JDialog by getWindowAncestor() Window window = SwingUtilities.getWindowAncestor(NewJDialog.this); // Just setVisible(false) instead of dispose() window.setVisible(false); } else { JOptionPane.showMessageDialog(this, "Wrong Input"); } } public String getInput() { return textToFieldTimer; } // default variables declarations }
Hope this answer helps you well.
Would be better if you displayed the source code, but a simple solution to update values to an existing JFrame is by using setText()
and getText()
.
For example:
String input = JOptionPane.showInputDialog(this, "Nuevo valor"); lbPresionActual.setText(input);
If you created a self-defined JDialog
, it is about to transfer the input
value when closing the JDialog
, and that could be a different question.