2 Replies - 555 Views - Last Post: 09 October 2012 - 07:57 AM Rate Topic: -----

#1 Anne2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-October 12

Need to add text to JOptionPane.showConfirmDialog

Posted 09 October 2012 - 06:21 AM

Like many of the posts I've read through, I'm also new to Java. All the code I've written works with one exception. I have the JoptionPane set-up to display multiple input fields. I am able to get all the fields I require and I can use the data entered as I need. What I am having problems with is putting a label on the field in the dialog box. I am able to put either the fields or text but not both.

int value = JOptionPane.showConfirmDialog(null, userdatafields, "Dialog Box title", + JOptionPane.OK_CANCEL_OPTION);
if (value == JOptionPane.OK_OPTION)

userdatafields grabs the fields I need and inserts them into the box. "Dialog Box Title" is the text that is displayed in the title bar of the box.

any suggestions would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Need to add text to JOptionPane.showConfirmDialog

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,837
  • Joined: 05-April 11

Re: Need to add text to JOptionPane.showConfirmDialog

Posted 09 October 2012 - 06:46 AM

Uhm so you want to do some gui stuff? Not the easiest thing for a beginner!

You might be better off using multiple showInputDialog s, it will be much easier for you

If I understand you correctly, then you want to show a label and an input box to the right of it?
The example blow does just that. It uses GridBagLayout which is one of the hardest layout managers to understand (I think), and I guess you haven't dealt with GUI components before
Oh well.. At least you can see what it looks like. You might be able to do some copy pasting without understanding the code 100%

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Snippet {

	public static void main(String[] args) {
		JTextField name = new JTextField(10);
		JTextField address = new JTextField(10);
		
		JPanel panel = new JPanel(new GridBagLayout());
		//Add name
		addComponent(panel, new JLabel("Name"), 0, 0, GridBagConstraints.NONE);
		addComponent(panel, name, 1, 0, GridBagConstraints.HORIZONTAL);
		//Add address
		addComponent(panel, new JLabel("Address"), 0, 1, GridBagConstraints.NONE);
		addComponent(panel, address, 1, 1, GridBagConstraints.HORIZONTAL);
		
		int value = JOptionPane.showConfirmDialog(null, panel, "Dialog Box title", JOptionPane.OK_CANCEL_OPTION);
		if (value == JOptionPane.OK_OPTION) {
			System.out.println(name.getText());
			System.out.println(address.getText());
		}
	}
	
	private static void addComponent(Container container, Component component, int gridX, int gridY, int fill) {
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = gridX;
		c.gridy = gridY;
		c.fill = fill;
		container.add(component, c);
	}
}


Was This Post Helpful? 1
  • +
  • -

#3 Anne2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-October 12

Re: Need to add text to JOptionPane.showConfirmDialog

Posted 09 October 2012 - 07:57 AM

Thanks, that works well, and your right that is above my level. Initially I was using showInputDialog and had everything working correctly until I decided to many dialog boxes were appearing and I tried to streamline things by placing 4 input user fields onto the same dialog box. I was able to get that working as well, just couldn't label the fields.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1