Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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  
  Creating an text area  (Read 1564 times)
0 Members and 1 Guest are viewing this topic.
Offline hotmail590

Junior Newbie





« Posted 2005-07-22 23:56:31 »

I would like to create a simple window that has a text area
where people cannot edit text area, and there should be a scroll bar for it also.
On the top right corner above the text area should be a button that says "Print Out Config"

the result is just a simple widow that has a blank text area with a scroll bar next  to it, a button somewhere that says Print Out Config.

I have tried the following, but nothing shows up when I run the program.

so far this is what I have came up with

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  
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.BorderLayout;

public class ConfigTester {
   
   private static final int FRAME_WIDTH = 800;
   private static final int FRAME_HEIGHT = 600;
   

   
   public static void main(String[] args) {
     
      JFrame myFrame = new JFrame("Config Editor 1.0");
     
     
      myFrame.setBounds(0,0,FRAME_WIDTH,FRAME_HEIGHT);
      myFrame.setVisible(true);
     
      myFrame.addWindowListener( new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}   });
     
      final int AREA_ROWS = 10;
      final int AREA_COLUMNS= 30;
     
     
      final JTextArea resultsArea = new JTextArea(AREA_ROWS, AREA_COLUMNS);
      JScrollPane scrollPane = new JScrollPane(resultsArea);
     
      resultsArea.setEditable(false);
     

     
      JButton printAllButton = new JButton("Print Out Config");
     
      JPanel northPanel = new JPanel();
      northPanel.add(printAllButton);
     
      myFrame.add(northPanel, BorderLayout.NORTH);
      myFrame.add(scrollPane);
     
     
     
     
   }
}
kul_th_las
Guest
« Reply #1 - Posted 2005-07-23 00:52:32 »

1) Layout containers, add listeners, and add components /before/ you make the frame visible. Any changes you make to the layout of a Swing component while it is visible do not get made unless the container is laid out agin. That is, resized, or hidden and shown again.
2) When you add things to a JFrame, you should add them to the content pane, not the JFrame itself. For example:

You have:
myFrame.add(northPanel, BorderLayout.NORTH);

Should (probably) be:
myFrame.getContentPane().add(northPanel, BorderLayout.NORTH);

3) For cleanliness reasons, consider explicitly setting the layout of your containers before adding anything to them.

4) Don't set the bounds of your frame - set the bounds of the components instead. In this case, the ScrollPane.

5) Before you make the frame visible, make the following call on your frame (check it out in the Java Docs):

myFrame.pack();

6) Don't use JFrame.setVisible(boolean visible) to make it visible. Use JFrame.show() and JFrame.hide(). These methods are actually members of the java.awt.Window class, but JFrame gets this functionality by extension.

7) Which version of the JDK are you coding in?


Hope that helps.
Offline hotmail590

Junior Newbie





« Reply #2 - Posted 2005-07-23 01:14:01 »

Oh I am using JDK 1.5 right now and eclipse to write my programs.

Thank you for all your help!! I will try to fix my program right now.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #3 - Posted 2005-07-23 02:08:33 »

1  
2) When you add things to a JFrame, you should add them to the content pane, not the JFrame itself. For example:


I think that in 1.5 the add method of JFrame adds directly to the content pane (new "feature"). Could be my memory acting up though.

EDIT: This is definitely the case:
Quote
The JFrame class is slightly incompatible with Frame. Like all other JFC/Swing top-level containers, a JFrame contains a JRootPane as its only child. The content pane provided by the root pane should, as a rule, contain all the non-menu components displayed by the JFrame. This is different from the AWT Frame case. As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write:

       frame.add(child);
 
kul_th_las
Guest
« Reply #4 - Posted 2005-07-23 02:38:22 »

Ah - adds to the content pane for you - that's a welcome change.
Offline hotmail590

Junior Newbie





« Reply #5 - Posted 2005-07-23 02:47:19 »

I have revised my .java and decided to only learn how to make text fields first.  Right now I am having trouble trying to set the size and the position of my textbox.  I would like it to be only half the size of the window positioned and the bottom however, right now the textbox seems to take up the whole window.

Here is my .java


Thank you very much for your help!!

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  
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.Color;


public class ConfigTester {
   
   private static final int FRAME_WIDTH = 800;
   private static final int FRAME_HEIGHT = 600;
   

   
   public static void main(String[] args) {
     
      Color frameBackgroundColour = new Color(76,88,68);
     
     
      JFrame myFrame = new JFrame("Config Editor 1.0");
      myFrame.setBounds(0,0,FRAME_WIDTH,FRAME_HEIGHT);
      myFrame.setBackground(frameBackgroundColour);
      myFrame.addWindowListener( new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}   });
     
     
     
      final int AREA_ROWS = 10;
      final int AREA_COLUMNS= 30;
      final JTextArea resultsArea = new JTextArea();
      resultsArea.setEditable(false);
     
     
     
      JScrollPane scrollPane = new JScrollPane(resultsArea);
     
      scrollPane.setBounds(0, 0, AREA_ROWS, AREA_COLUMNS);
      scrollPane.setBackground(frameBackgroundColour);
     

     

     
     
      myFrame.getContentPane().add(scrollPane);
     
     
     
      myFrame.setVisible(true);
     
     
   }

}
Offline Jeff

JGO Coder




Got any cats?


« Reply #6 - Posted 2005-07-23 05:56:40 »

Maybe Im just blind but I dont see a "setLayout()" anywhere.

You really want to set a layout manager on the content pane. I recommend BorderLayout as the easiest one to learn to use.

Otherwise you need to manually set the actual pixel location adn size of each compoenent AND it will not chnage if you resize the window.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline hotmail590

Junior Newbie





« Reply #7 - Posted 2005-07-23 06:40:31 »

Right now my problem is that the textbox area takes up the whole window Sad Sad how would i be able to make it half its size and have it positioned on the bottom?  Also i would like to fix the window so it cannot be resized by the user.

Thanks for your help!
Offline Jackal von ÖRF

Junior Member





« Reply #8 - Posted 2005-07-23 16:38:34 »

6) Don't use JFrame.setVisible(boolean visible) to make it visible. Use JFrame.show() and JFrame.hide(). These methods are actually members of the java.awt.Window class, but JFrame gets this functionality by extension.
Actually they are part of java.awt.Component and setVisible(boolean) is the right method to use. show() and hide() have been deprecated as of JDK 1.1.
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Component.html#setVisible(boolean)

Offline c_lilian

Senior Member


Projects: 1


Java games will probably rock someday...


« Reply #9 - Posted 2005-07-23 17:17:41 »

Right now my problem is that the textbox area takes up the whole window Sad Sad how would i be able to make it half its size and have it positioned on the bottom?  Also i would like to fix the window so it cannot be resized by the user.


You should read the java tutorial before continuing, it will make more sense... : http://java.sun.com/docs/books/tutorial/uiswing/index.html
Than, go and search for the Jgoodies forms layout, which is the best (IMHO) layout out there, if you don't loke it, then you'll have to use the hard-to-learn GridBagLayout.

Until then, I suggest you do the following :
frame.setLayout(new GridLayout(2,1); // 2 rows, 1 column
frame.add(Box.createGlue()); // an invisible component on top (first row)
frame.add(scrollPane); // your scrollpane under it

warning : don't rely on gridlayout as all cells end up with the same size. But in that case, that's what you want to acheive. Usually you only use that layout to have a row of buttons of the same size.


Oh and about your other problem : frame.setResizeable(false);

Lilian


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeff

JGO Coder




Got any cats?


« Reply #10 - Posted 2005-07-25 04:45:25 »

Right now my problem is that the textbox area takes up the whole window Sad Sad how would i be able to make it half its size and have it positioned on the bottom?  Also i would like to fix the window so it cannot be resized by the user.


You should read the java tutorial before continuing, it will make more sense... :

+1.  Excellent advice.

You could also go to the Swing specific section of the Java Desktop community here on java.net.

JK

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
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.

xsi3rr4x (53 views)
2014-04-15 18:08:23

BurntPizza (51 views)
2014-04-15 03:46:01

UprightPath (65 views)
2014-04-14 17:39:50

UprightPath (47 views)
2014-04-14 17:35:47

Porlus (65 views)
2014-04-14 15:48:38

tom_mai78101 (89 views)
2014-04-10 04:04:31

BurntPizza (149 views)
2014-04-08 23:06:04

tom_mai78101 (245 views)
2014-04-05 13:34:39

trollwarrior1 (203 views)
2014-04-04 12:06:45

CJLetsGame (210 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!