JTextArea question

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2006
Posts: 134
Reputation: ryy705 is an unknown quantity at this point 
Solved Threads: 0
ryy705 ryy705 is offline Offline
Junior Poster

JTextArea question

 
0
  #1
Jul 25th, 2006
Hello,
I am novice programmer and I am experimenting with swing. I created a scrollabe text area that seems to scroll great when I hold down a key for a long time. But, when I select a different tabbed pane and then come back to the pane containing the text area, I find that all objects in that pane have lost there shapes. What is the remedy for this problem? Please help, I have been stuck with this problem for a whole week. Thanks in advance.
Last edited by ryy705; Jul 26th, 2006 at 12:00 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 216
Reputation: hooknc is an unknown quantity at this point 
Solved Threads: 8
hooknc hooknc is offline Offline
Posting Whiz in Training

Re: JTextArea question

 
0
  #2
Jul 26th, 2006
Wouldn't it be better to but a JTextArea inside of a JScrollPane and then put the pane on your panel?

And what do you mean by your objects lose their shapes?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 134
Reputation: ryy705 is an unknown quantity at this point 
Solved Threads: 0
ryy705 ryy705 is offline Offline
Junior Poster

Re: JTextArea question

 
0
  #3
Jul 26th, 2006
JTextArea is in a scrollpane which is in a tabbed pane. When I type, the text area scrolls to fit the text. The following is my code. Please copile it and invoke the run() method, that will make the gui visible. Then type something very lengthy so the text area scrolls horizontally. Then click on a different tab and come back to the tab containing the text area. You will see that all the gui objects have collapsed.

  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.util.*;
  4. import java.awt.event.*;
  5. import java.util.*;
  6. import javax.swing.border.*;
  7. import javax.swing.event.*;
  8. /**
  9.  * Write a description of class AddBookFrame here.
  10.  *
  11.  * @author (your name)
  12.  * @version (a version number or a date)
  13.  */
  14. public class AddBookFrame extends JFrame
  15. {
  16. // instance variables - replace the example below with your own
  17. private JFrame rb;
  18.  
  19. /**
  20.   * Constructor for objects of class AddBookFrame
  21.   */
  22. public AddBookFrame()
  23. {
  24. super("Address Book");
  25. //setSize(475, 400);
  26. // setVisible(true);
  27. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.  
  29. //******* start List Entry Pane ********
  30. String labels[] = {"Entry1", "Entry2", "Entry3", "Entry4",
  31. "Entry5", "Entry6"};
  32.  
  33. JPanel listEntryPane = new JPanel();
  34.  
  35.  
  36. DefaultListModel model = new DefaultListModel();
  37. for(String x : labels) {
  38. model.addElement(x);
  39. }
  40. JList entryList = new JList(model);
  41. entryList.setVisibleRowCount(4);
  42.  
  43. JScrollPane entryListScrollpane = new JScrollPane(entryList,
  44. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  45. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  46.  
  47. JButton viewEntryButt = new JButton("View");
  48. JButton eraseEntryFromGroupButt = new JButton("Erase Entry");
  49.  
  50. listEntryPane.add(entryListScrollpane);
  51. listEntryPane.add(viewEntryButt);
  52. listEntryPane.add(eraseEntryFromGroupButt);
  53. //******* end List Entry Pane ****************//
  54.  
  55.  
  56. //******* start Group Pane ***************//
  57. JPanel groupPane = new JPanel();
  58.  
  59. JList groupList = new JList(model);
  60. groupList.setVisibleRowCount(4);
  61.  
  62. JScrollPane groupListScrollPane = new JScrollPane(groupList,
  63. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  64. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  65.  
  66. JButton viewListButt = new JButton("View List");
  67. JButton editGroupNameButt = new JButton("Edit Group Name");
  68. JButton eraseGroupButt = new JButton("Erase Group");
  69.  
  70. groupPane.add(groupListScrollPane);
  71. groupPane.add(viewListButt);
  72. groupPane.add(editGroupNameButt);
  73. groupPane.add(eraseGroupButt);
  74. //****** end Group Pane ***********//
  75.  
  76. //******** start ADD/EDIT entry pane **********//
  77. JPanel viewAddPane = new JPanel();
  78. viewAddPane.setLayout(new GridBagLayout());
  79.  
  80.  
  81. final JTextField desigTxtField = new JTextField(20);
  82. final JTextArea detailsTxtArea = new JTextArea(8, 30);
  83. JButton editEntryButt = new JButton("Edit");
  84.  
  85. JButton saveEntryButt = new JButton("Save");
  86. saveEntryButt.addActionListener(new ActionListener() {
  87. public void actionPerformed(ActionEvent e) {
  88. save(desigTxtField, detailsTxtArea);
  89. }
  90. });
  91.  
  92. JButton eraseEntryButt = new JButton("Erase");
  93.  
  94. JScrollPane viewAddScrollpane = new JScrollPane(detailsTxtArea,
  95. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  96. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  97.  
  98. JScrollPane viewAddGListScrollpane = new JScrollPane(groupList,
  99. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  100. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  101.  
  102. addComponent(viewAddPane, new JLabel("Designation:"), 0, 0,
  103. 1, 1, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE);
  104.  
  105. addComponent(viewAddPane, desigTxtField, 1, 0, 3, 1,
  106. GridBagConstraints.NORTHWEST, GridBagConstraints.NONE);
  107.  
  108.  
  109. addComponent(viewAddPane, new JLabel("Details:"), 0, 2,
  110. 1, 1, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE);
  111.  
  112. addComponent(viewAddPane, viewAddGListScrollpane, 4, 1, 3, 3,
  113. GridBagConstraints.NORTHWEST, GridBagConstraints.NONE);
  114.  
  115. addComponent(viewAddPane, viewAddScrollpane, 1, 1,
  116. 3, 3, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE);
  117.  
  118. addComponent(viewAddPane, editEntryButt, 1, 5, 1, 1,
  119. GridBagConstraints.NORTHWEST, GridBagConstraints.NONE);
  120.  
  121. addComponent(viewAddPane, saveEntryButt, 2, 5, 1, 1,
  122. GridBagConstraints.NORTHWEST, GridBagConstraints.NONE);
  123.  
  124. addComponent(viewAddPane, eraseEntryButt, 3, 5, 1, 1,
  125. GridBagConstraints.NORTHWEST, GridBagConstraints.NONE);
  126.  
  127. CheckListCellRenderer renderer = new CheckListCellRenderer();
  128. groupList.setCellRenderer(renderer);
  129. groupList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  130.  
  131. //**************** end add view pane ************///
  132.  
  133. JTabbedPane tabs = new JTabbedPane();
  134. tabs.addTab("List Entry", listEntryPane);
  135. tabs.addTab("View/Edit Entry", viewAddPane);
  136. tabs.addTab("Groups", groupPane);
  137.  
  138.  
  139. setContentPane(tabs);
  140. }
  141.  
  142. private void addComponent(Container container, Component component,
  143. int gridx, int gridy, int gridwidth, int gridheight, int anchor,
  144. int fill)
  145. {
  146. Insets insets = new Insets(2,2,2,2);
  147. GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
  148. gridwidth, gridheight, 1.0, 1.0, anchor, fill, insets, 0, 0);
  149. container.add(component, gbc);
  150. }
  151. public void run()
  152. {
  153. AddBookFrame rb = new AddBookFrame();
  154. rb.pack();
  155. rb.setVisible(true);
  156.  
  157. }
  158.  
  159.  
  160. class CheckListCellRenderer extends JCheckBox
  161. implements ListCellRenderer
  162. {
  163. protected Border noFocusBorder =
  164. new EmptyBorder(1, 1, 1, 1);
  165.  
  166. public CheckListCellRenderer()
  167. {
  168. super();
  169. setOpaque(true);
  170. setBorder(noFocusBorder);
  171. }
  172.  
  173. public Component getListCellRendererComponent(JList list, Object value,
  174. int index, boolean isSelected, boolean cellHasFocus)
  175. {
  176. setText(value.toString());
  177.  
  178. setBackground(isSelected ? list.getSelectionBackground() :
  179. list.getBackground());
  180.  
  181. setForeground(isSelected ? list.getSelectionForeground() :
  182. list.getForeground());
  183.  
  184. setFont(list.getFont());
  185. setBorder((cellHasFocus) ?
  186. UIManager.getBorder("List.focusCellHighlightBorder") :
  187. noFocusBorder);
  188.  
  189. return this;
  190. }
  191. }
  192.  
  193. public void save(JTextField desigTxtField, JTextArea detailsTxtArea)
  194. {
  195. System.out.println(desigTxtField.getText());
  196. System.out.println(detailsTxtArea.getText());
  197. }
  198. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 134
Reputation: ryy705 is an unknown quantity at this point 
Solved Threads: 0
ryy705 ryy705 is offline Offline
Junior Poster

Re: JTextArea question

 
0
  #4
Jul 26th, 2006
I have attached before, during and after pictures which will help explain what I am trying to say. There is no need for you to go through the code.
Attached Thumbnails
before.gif   during.gif   after.gif  
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 216
Reputation: hooknc is an unknown quantity at this point 
Solved Threads: 8
hooknc hooknc is offline Offline
Posting Whiz in Training

Re: JTextArea question

 
0
  #5
Jul 26th, 2006
I'm pretty sure its the way you're working with the GridBagLayout. If i saw correctly there isn't any FILL on any of the components.

Now if you resize your frame larger the components will reappear.

First off, I would recommend that each tabs JPanel be its own class. That should make things a little easier to read inside of your main frame.

Second. Using gridwidth and gridheight tends to be troublesome. I would recommend looking at ways to use everything as a gridwidth and gridheight of 1. Use gridx, gridy, weight and fill to get the effect that you're looking for.

For instance, the labels the textfield, textarea and listbox all go into one JPanel and then have the buttons at the bottom go into their own JPanel. Then place both of those JPanels into another JPanel that has the top jpanel will weightx of 1, weighty of 1, fill of both. The bottom panel would only have a wieghtx of 1, fill of both.

Swing gui building is all about Panels inside of other Panels and all the layouts are set to GridBagLayout.
Last edited by hooknc; Jul 26th, 2006 at 2:10 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 134
Reputation: ryy705 is an unknown quantity at this point 
Solved Threads: 0
ryy705 ryy705 is offline Offline
Junior Poster

Re: JTextArea question

 
0
  #6
Jul 26th, 2006
Thank you for your prompt responses. I changed the the gridbagconstraints to .BOTH. You will see the result in the attached pics. Is there a way to make the objects retain there shapes?

Why do you want the objects to span only one cell? What are the benefits?

Do proffesional programmers write the code for java gui? Or do they use some kind of software to drag and drop objects on to the frame?
Last edited by ryy705; Jul 26th, 2006 at 12:16 pm.
Attached Thumbnails
before.gif   after.gif  
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 216
Reputation: hooknc is an unknown quantity at this point 
Solved Threads: 8
hooknc hooknc is offline Offline
Posting Whiz in Training

Re: JTextArea question

 
0
  #7
Jul 26th, 2006
Well, my guess is what is happening is that not only is fill set to both. But weighty is set to 1 for all the components. You really only want weighty to be set to 1 for your textarea.

BTW i would recommend getting rid of the addComponent method that was written and set all GridBagConstraints rules where you create the component and then add the component to your panel. It makes it very clear at what you're doing for each component.

What are the benefits of spanning multiple cells?

Spanning multiple cells can cause many headaches down the road. For example, lets say you need to add another button to the bottom of the page. Now the textarea is going to have to span 4 grids instead of 3. So now you have to make 2 code changes. If the top components were in their own panel and the buttons were in their own panel, you would only need to make one code change. What if you want to have dynamically build panels? That can really cause a mess with having to calculate what is where and how many grids a component spans, etc...

I would consider myself a professional Swing developer and I've used both GUI development tools and written entire GUIs by hand. Just like with WYSIWYG html editors.... Hand written code is FAR superior to generated code.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC