Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Layout Manager pain

Hi people...i'm new to java and at the moment i'm using the .awt layout managers for an applet project i'm doing. However i've tryed everything i know to get a specific display and it's becoming very annoying to do.
The display i'm trying to acheive is a keypad at the bottom of the applet (from 0 to 9, and a to z) and two text fields above the keypad, one at the top(labelled - input), and one below( labeled - result). any help will be appreciated. (ps. nevermind about the calculations as i havent finished that yet - its just the display i cant do) thanks in advance

the code i've got so far is:
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;

public class RecogniseNumber3 extends java.applet.Applet
implements ActionListener
{
final int IDLE = 0;
final int BUILDING_REAL = 1;
final int BUILDING_INT = 2;
final int INVALID = 3;
int state;//current user holds state
Panel ip;//contains user buttons
TextField input;//displays current number
TextField result;//displays result of number check
StringBuffer currentinput;//input characters stored here
char currentchar;




public void init()
{


setLayout(new BorderLayout());
input = new TextField(24);
input.setEditable(false);//read only
add("North", input);
result = new TextField(24);
result.setEditable(false);//read only
add("Center", result);
ip = new Panel();
ip.setLayout(new GridLayout(4,3,5,5));
Button b;
for (char input='a'; input < 'z' + 1; input++)
{
b = new Button ("" + input);
b.addActionListener(this);
ip.add(b);
}//end for
for (char input='0'; input < '9' + 1; input++ )
{
b = new Button ("" + input);
b.addActionListener(this);
ip.add(b);
}//end for
b = new Button(".");
b.addActionListener(this);
ip.add(b);

b = new Button("Finish");
b.addActionListener(this);
ip.add(b);

add("South", ip);
currentinput = new StringBuffer();
state = IDLE;//initial state
}//end init

public void handleChar()
{
switch(state)
{
case IDLE:
result.setText("");//clear result of any display
input.setText("");//clear any previous display
if (currentchar == '.')//decimal point so must be real
{
state = BUILDING_REAL;
currentinput.append(currentchar);
//add to the input string

input.setText(currentinput.toString());
//refresh output window

break;
}
else//must be a digit
{
state = BUILDING_INT;
currentinput.append(currentchar);
input.setText(currentinput.toString());
break;
}
case BUILDING_REAL:
if (currentchar == '.')//cant have two
{
state = INVALID;
currentinput.append(currentchar);
input.setText(currentinput.toString());
break;
}
else
{
currentinput.append(currentchar);
input.setText(currentinput.toString());
break;
}
case BUILDING_INT:
if (currentchar == '.')
{
state = BUILDING_REAL;
currentinput.append(currentchar);
input.setText(currentinput.toString());
break;
}
else
{
currentinput.append(currentchar);
input.setText(currentinput.toString());
break;
}
case INVALID:
currentinput.append(currentchar);
input.setText(currentinput.toString());
break;
}//end switch
}//end handleChar



public void finish()
{
switch(state)
{
case BUILDING_REAL:
result.setText("Valid");
state = IDLE;
currentinput = new StringBuffer();
//discard old string bufffer
break;

case BUILDING_INT:
result.setText("The number is an integer");
state = IDLE;
currentinput = new StringBuffer();
break;

case INVALID:
result.setText("Invalid");
state = IDLE;
currentinput = new StringBuffer();
}//end switch
}//end finish

public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if (event.getSource() instanceof Button)
{
if (command.equals("Finish"))
{
finish();
}
else
{
currentchar = command.charAt(0);
handleChar();
}
}
}
}
[6374 byte] By [cascade] at [2007-11-11 10:19:25]
# 1 Re: Layout Manager pain
This takes quite some panels (if you don't want to use the dreaded GridBagLayout), so here is what I would do:

Numeric buttons panel:
BorderLayout, with a gridlayout center panel, 3 rows, 3 columns for 1->9 buttons. A single (wide) 0-button in south.

Char buttons panel:
GridLayout, use the numer of rows and columns you find best for height and width.

Labels and text fields panel:
GridLayout, 2 rows 2 columns. Use column 1 for labels, column 2 for textfields.

Put these three panels into a main panel (the applet ?) using a BorderLayout. Put the numeric buttons panel in west or east and the char buttons panel in center.
Put the labels and text fields panel in north.

This will make a keypad with numeric buttons at least twice the size of the char buttons, but that should not be a problem... ?

NOTE: you could also use a null layout for the whole shebang, and for the buttons, textfields and labels set the exact size and position using the setBounds(x,y,w,h) method for each one before you add them to the applet. That will take some nitty gritty pixel fiddling though but would only require one panel and you would have full control over the looks of things.
sjalle at 2007-11-11 22:31:43 >
# 2 Re: Layout Manager pain
thanks for the reply mate...i'll give it a go 8-D
cascade at 2007-11-11 22:32:43 >