4 Replies - 504 Views - Last Post: 05 August 2012 - 12:39 PM Rate Topic: -----

#1 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

JDialog resize, and String question

Posted 05 August 2012 - 10:51 AM

My final project for Java is, done, and due tomorrow. I keep adding extra stuff, I can't help myself. Today I am trying to add a list of contacts in checkboxes, to choose who you send a text message to. That works fine, but I am having issues with my JDialog box, solely stemming from the fact that I am teaching myself about them over the last 2 days.

First question.. I am trying to concat several strings into one long string with carriage returns after each entry. That String is then placed inside a text box. Right now, when I render my textbox, the String is all one long line. I don't understand why. Code:

if (e.getSource()== btnSelect)
			{
				
				
				String chosen = "";
				if (cb1.isSelected())
					chosen = chosen + "" + cb1.getText() + "\r\n";
				if (cb2.isSelected())
					chosen = chosen + "" + cb2.getText() + "\r\n";
				if (cb3.isSelected())
					chosen = chosen + "" + cb3.getText() + "\r\n";
				if (cb4.isSelected())
					chosen = chosen + "" + cb4.getText() + "\r\n";
				
				cb1.setVisible(false);
				cb2.setVisible(false);
				cb3.setVisible(false);
				cb4.setVisible(false);
				
				pack();
				
				btnSelect.setVisible (false);
				btnOK.setVisible(true);
				
				
				message.setText("Enter your text message");
				textMessage.setVisible(true);
				
				
				txtChosen.setSize(100,500);
				txtChosen.setVisible(true);
				txtChosen.setText(chosen);
			}



This JDialog box has components that come and go. Maybe I am doing it all wrong. But the Window doesn't have room for the new components after the user selects their recipients, even though I called the pack() method. This is all experimentation on my part, and I am out of time, I need to wrap this up and turn it in. I will take away the text window if I have to, but I'd rather not since I spent all day working on it.

How can I resize my window to hold the new components? I tried calling setSize(), but it didn't work. Here is the code, I know I need to clean it up a bit.. thank you!!

import javax.swing.JDialog; 
import java.awt.Frame;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
//*****************************************
//*** FILE: Textwindow.java                *
//***  PROG:		  *
//**  PURP: Final Project - choose text message recipients    *
//******************************************

class TextWindow extends JDialog implements ActionListener
{
	private JPanel myPanel = null;
	private JLabel message;
    private JButton btnOK, btnSelect;
	private JCheckBox cb1, cb2, cb3, cb4;
	private JTextField textMessage, txtChosen;

	//constructor
	
	 public TextWindow(Frame parent)
	    {
	    	super(parent, true);
	    	
	    	String con1 = "Doctor Who";
	    	String con2 = "Harry Potter";
	    	String con3 = "Frodo Baggins";
	    	String con4 = "Gandolf the Gray";
	    	
	    	myPanel = new JPanel();
	        getContentPane().add(myPanel);
	        
	        message = new JLabel("Who would you like to text?");
	        myPanel.add(message);
	        message.reshape(10, 10, 200, 50);  
	        
	        cb1 = new JCheckBox(con1, false);
	        myPanel.add(cb1);
	        //cb1.addActionListener(this);
	        
	        cb2 = new JCheckBox(con2, false);
	        myPanel.add(cb2);
	        //cb2.addActionListener(this);
	        
	        cb3 = new JCheckBox(con3, false);
	        myPanel.add(cb3);
	        //cb3.addActionListener(this);
	        
	        cb4 = new JCheckBox(con4, false);
	        myPanel.add(cb4);
	        //cb4.addActionListener(this);
	        
	        btnSelect = new JButton("Select");
	    	btnSelect.addActionListener(this);
	    	myPanel.add(btnSelect);
	    	
	    	textMessage = new JTextField(25);
	    	myPanel.add(textMessage);
	    	textMessage.setVisible(false);
	    	
	    	txtChosen = new JTextField("20");
	    	myPanel.add(txtChosen);
	    	txtChosen.setSize(30,30);
	    	txtChosen.setVisible(false);
	    	
	    	btnOK = new JButton("OK");
	    	btnOK.addActionListener(this);
	    	myPanel.add(btnOK);
	    	btnOK.setVisible(false);
	    	
	    	
	    	 pack();
	         setVisible(true);
	      
}
	 
	 public void actionPerformed(ActionEvent e) 
		{
			
			if (e.getSource()== btnSelect)
			{
				
				
				String chosen = "";
				if (cb1.isSelected())
					chosen = chosen + "" + cb1.getText() + "\r\n";
				if (cb2.isSelected())
					chosen = chosen + "" + cb2.getText() + "\r\n";
				if (cb3.isSelected())
					chosen = chosen + "" + cb3.getText() + "\r\n";
				if (cb4.isSelected())
					chosen = chosen + "" + cb4.getText() + "\r\n";
				
				cb1.setVisible(false);
				cb2.setVisible(false);
				cb3.setVisible(false);
				cb4.setVisible(false);
				
				pack();
				
				btnSelect.setVisible (false);
				btnOK.setVisible(true);
				
				
				message.setText("Enter your text message");
				textMessage.setVisible(true);
				
				
				txtChosen.setSize(100,500);
				txtChosen.setVisible(true);
				txtChosen.setText(chosen);
			}
		 
		 
		 	if(e.getSource()==btnOK) 
	        {
				setVisible(false);
	        }
		}//END LISTENER
	 
}//END TEXTWINDOW CLASS


Is This A Good Question/Topic? 0
  • +

Replies To: JDialog resize, and String question

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: JDialog resize, and String question

Posted 05 August 2012 - 12:13 PM

Quote

First question.. I am trying to concat several strings into one long string with carriage returns after each entry. That String is then placed inside a text box. Right now, when I render my textbox, the String is all one long line. I don't understand why.

Strings inside Java components can usually be formatted using HTML. Try that.

I'm not sure what else you're trying to do, so I'll stop there.
Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: JDialog resize, and String question

Posted 05 August 2012 - 12:22 PM

JTextField cannot contain multiple lines. Use instead something like JTextArea
Was This Post Helpful? 1
  • +
  • -

#4 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1378
  • View blog
  • Posts: 3,038
  • Joined: 05-April 11

Re: JDialog resize, and String question

Posted 05 August 2012 - 12:29 PM

Instead of cb1, 2, 3, 4 use an array :)

With a textbox are you meaning a JTextArea ?
Was This Post Helpful? 1
  • +
  • -

#5 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: JDialog resize, and String question

Posted 05 August 2012 - 12:39 PM

View PostCasiOo, on 05 August 2012 - 02:29 PM, said:

Instead of cb1, 2, 3, 4 use an array :)

With a textbox are you meaning a JTextArea ?

No, I was using a JTextField. I've never heard of a JTextArea, I will go try it now!

EDIT! It worked, thanks so much!! There are a million things I wish I could do to this project, but I am out of time :nottalkingtoyou:

This post has been edited by synlight: 05 August 2012 - 01:02 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1