Results 1 to 8 of 8

Thread: how to fix it?

  1. #1
    djokovic is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default how to fix it?

    I'm trying to create email system. with this form:
    how to fix it?-problem.jpg

    problem is, that I'm using boxLayout and when I type smth in textare, the labels (to, cc, subject) is going to the right position. like this
    how to fix it?-problem2.jpg

    how to fix this? I tried to use center alignment but it doesn't work.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: how to fix it?

    What is the code that creates that panel?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    djokovic is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: how to fix it?

    Java Code:
    public class emailMessage extends JPanel{
    
    		private JTextField To;
    		private JTextField CC;
    		private JTextField subject;
    		private JLabel ToInfo;
    		private JLabel CCInfo;
    		private JLabel subjectInfo;
    		private JButton send;
    		private JTextArea text;
    		private JOptionPane message;
    		private String subjectText;
    		private String ToText;
    		private String CCText;
    		
    		public emailMessage(){
    			To = new JTextField(20);
    			CC = new JTextField(20);
    			subject = new JTextField(20);
    			
    			ToInfo = new JLabel("To : ");
    			
    			CCInfo = new JLabel("CC : ");
    			subjectInfo = new JLabel("Subject : ");
    			text = new JTextArea(20,30);
    			
    			send = new JButton("Send");
    			
    			send.addActionListener(new sendListener());
    			
    			setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    			setAlignmentX(CENTER_ALIGNMENT);
    			
    			add(ToInfo);
    			add(To);
    			add(CCInfo);
    			add(CC);
    			add(subjectInfo);
    			add(subject);
    			add(text);
    			add(send);
    		}
    		
    		private boolean notEmpty(){
    			 ToText = To.getText();
    			 CCText = CC.getText();
    	         subjectText = subject.getText();
    			if((ToText.length() > 0) && (CCText.length() > 0) && (subjectText.length() > 0) && (text != null)){
    				return true;
    			}else{
    				return false;
    			}
    		}
    		private class sendListener implements ActionListener{
    			public void actionPerformed(ActionEvent arg0) {
    				if(notEmpty()){
    					message = new JOptionPane();
    					message.showMessageDialog(null,"Email sent");
    					resultsWindow rw = new resultsWindow(ToText,CCText,subjectText,text);
    				}else{
    					message = new JOptionPane();
    					message.showMessageDialog(null,"Some of the fields are empty");
    				}
    			}
    			
    		}
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: how to fix it?

    Right.
    Using Box you need to group things a bit, often paired with glue and struts to keep elements apart or push them one way or the other.
    In this case I would create a horizontal Box (Box.createHorizontalBox()) for each label, adding the label and then a horizontalGlue to it. That'll shove the label off to the left.

    I would say that extending JPanel in this case is pretty much wrong. It's a bit like extending ArrayList in order to populate it with objects.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    b4viral is offline Member
    Join Date
    Jul 2012
    Posts
    17
    Rep Power
    0

    Default Re: how to fix it?

    hi,

    you can try a flow layout

    -vj
    Last edited by b4viral; 07-04-2012 at 08:29 PM.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: how to fix it?

    Please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: how to fix it?

    @djokovic: If I understood your question you're want that your text in "JTextArea text" component to go from central position instead from left to right direction.

    So, when you have written this code: "setAlignmentX(CENTER_ALIGNMENT);" and your public class extends JPanel it means that your Panel x position will have "CENTER_ALIGMENT", but what you are really want is that your component "JTextArea text" have "CENTER_ALIGMENT".

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: how to fix it?

    @cselic: I think it's the other way around - the OP wants the text area as it is, but the labels and text fields left aligned. Ie the desired layout is the first in this thread. Perhaps he or she can say.

    Tolls has suggested putting the label+field into a horizontal box, together with glue and struts to make the layout look nice. Stacking such horizontal boxes in a vertical box is a common layout idiom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •