newbie: GUI help

V

Veeral Patel

G'dday,

I am just a beginner in Java and have stumbled across a couple of
problems in writing a GUI applet. I have included the applet below at
the end of this message. My problem is that I have created a input
field which at the moment the USER simply types in a numerical value
and then presses the ENTER key (on keyboard) to produce the result. So
far it works. I decided to play with the JButton feature. Now I have
no idea how to get the JButton feature to work. At the moment I have
commented out the JBUtton lines. Can anyone tell me how to go about
configuring this JButton feature. I dont know how to pass the input
variable entered in the FIELD box to the append function when the USER
presses the ENTER Button on the applet.

My other question is how to fix the GUI components in one place. At
the moment if I enter a larger value say 3333333 the GUI components
shift. Is there a way to permanently fix the GUI components in one
location? Or do I have to use a different GUI component manager ?

Any feedback will be highly appreciated
thanks
Veeral

################################################################################

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.math.*;

public class JFloor2 extends JApplet implements ActionListener
{
JLabel inputLabel;
JTextField inputField;
JTextArea outputArea;
//JButton PushButton; //comment this line out for no button support
public void init()
{
Container container = getContentPane(); //setting GUI
container.setLayout(new FlowLayout() ); //setting GUI

inputLabel = new JLabel("Enter input value");
container.add(inputLabel);

inputField = new JTextField(5); //creating field
inputField.addActionListener(this); //uncomment this line when
button feature is disabled
container.add(inputField);

outputArea = new JTextArea();
container.add(outputArea);

//PushButton = new JButton("ENTER"); //comment this line out
for no button support
//PushButton.addActionListener(this); //comment this line out
for no button support
//container.add(PushButton); //comment this line out for no
button support
}

public void actionPerformed(ActionEvent actionEvent)
{
double input = Double.parseDouble
(actionEvent.getActionCommand()); //this only works when user
directly presses ENTER key in the FIELD BOX.
outputArea.append("\n Input Value is " + input + " \n Non
Floored Value is " + (input + 0.5) + " \n Floored Value is "
+ (int)(Math.floor(input + 0.5)) + "\n");
}


}
 
A

Andrew Thompson

.....
I am just a beginner in Java and have stumbled across a couple of
problems in writing a GUI applet.

Hi Veeral, initially I ignored your post hoping someone else
would answer you, but you have posted so well (providing a
small self contained compileable example, clear problem
statement etc.) that I'm now compelled to answer..

1st up. It seems you've jumped in at the deep end,
both swing and applets are something I'd only recommend
attacking after you've mastered the AWT, layouts,
and applications.

2nd up. This question may have got a better response to
the GUI problem by putting it to comp.lang.java.gui,
you might also check that group for more specific GUI
questions..

3rd. Try to make your (posted to newsgroups) program lines
short (especially the comments), as they tend to wrap in
news readers and do not compile cleanly..

Now, to your question. There are various ways to
achieve what you want, both in the layout and how
to handle the button.

The (single) answer I offer leaves the text field active
('enter' is still effective), but adds the button as well.
Either work.

The layout I chose is a 'nested' layout with a
border layout as the main layout, with a flow
layout in the north to contain the controls.

Further comments are in the code below
___________________________________________
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.math.*;

public class JFloor2 extends JApplet implements ActionListener
{
JLabel inputLabel;
JTextField inputField;
JTextArea outputArea;
// you have used the accepted naming conventions
// for the other three but here you suddenly
// went to PushButton rather than pushButton
JButton pushButton; //comment this line out for no button support
public void init()
{
Container container = getContentPane(); //setting GUI

// I create this to contain the top components
// and give the topPanel a flow layout
JPanel topPanel = new JPanel( new FlowLayout() );

// but make the content pane a border layout
container.setLayout(new BorderLayout() ); //setting GUI

inputLabel = new JLabel("Enter input value");
// we need to add the label, text field and button
// to the topPanel
topPanel.add(inputLabel);

inputField = new JTextField(5); //creating field
inputField.addActionListener(this); //uncomment this line when
button feature is disabled
// same here
topPanel.add(inputField);

outputArea = new JTextArea();
// the text area (in the center position of a border layout)
// gets all the remaining space..
// it is added directly to the content pane
container.add(outputArea, BorderLayout.CENTER);

pushButton = new JButton("ENTER"); //comment this line out for no
button support
pushButton.addActionListener(this); //comment this line out for no
button support
// changed to the topPanel
topPanel.add(pushButton); //comment this line out for no button
support

// now we need to add the topPanel to the
// content panes's NORTH area..
container.add( topPanel, BorderLayout.NORTH );

// VERY important
validate();
}

public void actionPerformed(ActionEvent actionEvent)
{
double input = Double.parseDouble
// (actionEvent.getActionCommand()); //this only works when user
directly presses ENTER key in the FIELD BOX.
// this works if _all_ actions (enter and button click)
// do the same thing..
( inputField.getText() );
// using append here will cause your results to scroll
// out of site unless you drop the text area inside
// a scroll pane..
outputArea.append("\n Input Value is " + input + " \n Non" +
"Floored Value is " + (input + 0.5) + " \n Floored Value is " +
+ (int)(Math.floor(input + 0.5)) + "\n");
}


}
___________________________________________

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top