Creating a custom Java Swing tooltip lock

This issue has been resolved.

I am developing a Java Swing designed and the look of the application is completely customizable. We try to maintain a constant appearance of the entire program, and the default dialog boxes for Java do not match.

The current problem requires blocking the control call at the user prompt. Like JOptionPane.showConfirmDialog () In this case, a static call creates a window and stops the program flow until the user selects an option. It also returns the value of the parameter. Please note: the GUI itself is not logically locked, but the user cannot interact with the rest.

int n = JOptionPane.showConfirmDialog(this, "Are you sure?", "Confirm" JOptionPane.YES_NO_OPTION); 

I would like to duplicate this functionality with a custom look and use a string. Ideally, my code would look like this:

 String result = CustomPrompt.showPrompt(this, "Please enter your name", //Text "Prompt.", //Title "John Smith"); //Default 

This is usually used to enter a password, and I understand that the type of password returned is different, but this is an example. This can be achieved with a series of buttons and event listeners in several classes, but code readability and application reliability are reduced.

The frame will be built through NetBeans and configured from there. I understand that such an invitation already exists in Swing, but it looks and feels completely and completely different.

Consolidated question: how to use a custom frame to request the user to enter in a blocking way.

The solution to this problem is as follows:

 public class PromptForm extends JDialog { transient char[] result; public char[] getResult() { return result; } public PromptForm(Frame parent) { super(parent, true); initComponents(); } public void prompt() { this.setVisible(true); } public static char[] promptForPassword(Frame parent) { PromptForm pf = new PromptForm(parent); pf.prompt(); return pf.getResult(); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { result = jPasswordField1.getPassword(); setVisible(false); dispose(); } private void initComponents() {...} private javax.swing.JButton jButton1; private javax.swing.JPasswordField jPasswordField1; } 

Called:

  char [] ret = PromptForm.promptForPassword(this); JOptionPane.showMessageDialog(this, new String(ret), "Neat", JOptionPane.QUESTION_MESSAGE); 
+6
source share
1 answer

Make CustomPrompt extends JDialog , ask its call super constructor to pass to the Window owner and the desired ModalityType .

 public class CustomPrompt extends JDialog { public static String showPrompt(Window parent, String title, String text, String defaultText) { final CustomPrompt prompt = new CustomPrompt(parent); prompt.setTitle(title); // set other components text prompt.setVisible(true); return prompt.textField.getText(); } private JTextField textField; // private if you only want this prompt to be accessible via constructor methods private CustomPrompt(Window parent) { super(parent, Dialog.DEFAULT_MODALITY_TYPE); // Java >= 6, else user super(Frame, true) or super(Dialog, true); initComponents(); // like Netbeans } // initComponents() and irrelevant code. } 
+5
source

Source: https://habr.com/ru/post/907405/


All Articles