fix: salvando nome e porta

This commit is contained in:
tonyhcj 2026-07-29 15:00:44 -03:00
parent 239a10e79f
commit 7f21e89a0a
1 changed files with 15 additions and 9 deletions

View File

@ -1,8 +1,3 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pitiupi.GUI; package pitiupi.GUI;
import java.awt.BorderLayout; import java.awt.BorderLayout;
@ -11,6 +6,7 @@ import java.awt.GridBagConstraints;
import java.awt.GridBagLayout; import java.awt.GridBagLayout;
import java.awt.Insets; import java.awt.Insets;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.util.prefs.Preferences;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JDialog; import javax.swing.JDialog;
@ -26,6 +22,10 @@ import javax.swing.JTextField;
*/ */
public final class Login extends JDialog { public final class Login extends JDialog {
private static final Preferences prefs = Preferences.userNodeForPackage(Login.class);
private static final String PREF_NOME = "lastUserName";
private static final String PREF_PORTA = "lastPort";
JLabel nameLabel = new JLabel("Name: "); JLabel nameLabel = new JLabel("Name: ");
JLabel portLabel = new JLabel("Port: "); JLabel portLabel = new JLabel("Port: ");
@ -44,8 +44,12 @@ public final class Login extends JDialog {
public void initComponents() { public void initComponents() {
this.setTitle("Login"); this.setTitle("Login");
portField.setModel(new javax.swing.SpinnerNumberModel(8000, 8000, 9000, 1));
nameField.setText(System.getProperty("user.name")); String nomeSalvo = prefs.get(PREF_NOME, System.getProperty("user.name"));
int portaSalva = prefs.getInt(PREF_PORTA, 8000);
portField.setModel(new javax.swing.SpinnerNumberModel(portaSalva, 8000, 9000, 1));
nameField.setText(nomeSalvo);
JPanel topPanel = new JPanel(new GridBagLayout()); JPanel topPanel = new JPanel(new GridBagLayout());
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
@ -83,17 +87,19 @@ public final class Login extends JDialog {
this.add(topPanel); this.add(topPanel);
this.add(buttonPanel, BorderLayout.SOUTH); this.add(buttonPanel, BorderLayout.SOUTH);
okButton.addActionListener((ActionEvent e) -> { okButton.addActionListener((ActionEvent e) -> {
try { try {
portField.commitEdit(); portField.commitEdit();
} catch (java.text.ParseException ex) { } catch (java.text.ParseException ex) {
return; return;
} }
prefs.put(PREF_NOME, nameField.getText());
prefs.putInt(PREF_PORTA, (Integer) portField.getValue());
Login.this.setVisible(false); Login.this.setVisible(false);
}); });
cancelButton.addActionListener((ActionEvent e) -> { cancelButton.addActionListener((ActionEvent e) -> {
System.exit(0); System.exit(0);
}); });