Results 1 to 8 of 8
Thread: how to fix it?
- 07-04-2012, 01:07 PM #1
Member
- Join Date
- May 2012
- Posts
- 17
- Rep Power
- 0
- 07-04-2012, 01:45 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: how to fix it?
What is the code that creates that panel?
Please do not ask for code as refusal often offends.
- 07-04-2012, 05:20 PM #3
Member
- Join Date
- May 2012
- Posts
- 17
- Rep Power
- 0
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"); } } } }
- 07-04-2012, 05:40 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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.
- 07-04-2012, 08:25 PM #5
Member
- Join Date
- Jul 2012
- Posts
- 17
- Rep Power
- 0
Re: how to fix it?
hi,
you can try a flow layout
-vjLast edited by b4viral; 07-04-2012 at 08:29 PM.
- 07-04-2012, 10:27 PM #6
Re: how to fix it?
Please go through the Forum Rules -- particularly the third paragraph.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 07-04-2012, 11:38 PM #7
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
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".
- 07-05-2012, 12:48 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.


LinkBack URL
About LinkBacks

Reply With Quote
Bookmarks