aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Layout Problem, Help Me Plz Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Layout Problem, Help Me Plz" Watch "Layout Problem, Help Me Plz" New topic
Author

Layout Problem, Help Me Plz

ch praveen
Ranch Hand

Joined: Mar 08, 2004
Posts: 67
Hello Friends,

For my chat application, Iam using the following components.

[1] custom multicolor textarea component.
[2] java.awt.List to display users in chatroom.
[3] some java.awt.Button components.


My layout is such that, custom textarea(say chatarea) willbe at topleft [row 0 , col 0] , followedby userlist [row 0 , col 1]. Panel containing some buttons is displayed at bottom [row 1 , with col span 2].

Now my problem is when I maximize the browser containing the chat applet and restore it, the components at row 0 are stretching vertically in such a manner so that I couldn't see the components that are in row 1. i.e height of components in row 0 are increasing and that of components in row 1 are shrinking and almost becoming invisible. Any please help me to get out of this problem as it's paining me a lot.

Layout pattern is listed as follows::

------------
private void buildLayout() {
Dimension size = getSize();

setBackground(new Color(208,208,236));
setBackground(new Color(0,0,0));
GridBagLayout gbl = new GridBagLayout();
setLayout(gbl);
GridBagConstraints c = new GridBagConstraints();

// include chatarea [custom multicolor textarea like component that willbe included at row 0 col 0]
// Even here width and height are passed as 100, it willbe based on GridBagLayout.
chatarea = new g(100,100,new Font("SansSerif", Font.BOLD , 12));
chatarea.a(this);
chatarea.setBackground(new Color(255,255,255));

Panel westpanel =new Panel();
westpanel.setLayout(new BorderLayout());
westpanel.add("Center",chatarea);

c.gridx = 0;
c.gridy = 0;
c.fill = 1;
c.weightx = 8.5;
c.weighty = 8;
gbl.setConstraints(westpanel,c);
add(westpanel);

// include users list which willbe included at row 0 col 1
userslist = new java.awt.List(20,false);
userslist.setFont(new Font("SansSerif", Font.BOLD , 12));
userslist.setForeground(Color.gray);

userslistlbl = new Label("Chatters In The Room",Label.CENTER);
userslistlbl.setFont(new Font("SansSerif", Font.BOLD | Font.ITALIC , 12));
userslistlbl.setForeground(Color.gray);
userslistlbl.setBackground(Color.white);

Panel eastpanel = new Panel(); // This is the panel which holds userslist and it's caption.
eastpanel.setBackground(Color.white);
eastpanel.setBackground(Color.white);

GridBagLayout eastpanelgbl = new GridBagLayout();
eastpanel.setLayout(eastpanelgbl);
c.gridx = 0;
c.gridy = 0;
eastpanelgbl.setConstraints(userslistlbl,c);
eastpanel.add(userslistlbl);
c.gridx = 0;
c.gridy = 1;
c.fill = 1;
eastpanelgbl.setConstraints(userslist,c);
eastpanel.add(userslist);

c.gridx = 1;
c.gridy = 0;
c.weightx = 1.5;
c.weighty = 8;
c.fill = 1;
gbl.setConstraints(eastpanel,c);
add(eastpanel);


//workaround with other components that will be displayed at bottom [row 1 with col span 2]

input = new TextField(32);
input.setEditable(true);
input.setBackground(new Color(255,255,255));

sendbutton = new Button("Send All");
privatebutton = new Button("Send Private");
emoticansbutton = new Button("Emotions");
settingsbutton = new Button("Settings");
exitbutton = new Button("Logout");


sendbutton.setBackground(new Color(255,255,255));
sendbutton.setForeground(new Color(0,90,142));

privatebutton.setBackground(new Color(255,255,255));
privatebutton.setForeground(new Color(0,90,142));

emoticansbutton.setBackground(new Color(255,255,255));
emoticansbutton.setForeground(new Color(0,90,142));

exitbutton.setBackground(new Color(255,255,255));
exitbutton.setForeground(new Color(0,90,142));

settingsbutton.setBackground(new Color(255,255,255));
settingsbutton.setForeground(new Color(0,90,142));

Panel southpanel = new Panel(); // This is panel that will be included in row 1 with col span 2
southpanel.setLayout(gbl);
southpanel.setBackground(new Color(208,208,236));
southpanel.add(input);
southpanel.add(sendbutton);
southpanel.add(privatebutton);
southpanel.add(emoticansbutton);
southpanel.add(settingsbutton);
southpanel.add(exitbutton);

// adhere some components with southpanel.
c.gridx = 0; // Layout Constraints For input wrt. southpanel.
c.gridy = 0;
c.weightx = 5;
gbl.setConstraints(input,c);

c.gridx = 1; // Layout Constraints For sendbutton wrt. southpanel.
c.gridy = 0;
c.weightx = 1;
gbl.setConstraints(sendbutton,c);

c.gridx = 2; // Layout Constraints For privatebutton wrt. southpanel
c.gridy = 0;
c.weightx = 1;
gbl.setConstraints(privatebutton,c);

c.gridx = 3; // Layout Constraints For emoticansbutton wrt. southpanel
c.gridy = 0;
c.weightx = 1;
gbl.setConstraints(emoticansbutton,c);

c.gridx = 4; // Layout Constraints For settingsbutton wrt. southpanel
c.gridy = 0;
c.weightx = 1;
gbl.setConstraints(settingsbutton,c);

c.gridx = 5; // Layout Constraints For exitbutton wrt. southpanel
c.gridy = 0;
c.weightx = 1;
gbl.setConstraints(exitbutton,c);

c.gridx = 0; // include southpanel in row 1 with col span 2.
c.gridy = 1;
c.fill=GridBagConstraints.BOTH;
c.weightx = 10;
c.weighty = 2;
c.gridwidth=2;
gbl.setConstraints(southpanel,c);
add(southpanel);

}
------------
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Give this a go and see how it works for you. The gbc.weightx calls marked with "/****/" are the relative values to juggle to get the resizing behavior you desire with the textarea and list.
ch praveen
Ranch Hand

Joined: Mar 08, 2004
Posts: 67
Hello Craig Wood ,
Thanks for your reply. The code posted by you is also producing the same result, when c.fill is set to GridBagConstraints.BOTH for chatArea. As you know that chatArea must strech horizantally and vertically to fill the extra space rather than being aligned center, c.fill = GridBagConstraints.BOTH is compulsary for chatArea. This is producing the same result as I explained in the first post wrt. this topic[open applet window press F11 in IE and then restore it to normal position]. However things are somewhat resolved when c.fill is set to GridBagConstraints.HORIZONTAL for chatArea. But this approach still doesn't proved tobe a satisfactory aproach. I think there might be some problem in custom multicolor-textarea component which Iam posting below. Kindly examine it.

-----------------

import java.applet.Applet;
import java.awt.*;
import java.io.PrintStream;
import java.net.URL;
import java.util.*;

class k extends Canvas
{

public void setForeground(Color color)
{
super.setForeground(color);
t = color;
}

public void a(Image image)
{
x = image;
}

public synchronized Dimension getMinimumSize()
{
return size();
}

public int b()
{
return r;
}

public Dimension getPreferredSize()
{
return getMinimumSize();
}

public int c()
{
return m;
}

public void a(int i1, int j1)
{
m = j1 / r - 1;
a = i1;
s = j1;
c = new Dimension(a, s);
}

public void update(Graphics g1)
{
Object obj = null;
MediaTracker mediatracker = new MediaTracker(this);
int j1 = 0;
if(d())
{
f = createImage(size().width, size().height);
g = f.getGraphics();
g.setFont(getFont());
} else
{
g.setColor(y);
g.fillRect(0, 0, size().width, size().height);
}
if(x != null)
{
int k1 = x.getWidth(this);
int i2 = x.getHeight(this);
g.drawImage(x, (a - k1) / 2, (s - i2) / 2, this);
}
String s1 = "";
String s3 = "";
boolean flag = false;
Image image = null;
for(int k2 = d; k2 < u.size(); k2++)
{
if((k2 - d) * r >= s)
break;
String s2 = (String)u.elementAt(k2);
j1 += r;
StringTokenizer stringtokenizer = new StringTokenizer(s2, "\251", true);
int j2 = 5;
while(stringtokenizer.hasMoreTokens())
{
String s4 = stringtokenizer.nextToken();
if(v.elementAt(k2) != null)
g.setColor((Color)v.elementAt(k2));
else
g.setColor((Color)w.elementAt(k2));
if(s4.indexOf("\251") == -1)
{
g.drawString(s4, j2, j1);
j2 += k.stringWidth(s4);
} else
{
String s5 = stringtokenizer.nextToken();
try
{
URL url = new URL(b + s5.substring(0, s5.indexOf(' ')));
image = z.getImage(url);
mediatracker.addImage(image, 0);
mediatracker.waitForID(0, 5000L);
}
catch(Exception _ex) { }
int l1 = image.getWidth(z);
int i1 = j1 - image.getHeight(z);
g.drawImage(image, j2, i1, z);
j2 += l1;
g.drawString(s5.substring(s5.indexOf(' ')), j2, j1);
j2 += k.stringWidth(s5.substring(s5.indexOf(' ')));
}
}
}

g1.drawImage(f, 0, 0, this);
// If we won't include this step then animated gif file is displayed as static a image -
// [ only first frame is being considered without the below line ].
super.repaint();
}

protected boolean d()
{
Dimension dimension = size();
return f == null || dimension.width != f.getWidth(this) || dimension.height != f.getHeight(this);
}

public int a(String s1)
{
return a(s1,Color.black);
}

public int a(String s1, Color color)
{
char c1 = '\u012C';
int i1 = 0;
String as[] = new String[c1];
int j1 = 0;
Object obj = null;
MediaTracker mediatracker = new MediaTracker(this);
String s2 = "";
String s3 = "";
s1 = s1.trim() + " ";
for(int j2 = s1.indexOf("*", 1); j2 > 0; j2 = s1.indexOf("*", j2 + 2))
if(!s1.substring(j2 - 1, j2).equals(" ") && j2 < s1.length() - 1)
{
String s5 = s1.substring(j2 + 1, j2 + 2);
if(s5.compareTo("9") <= 0 && s5.compareTo("0") >= 0)
s1 = s1.substring(0, j2) + " " + s1.substring(j2);
}

int k2 = -1;
byte byte0 = -1;
k2 = s1.indexOf("_.");
if(k2 >= 0)
{
int l2 = s1.indexOf(" ", k2);
s2 = s1.substring(k2 + 2, l2);
String s4 = s1.substring(0, k2);
s4 = s4 + s1.substring(l2 + 1);
s1 = s4;
if(l2 < 0)
{
int i3 = s1.length();
s2 = s1.substring(k2 + 2, i3);
}
}
try
{
q = Integer.parseInt(s2, 10);
}
catch(Exception _ex)
{
q = -1;
}
if(q >= j.length)
q = -1;
for(int j3 = 0; j3 < c1; j3++)
as[j3] = "";

StringTokenizer stringtokenizer = new StringTokenizer(s1, " \t\n\r", true);
int k3 = 0;
int l3 = size().width - 10;
boolean flag = false;
boolean flag1 = false;
String s7 = "";
while(stringtokenizer.hasMoreElements())
{
String s8 = stringtokenizer.nextToken();
boolean flag2 = false;
if(s8.equals("\n"))
{
s8 = "";
flag2 = true;
}
if(s8.equals("\t"))
s8 = " ";
if(p.containsKey(s8.toUpperCase()))
{
try
{
String s9 = (String)p.get(s8.toUpperCase());
String s6;
if(s8.charAt(0) == '*')
s6 = "";
else
s6 = s8.toUpperCase();
URL url = new URL(b + "/icon/" + s9);
Image image = z.getImage(url);
mediatracker.addImage(image, 0);
try
{
mediatracker.waitForID(0, 5000L);
}
catch(InterruptedException _ex) { }
int k1 = image.getWidth(z);
if(k1 > 0)
{
int i2 = k.stringWidth(s6);
if(j1 + k1 + i2 >= l3 || flag2)
{
as[++k3] = " \251" + "/icon/" + s9 + " " + s6;
//j1 = 5 + k1 + k.stringWidth(" " + s6);
j1 = 5 + k1 + k.stringWidth(s6);
} else
{
as[k3] += "\251" + "/icon/" + s9 + " " + s6;
//j1 += k1 + k.stringWidth(" " + s6);
j1 += k1 + k.stringWidth(s6);
}
i1 = Math.max(image.getHeight(z), i1);
}
}
catch(Exception exception)
{
System.err.println("Problem with the image " + exception);
}
} else
{
int l1 = k.stringWidth(s8);
if(j1 + l1 >= l3 || flag2)
{
//as[++k3] = " " + s8;
as[++k3] = s8;
j1 = 5 + l1;
} else
{
as[k3] += s8;
j1 += l1;
}
}
}
boolean flag3 = false;
for(int l4 = 0; l4 <= k3; l4++)
{
if(as[l4].indexOf("\251") != -1)
{
int i4 = i1 % r;
int j4;
if(i4 == 0)
j4 = 0;
else
j4 = r - i4;
int k4 = (i1 + j4) / r;
for(int i5 = 1; i5 < k4; i5++)
{
u.insertElementAt(" ", i);
if(q > 0)
{
v.insertElementAt(j[q], i);
w.insertElementAt(null, i);
} else
{
v.insertElementAt(null, i);
w.insertElementAt(color, i);
}
if(i >= 200)
{
u.removeElementAt(0);
w.removeElementAt(0);
v.removeElementAt(0);
} else
{
i++;
}
}

}
u.insertElementAt(as[l4], i);
if(q > 0)
{
v.insertElementAt(j[q], i);
w.insertElementAt(null, i);
} else
{
v.insertElementAt(null, i);
w.insertElementAt(color, i);
}
if(i >= 200)
{
u.removeElementAt(0);
w.removeElementAt(0);
v.removeElementAt(0);
} else
{
i++;
}
}

repaint();
if(i - m < 0)
d = 0;
else
d = i - m;
return d;
}

public void setFont(Font font)
{
super.setFont(font);
k = getFontMetrics(font);
r = k.getHeight();
}

public Dimension size()
{
if(super.size().width <= 0)
{
return c;
} else
{
a(super.size().width, super.size().height);
return super.size();
}
}

public int a()
{
return 200;
}

public int a(int i1)
{
int j1 = 0;
if(i - m < 0)
j1 = 0;
else
j1 = i - m;
if(i1 >= j1)
d = j1;
else
d = i1;
repaint();
return d;
}

public void paint(Graphics g1)
{
update(g1);
}

public void a(Applet applet)
{
try
{
z = applet;
b = z.getCodeBase();
return;
}
catch(Exception exception)
{
System.err.println(exception.getMessage());
exception.printStackTrace();
return;
}
}

public k(int i1, int j1, Font font)
{
j = (new Color[] {
new Color(0,0,0), Color.blue, Color.black, Color.gray, Color.green, Color.orange, Color.red, Color.white, Color.yellow
});
i = 0;
q = -1;
y = Color.white;
t = Color.black;
x = null;
d = 0;
setFont(font);
setBackground(y);
a(i1, j1);
u = new Vector(200);
w = new Vector(200);
v = new Vector(200);
p = new Hashtable();
int k1 = 0;
do
p.put("*" + k1, "icon" + k1 + ".gif");
while(++k1 < 41);
}

public void setBackground(Color color)
{
super.setBackground(color);
y = color;
}

private final String e = "/icon/";
private URL b;
private Color t;
private Color y;
private Graphics g;
public Dimension c;
private Dimension h;
private Image x;
private Image f;
private FontMetrics k;
private int q;
private int s;
private int a;
private int r;
private int m;
private int d;
private int i;
private final int n = 5;
private final int o = 5;
private final int l = 200;
private Hashtable p;
private Vector v;
private Vector w;
private Vector u;
private Applet z;
final Color j[];
final int ab = 200;
}

-----------------
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
I made some changes to the LayoutTest app (I had a misplaced line for one thing). It uses fill [BOTH] for both the textarea and list and only fill[HORIZONTAL] with weighty [zero] for the south panel. It fills the textarea by loading the LayoutTest.java file into it and seems to resize well. Please run this as–is to observe the resize behavior; I'd much rather work with a test app than have to wade through the code for class k.
ch praveen
Ranch Hand

Joined: Mar 08, 2004
Posts: 67
Hello Craig Wood,

I thank you once again for your interest. It seems that there is no problem with the layout that I have implemented at first. Because when I implement that layout by considering java.awt.TextArea, it's working well when browser containing applet is maximized [in IE - press F11] or restored. The problem is arising when I use multicolor textarea component. I pardon you to kindly examine the multicolor textarea component code that I have posted under this topic, which may be the primary cause of concern.
ch praveen
Ranch Hand

Joined: Mar 08, 2004
Posts: 67
My initial layout with java.awt.TextArea is posted below for reference::

---------------------
import java.applet.*;
import java.awt.*;

public class LayoutTest_TextArea extends Applet
{

TextArea chatarea;
java.awt.List roomslist,userslist;
TextField input;
Button sendbutton,exitbutton,privatebutton,emoticansbutton,settingsbutton;
Label userslistlbl;

public void init()
{
buildLayout();
}

private void buildLayout() {
Dimension size = getSize();

setBackground(new Color(208,208,236));
setBackground(new Color(255,255,255));
GridBagLayout gbl = new GridBagLayout();
setLayout(gbl);
GridBagConstraints c = new GridBagConstraints();

chatarea = new TextArea("",5,40,1); // v 2
chatarea.setBackground(new Color(255,255,255));

c.gridx = 0;
c.gridy = 0;
//c.fill = GridBagConstraints.HORIZONTAL;
c.fill = 1;
c.weightx = 8.5;
c.weighty = 8;
gbl.setConstraints(chatarea,c);
add(chatarea);

userslist = new java.awt.List(20,false);
userslist.setFont(new Font("SansSerif", Font.BOLD , 12));
userslist.setForeground(Color.gray);

userslistlbl = new Label("Chatters In The Room",Label.CENTER);
userslistlbl.setFont(new Font("SansSerif", Font.BOLD | Font.ITALIC , 12));
userslistlbl.setForeground(Color.gray);
userslistlbl.setBackground(Color.white);

Panel eastpanel = new Panel(); // This is the panel which holds userslist and it's caption.
eastpanel.setBackground(Color.white);
eastpanel.setBackground(Color.white);

GridBagLayout eastpanelgbl = new GridBagLayout();
eastpanel.setLayout(eastpanelgbl);
c.gridx = 0;
c.gridy = 0;
eastpanelgbl.setConstraints(userslistlbl,c);
eastpanel.add(userslistlbl);
c.gridx = 0;
c.gridy = 1;
c.fill = 1;
eastpanelgbl.setConstraints(userslist,c);
eastpanel.add(userslist);

c.gridx = 1;
c.gridy = 0;
c.weightx = 1.5;
c.weighty = 8;
c.fill = 1;
gbl.setConstraints(eastpanel,c);
add(eastpanel);

input = new TextField(32);
input.setEditable(true);
input.setBackground(new Color(255,255,255));

sendbutton = new Button("Send All");
privatebutton = new Button("Send Private");
emoticansbutton = new Button("Emotions");
settingsbutton = new Button("Settings");
exitbutton = new Button("Logout");

sendbutton.setBackground(new Color(255,255,255));
sendbutton.setForeground(new Color(0,90,142));

privatebutton.setBackground(new Color(255,255,255));
privatebutton.setForeground(new Color(0,90,142));

emoticansbutton.setBackground(new Color(255,255,255));
emoticansbutton.setForeground(new Color(0,90,142));

exitbutton.setBackground(new Color(255,255,255));
exitbutton.setForeground(new Color(0,90,142));

settingsbutton.setBackground(new Color(255,255,255));
settingsbutton.setForeground(new Color(0,90,142));

Panel southpanel = new Panel();
southpanel.setLayout(gbl);
southpanel.setBackground(new Color(208,208,236));
southpanel.add(input);
southpanel.add(sendbutton);
southpanel.add(privatebutton);
southpanel.add(emoticansbutton);
southpanel.add(settingsbutton);
southpanel.add(exitbutton);
c.gridx = 0; // Layout Constraints For input
c.gridy = 0;
c.weightx = 5;
gbl.setConstraints(input,c);
c.gridx = 1; // Layout Constraints For sendbutton
c.gridy = 0;
c.weightx = 1;
gbl.setConstraints(sendbutton,c);
c.gridx = 2; // Layout Constraints For privatebutton
c.gridy = 0;
c.weightx = 1;
gbl.setConstraints(privatebutton,c);
c.gridx = 3; // Layout Constraints For emoticansbutton
c.gridy = 0;
c.weightx = 1;
gbl.setConstraints(emoticansbutton,c);
c.gridx = 4; // Layout Constraints For settingsbutton
c.gridy = 0;
c.weightx = 1;
gbl.setConstraints(settingsbutton,c);
c.gridx = 5; // Layout Constraints For exitbutton
c.gridy = 0;
c.weightx = 1;
gbl.setConstraints(exitbutton,c);

c.gridx = 0;
c.gridy = 1;
c.fill=GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 2;
c.gridwidth=2;
gbl.setConstraints(southpanel,c);
add(southpanel);

}

}

---------------------

This layout is posing problems when custom multicolor text area component is used.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Layout Problem, Help Me Plz
 
Similar Threads
GridBagLayout:: a few Qs
GridBagLayout problems
getScaledInstance() and resize event
help automate code
Keyboard layout problem