# Solved: Java Questions



## doomlord289 (Nov 2, 2005)

I have a few questions about creating a GUI-based app in Java.

How can I make the initial frame display as a maximized window?
How do I make a menu item open a new dialog box?
How do I package my app for distribution over the internet?
How do I get input from a text box and button?
How do I close a dialog box once the user clicks a button?
I don't have any code for this project other than the menu bar and JFrame code. I am also pretty new to GUI app development, but I can understand most code that comes my way.

I am using JCreator LE 2.50 as my IDE in case anyone needs to know.
Thank you all for any and all help that you can provide. :up:


----------



## Chicon (Jul 29, 2004)

1°

```
[SIZE=2]JFrame frame = new JFrame();
   frame.setSize(Toolkit.getDefaultToolkit().getScreenSize());[/SIZE]
```
2° Difficult to give you a precise answer. You probably need to learn about Java Swing and AWT components, their respective events and the way to 'listen' to them.

3° A Java applet is invoked in a HTML page by a tag .
Example :

```
[SIZE=2]
<HTML>
    <HEAD>
        <TITLE>HelloWorld Applet</TITLE>
    </HEAD>
    <BODY>
        <H1 Align="center">HelloWorldApplet</H1>  <BR>

        <DIV Align="center">
            <APPLET Code="HelloWorldApplet.class" Width=300 Height=50>Test</APPLET>
        </DIV>
    </BODY>
</HTML>[/SIZE]
```
Java applets are stored like other files (html, css, pictures and other medias ...) on the web server.

4° idem them 2°

5°

```
[SIZE=2]
   JFrame myDialogBox = new JFrame();
   .
   .
   .
   myDialogBox.dispose();[/SIZE]
```


----------



## doomlord289 (Nov 2, 2005)

1. It looks like all that code will do is make the JFrame the size of the user's screen. I want to have it as a maximized window, not just as big as the screen.
2. I'll look into that.
3. Is there any way to make an exe or executable jar file out of the resulting classes? I want to have an offline executable file, not an applet.
4. I'll look into that.
5. Looks like it should work. Thank you.


----------



## Chicon (Jul 29, 2004)

1°

```
[SIZE=2]
public class MainJFrame extends javax.swing.JFrame {
    
    /** Creates new form MainJFrame */
    public MainJFrame() {
        ...
    // The following instruction won't allow the user to reduce the window    
        this.setResizable(false);
    [COLOR=Red]// The following instruction sets the window to its maximized size
        this.setExtendedState(this.MAXIMIZED_BOTH);[/COLOR]
    // The following instruction sets the title of the window
        this.setTitle("This is a test");

    }
[/SIZE]
```
3° What do you need to run an executable JAR file ?

a) obviously, the appropriate or superior version of the Java runtime (JRE) must be installed on the computer
b) Manifest : it's possible the application needs other libraries, native or third-party.
In my above example of coding, the application will need the appropriate Swing library.
An executable JAR file contents a special file called *manifest.mf*

Example of content of the *manifest.mf* :

```
[SIZE=2]
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.5.0_09-b03 (Sun Microsystems Inc.)
[COLOR=Blue]Main-Class: test.MainJFrame[/COLOR]
[COLOR=Red]Class-Path: lib/swing-layout-1.0.jar[/COLOR]
X-COMMENT: Main-Class will be added automatically by build[/SIZE]
```
The blue line shows the main class of the JAR file that will be executed.
The red line shows the third-party library that will be used by the application; it is the library that contents the Swing classes and objects.
It is stored in a folder called *lib* which is stored itself at the same level of the executable JAR.

5° You're welcome !


----------



## doomlord289 (Nov 2, 2005)

1. Thank you
3. My IDE (JCreator 2.50 LE) has a tool that will create a jar file for me, but it doesn't seem to generate the manifest file correctly. It is missing the Main-Class item. How do I add this to the manifest?I 've tried editing the manifest file in notepad and adding the line manually, but it doesn't work.


----------



## Chicon (Jul 29, 2004)

3. There's an answer on this following page.
Personally, I don't use JCreator, I'm using NetBeans 5.5.


----------



## doomlord289 (Nov 2, 2005)

Thanks for the link, the JarMaker program referenced there works great!

I do have some more questions though:
1. How tall, in pixels, is a menu bar? I've estimated it at about 57 pixels, but I need an exact number if someone happens to know it.
2. Does anyone know where I can find some Java code for definite and indefinite integrals, derivatives, limits, etc. or if this is even possible in Java?
3. How do I set a menu item shortcut key to one of the F keys? (Help = F1, About = F12)

I've decided not to do a full screen app, which I was originally planning. This is going to be a menu bar that, when an item is clicked, it calls a dialog box with input boxes. After the user clicks the button in the dialog box, my program will do calculations, and display a new (or modified) dialog box with the answer from that calculation. In other words, it is a PC port of my TI-89 program Formula Suite, found at my site. Please keep in mind my site is undergoing MAJOR renovation. I designed it when I first began web design, and now that I'm more experienced, I'm working on a complete rewrite of the site.


----------



## Chicon (Jul 29, 2004)

1. The sizes of Java Swing components are variable, they can be set either to absolute values or to relative values.
Generally, relative values are used because it makes the GUI more comfortable for its users. In this case, layouts are necessary to implement components on a frame.
Here is a chapter about layouts.
Also, while building a new frame, it is always a good idea to get the user's screensize in the first place.

2. I can't answer this one, I'm having a look.

3. It is possible to use the function keys of a keyboard : you have to implement an action listener which event names are KEY_ACTION and KEY_ACTION_RELEASE.
Here is the page that gives the description of the java.awt.Event class.


----------



## doomlord289 (Nov 2, 2005)

1. Thank you. I've set my menu bar to the same size as my JFrame. I also get the user's screen size, because I plan to center the dialog boxes generated by the menu items.
2. Thanks for looking.
3. I don't see how this works. The book I'm working out of is telling me I need the following to create a menu shortcut (the comments represent what should go there):

```
component.setAccelerator(KeyStroke.getKeyStroke('/*character*/');
```
or

```
component.setAccelerator(KeyStroke.getKeyStroke('/*character*/', /*modifier key*/);
```
I'm trying to put F1 and F12 in at the character location, but that doesn't seem to work.

```
helphelp.setAccelerator(KeyStroke.getKeyStroke(java.awt.Event.F1));
helpabout.setAccelerator(KeyStroke.getKeyStroke(java.awt.Event.F12));
```
My compiler spits this out:


> cannot find symbol
> symbol : method getKeyStroke(int)
> location: class javax.swing.KeyStroke
> helphelp.setAccelerator(KeyStroke.getKeyStroke(java.awt.Event.F1));
> ...


Could provide some revised code? It would be very appreciated. Thank you.


----------



## Chicon (Jul 29, 2004)

I'm posting what I have about keyboard actions in my documentations as soon as possible. For the moment, I must leave.


----------



## Chicon (Jul 29, 2004)

Here is an example of how to use a *KeyListener* (see the red colored part of coding) in a JFrame.
To make it fast, I used the inner built Java Swing code generator of my IDE.


```
[SIZE=2]public class TestFrame extends javax.swing.JFrame {
    private javax.swing.JLabel jLabel1;

    public TestFrame() {
        initComponents();
    }

    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        [COLOR=Red]addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });[/Color]

        org.jdesktop.layout.GroupLayout layout = 
                      new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(26, 26, 26)
                .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 344,
                                   org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(30, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(123, 123, 123)
                .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37,
                                   org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(140, Short.MAX_VALUE))
        );
        pack();
    }

    [COLOR=Red]private void formKeyPressed(java.awt.event.KeyEvent evt) {
        int keyCode = evt.getKeyCode();
        String val = evt.getKeyText(keyCode);
        if (evt.isActionKey()) {
            jLabel1.setText("Action key : " + val + " - code = " + keyCode);
        }
        else {
            jLabel1.setText("Other key : " + val + " - code = " + keyCode);
        }
    }[/Color]

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestFrame().setVisible(true);
            }
        });
    }
}[/SIZE]
```


----------



## doomlord289 (Nov 2, 2005)

Wierd, when I tried to compile your code i got several (8 to be exact) errors, all saying "package org.jdesktop.layout does not exist". Is there any more code that I should have or is something, like JCreator, just being stupid.


----------



## doomlord289 (Nov 2, 2005)

Thank you for recommending NetBeans to me. I love how it generates forms. I still plan to use JCreator to code and compile and such, but NetBeans's form generator will help me get some of the harder-to-code things done much quicker.
As for the function keys, the code is as follows:

```
helpMenu_help.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F12,0));
```
and

```
helpMenu_about.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F12,0));
```
This code was generated by NetBeans.

I must thank you Chicon for all your help. I really appreciate it.

I'm still looking for those calculus classes. If I find them I'll post a link in this thread for anyone who is interested.


----------



## Chicon (Jul 29, 2004)

You're welcome ! 

I've browsed many math sites : I just got a lot of miscellaneous applets.


----------

