Hi everybody,

I was wondering if it is possible (and if it is, how to do it) to put a JPasswordField into a JOptionPane.

Thanks in advanced,
C++

Recommended Answers

All 10 Replies

I think JDialog is the best to use when puting custom components like that in.

can you please explain how to use JDialog???

Thanx in advanced,
C++

Here is an example, but remember this is just an inner class.

class processCylinderDialog extends JDialog implements ActionListener
 {
	 JLabel lblRadius;
	 JLabel lblHeight;
	 JTextField txtRadius;
	 JTextField txtHeight;
	 JButton btnDone;
	 
	 JDialog dlg;
	 
	 public processCylinderDialog()
	 {
		
		 JPanel panelOne = new JPanel();
		 lblRadius = new JLabel("Radius");
		 txtRadius = new JTextField(10);
		 panelOne.add(lblRadius);
		 panelOne.add(txtRadius);
	
		 JPanel panelTwo = new JPanel();
		 lblHeight = new JLabel("Height");
		 txtHeight = new JTextField(10);
		 panelOne.add(lblHeight);
		 panelOne.add(txtHeight);
		
		JPanel panelThree = new JPanel();
		btnDone = new JButton("Calculate");
		btnDone.addActionListener(this);
		panelThree.add(btnDone);
		
		 
		 dlg = new JDialog();
		 dlg.setResizable(false);
		 dlg.getContentPane().add(panelOne);
		 dlg.getContentPane().add(panelTwo);
		 dlg.getContentPane().add(panelThree);
		 dlg.setTitle("Cylinder Volume");
		 dlg.getContentPane().setLayout(new FlowLayout());
		 dlg.setSize(350,300);
		 dlg.setVisible(true);
	 }
	 
	 public void actionPerformed(ActionEvent ae)
	 {
		 getInfo();
	 }
	 public void getInfo()
	 {
		 if (txtRadius.getText() != null && txtHeight.getText() != null)
		 {
	   try
	   {
		 double cylRadius = Double.parseDouble(txtRadius.getText());
		 double cylHeight = Double.parseDouble(txtHeight.getText());
		 double cylVolume = Math.PI * cylRadius * cylRadius * cylHeight;
		 txtResult.setText(dec.format(cylVolume) + "");
	   }
	   catch(NumberFormatException nfe)
	   {
	   }
	}
}

You can see that it is just like creating a class that extends JFarme with a few modifications.

When you want to display this dialog, you call it like this:

new className();

Thank you very much, server_crash!!!

Thank you very much, server_crash!!!

Your very welcome, and if you need any further help just let me know, or Pm me.

Actually, that didn't work!!

Is there any other way (Preferably in JOptionPane)?

regard previous message - i forgot to put in:

new className();

Why don't you post your code, preferably all of it, but most importantly the JDialog inner class and where you call it.

thanx - but the code's working now. I have one more question:

Is there a way to figure out which character is most common in a string?

Here's a common example:

static int countChars( String str, char searchChar ) {
                  // Count the number of times searchChar occurs in
                  // str and return the result.
                int i;     // A position in the string, str.
                char ch;   // A character in the string.
                int count; // Number of times searchChar has been found in str.
                count = 0;
                for ( i = 0;  i < str.length();  i++ ) {
                    ch = str.charAt(i);  // Get the i-th character in str.
                    if ( ch == searchChar )
                       count++;
                }
                return count;
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.