Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

AWT TextArea Too Small

Posted on 1999-07-06
7
Medium Priority
?
301 Views
Last Modified: 2013-12-29
I read that Java 1.1 AWT TextAreas can only hold about 32kbytes.  JTextAreas a tiny bit more.

I have some large text files/strings I want to display.

What are my alternatives?

If I have to make something myself what is the best stratedgy.  Which classes do I subclass or do I start from scratch?

I'm getting more points tomorrow so I will up the points on this question accordingly depending on the level of detail.

Thanks in advance
0
Comment
Question by:Steve34
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 8

Expert Comment

by:diakov
ID: 1245390
Implement virtual paging. To do this your program have to follow the interaction with the user and change the content with the appropriate content. I believe you also have to implement your own scroll bar.

Cheers,
  Nik
0
 
LVL 7

Accepted Solution

by:
vladi21 earned 360 total points
ID: 1245391
Look at my example:

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

public class ap2 extends Applet
{

public void init()
{
MyTextField txt=new MyTextField("Hello World...", 30);
FontMetrics fm=getFontMetrics(getFont());
int w=fm.charWidth('w')*30;
int h=fm.getHeight()+4;
setLayout(null);
txt.reshape(50,50,w, h);
add(txt);
txt.requestFocus();
}
}


---------------------
//version 1.0.betta :)

import java.awt.*;
import java.awt.event.*;

class MyTextField extends Panel implements Runnable
{
String text="";
String echoText="";
StringBuffer textbuf=new StringBuffer("");

private final static int MAX_LEN=1024;

int pos=0;
boolean first=true;

private boolean isEnabled=true;
private boolean isActive=false;
private boolean isEcho=false;

private FontMetrics fm;
private int ypad;
private int xpad;
private int maxTextLen;

private int w;
private int h;


static void OUT(String line)
{
;//System.out.println(line);
}

MyTextField(String t, int max)
{

super();

maxTextLen=max;
text=t;
textbuf=new StringBuffer(t);
pos=t.length();
enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK
| AWTEvent.MOUSE_EVENT_MASK  );
setCursor(new Cursor(Cursor.TEXT_CURSOR));
setBackground(Color.white);
setForeground(Color.black);
}

MyTextField(String t)
{
this(t, MAX_LEN);
}

MyTextField(int len)
{
this("", len);
}

MyTextField()
{
this("", MAX_LEN);
}



public void setEnabled(boolean en)
{
super.setEnabled(en);
isEnabled=en;
if (fm!=null) repaint(2,2, fm.stringWidth(text)+20, h-4);
}

public void setEditable(boolean en)
{
setBackground(Color.lightGray);
setEnabled(en);
//repaint();
}

public boolean isFocusTraversable()
{
return true;
}

protected String getText()
{
return text;
}


protected void setText(String s)
{
if (s==null) {s="";echoText="";}
textbuf=new StringBuffer(s);
text=s;
pos=0;
repaint();

return;
}

/*protected void setEchoChar(char c)
{

}*/

protected void setEcho(String s) //temp param
{
isEcho=true;
echoText=s;
}

public void paint(Graphics  g)
{
if (first)
{

w=getSize().width;
h=getSize().height;

first=false;
fm=g.getFontMetrics();
ypad=(h>>1)-(fm.getHeight()>>1)+fm.getMaxAscent();
xpad=fm.charWidth('r');
}

String ptext=isEcho?echoText:text;
g.drawString(ptext, xpad, ypad);

if (isEnabled && isActive)
{
int x=fm.stringWidth(ptext.substring(0,pos));
g.drawLine(x+xpad,4,x+xpad,h-4);
}

DrawBorder( g, 0,  0, w, h, clr, true );
}

/*public Dimension minimumSize()
{

   
//return new Dimension( fm.stringWidth(text)+20, fm.getHeight()+6 );
return new Dimension( 200,50);
}

    public Dimension preferredSize()
{
return minimumSize();
}*/


protected void processFocusEvent( FocusEvent e )
{
if (e.getID()==FocusEvent.FOCUS_GAINED )
{
isActive=true;
if (fm!=null )
{
new Thread(this).start();
}
}else if (e.getID()==FocusEvent.FOCUS_LOST )
{
isActive=false;
if (fm!=null) repaint(2,2, fm.stringWidth(text)+20, h-4);

}
}

public void run()
{
try{Thread.sleep(100);}catch (InterruptedException i){;}
repaint(2,2, fm.stringWidth(text)+20, h-4);
}


protected void processMouseEvent( MouseEvent e )
{
if (e.getID()==MouseEvent.MOUSE_CLICKED)
{
isActive=true;
int x=e.getX()-xpad;
if (x<0) x=0;

int len=textbuf.length();

int endx=fm.stringWidth(text);

//WebSCD.OUT("x="+x+" end="+end);

if (x<endx)
{
int l=0;
int r=0;
for (int i=0;i<len;i++)
{

r+=fm.charWidth(textbuf.charAt(i));
if (x>=l && x<r)
{
pos=i;
repaint(2,2, endx+20, h-4);
break;
}
l=r;
}

}else
{
pos=len;
repaint(2,2, endx+20, h-4);
}


}
}


protected void processKeyEvent( KeyEvent e )
{
if (!(isEnabled && isActive)) return;

int len=textbuf.length();

boolean refresh=false;
boolean change=false;


if (e.getID()==KeyEvent.KEY_PRESSED )
{
int code=e.getKeyCode();
if(e.isActionKey())
{    
refresh=true;
if (code==KeyEvent.VK_LEFT && len>0 && pos>0)
{
pos--;
}else if (code==KeyEvent.VK_RIGHT && pos<len)
{
pos++;
}else if (code==KeyEvent.VK_HOME && pos>0)
{
pos=0;
}else if (code==KeyEvent.VK_END && pos<len)
{
pos=len;
}else refresh=false;
}else if (code==KeyEvent.VK_DELETE && len>0 && pos<len)
{

textbuf.setLength(0);
textbuf.append(text.substring(0,pos));
textbuf.append(text.substring(pos+1));
     change=true;
OUT("VK_DELETE pos="+pos+" buf="+textbuf);
refresh=true;
}
}else if (e.getID()==KeyEvent.KEY_TYPED)
{
char key=e.getKeyChar();
if ( key==KeyEvent.VK_BACK_SPACE )
{
if (len>0 && pos>0)
{
textbuf.setLength(0);
textbuf.append(text.substring(0,pos-1));
textbuf.append(text.substring(pos));
pos--;
change=true;
refresh=true;
}

}else if ( key==KeyEvent.VK_DELETE )
{
OUT("VK_DELETE");

}else if ( key==KeyEvent.VK_ENTER || key=='\r' )
{
OUT("VK_ENTER");
transferFocus();

}else if ( key==KeyEvent.VK_TAB )
{
OUT("VK_TAB");

}else if(!e.isActionKey() && e.getModifiers() != KeyEvent.CTRL_MASK)
{
if (len<maxTextLen)
{
//if (isEcho) key='*';
textbuf.insert(pos, key);
pos++;
change=true;
refresh=true;
}else getToolkit().beep();
}
}

if (change)
{
text=new String(textbuf);
if (isEcho)
{
int tlen=textbuf.length();
char []ebuf=new char[tlen];
for(int i=0;i<tlen;i++) ebuf[i]='*';
echoText=new String(ebuf);
}
}

if (refresh) repaint(2,2, fm.stringWidth(text)+20, h-4);
}





static final Color clr[]=
{
new Color( 255,255,255 ),
new Color( 227,227,227 ), Color.lightGray,
new Color( 100,100,100 ), new Color( 0,0,0 )
};

static void DrawBorder( Graphics g, int x1, int y1, int x2, int y2, Color colors[], boolean down )
{
int x1_ = x1 + 1,
x2_ = x2 - 1,
y1_ = y1 + 1,
y2_ = y2 - 1;
     
g.setColor( colors[(down)?4:0] );
g.drawLine( x1, y1, x2_, y1 );
g.drawLine( x1, y1, x1, y2_ );

g.setColor( colors[(down)?3:1] );
g.drawLine( x1_, y1_, x2-2, y1_ );
g.drawLine( x1_, y1_, x1_, y2-2 );

g.setColor( colors[(down)?1:3] );
g.drawLine( x1_, y2_, x2_, y2_ );
g.drawLine( x2_, y1_, x2_, y2_ );
     
g.setColor( colors[(down)?0:4] );
g.drawLine( x1, y2, x2, y2 );
g.drawLine( x2, y1, x2, y2 );
}
}


// possible additional features
// keys filter
// mouse selection
// *
// virtual size
// flash cursor

0
 

Author Comment

by:Steve34
ID: 1245392
Vladi21;

I'm impressed, but I am also confused ( I'm a new programmer).  My problem is that the Java 1.1 AWT TextArea has a top memory capacity of about 32kbytes which isn't enough for me to display some really FAT stirngs and files that I need to show the user.   I was looking for ideas on how to build/modify something myself to
get a component that would do that.

Could you help me out a bit more on that point.  Your TextField was impressive, but I missed the hint in it about getting around that max memory problem.
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 7

Expert Comment

by:vladi21
ID: 1245393
"max memory problem" - its problem of native Windows textarea
AWT TextArea use native component.
If you make your custom MyTextArea all will be ok :)
One way: use Canvas and  draw text by multiply drawString()
Second way: use AWT TextArea as buffer - see diakov comment
0
 

Author Comment

by:Steve34
ID: 1245394
Diakov;

I thought about your idea yesterday.  It seems that TextArea doesn't have enough functions to give the kind of info I would need to do something like that.  Am I wrong?   I'm thinking of programatically monitoring the user.  When S/He gets to the bottom of what S/He is reading the program erases the text field and dumps in the material that wouldn't fit before.

I have made my own rudimentry TextArea.  It is basically an AWT Canvas that I paint on and that I stuffed into an AWT ScrollPane.

Do you think it would be possible for me to make the Canvas as "long" as the Files/Strings are big........or would I run into similar memory problems that the AWT designers did?

If not I guess I can try the virtual paging idea with my homemade component as I can put in whatever reporting functions I need.

Any stratedgy tips for that?   I was thinking of breaking the FAT strings up into sub strings and doing an erase/insert routine when the user scrolls to the top/bottom of what they already have displayed.
0
 
LVL 3

Expert Comment

by:darinw
ID: 1245395
Reviewing Question

darinw
Customer Service @ Experts Exchange

0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6827755
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  Or if you are an EE Pro user, click the link below to select open items for your Member ID using Power Search:
http://www.experts-exchange.com/jsp/qPowerSearch.jsp.

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
http://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thank you,
Moondancer
Moderator @ Experts Exchange
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question