Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Adding JTextArea to JScrollPane and JPanel issue's  (Read 615 times)
0 Members and 1 Guest are viewing this topic.
Offline cloudwindgate

Senior Newbie





« Posted 2013-08-27 01:08:04 »

So when I don't add the text area to the scrollpane the update method works, however if I do all I get is a blank screen. The other problem is if I add any of this to a JPanl mainArea one below it becomes super doper tiny.

Any Help is appreciated thank-you.


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
public class Gui {
    JFrame frame;
    JPanel mainArea;
    JPanel choiceArea;
    JScrollPane scrollBar;
    JPanel charArea;//for later use
    JPanel mapArea;
    JTextArea textArea;
   
   
    public Gui(){
       
        frame = new JFrame("Escape");
        frame.setSize(700, 700);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
       
        mainArea = new JPanel();
        mainArea.setMaximumSize(new Dimension(680, 400));
        mainArea.setSize(680, 400);
        //mainArea.setPreferredSize(new Dimension(680, 400));
       
        choiceArea = new JPanel();
        choiceArea.setSize(700, 150);
       
        textArea = new JTextArea(35, 25);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setEditable(false);
        //textArea.setSize(680, 400);
        //textArea.setRows(35);
       
        scrollBar = new JScrollPane();
        scrollBar.setVerticalScrollBarPolicy(scrollBar.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollBar.setHorizontalScrollBarPolicy(scrollBar.HORIZONTAL_SCROLLBAR_NEVER);
        scrollBar.add(textArea);
       
        choiceArea.setLayout(new FlowLayout());
        //mainArea.add(scrollBar);
       
       
       
       
        frame.getContentPane().add(BorderLayout.CENTER, textArea);
        frame.getContentPane().add(BorderLayout.SOUTH, choiceArea);
       
       
        frame.validate();
        frame.setVisible(true);
       
    }
   
    public void addTextToTextArea(String s){
        textArea.append("\n" + s);
        textArea.repaint();
        textArea.revalidate();
        frame.repaint();
        frame.revalidate();
    }
Offline philfrei
« Reply #1 - Posted 2013-08-27 03:01:57 »

To debug this sort of thing, I set all the components with a different background color. That makes it pretty unambiguous what is where. The screen won't be blank!

At the moment your code only displays two components. I noticed if I change the number of rows, I don't get a change in the vertical size of textArea when it is in the CENTER. However, changes to the row size does affect the relative dims when you swap SOUTH and CENTER in the current code. I'm not sure what that means though. Maybe there are rules as to "who wins" when two areas are contending for a space, rather than the frame expanding to hold both.

I'm also noticing that when I use setPreferredSize(new Dimension(750, 150)) on choiceArea, it can go in either CENTER or SOUTH and isn't totally stomped on by textArea. I think setPreferredSize should be used rather than setSize.

Another tool to help understand what is going on: alternately use frame.pack() instead of frame.validate() at the end of the constructor, for debugging purposes. It will reduce the frame down to the minimum size based on the dimensions of the components, so that makes it easier to tell what is going on with the components before they get whacked by the BorderLayout logic.

It might take a bit to work out all the dimensions and rules as to "who wins", so I'd recommend going piece by piece, testing with each added component.

For ScrollPane, I usually put the component to be held by the ScrollPane in the constructor as a parameter. I don't know that it makes any difference though, compared to adding it later.

Hope some of this helps!

"It's after the end of the world! Don't you know that yet?"
Offline cloudwindgate

Senior Newbie





« Reply #2 - Posted 2013-08-28 04:19:34 »

Thank-you for your reply I'll look into your suggestions.  Buttons are created in a different class which then calls a method to add them to the choice area.

The thing about packing is it would be changing multiple times due to the adding and subtracting of components.

again thanks I'll look into those suggestions. Like I said if I don't add the text area to the panel or the scrollpane everything works just fine -_-
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (32 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (58 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!