Article View: borland.public.jbuilder.java.language
Article #19197Re: Adding a new line to a message box...
From: Conrad Hoesle-Kienzlen
Date: Sun, 08 Mar 1998 00:00
Date: Sun, 08 Mar 1998 00:00
171 lines
5520 bytes
5520 bytes
John,
you may like this....
-------------------------- snip ------------------------------------
// Creates a modal message panel with as many labels as there are
// lines in the message to display (delimited by \n), and 1 OK button
// (c) 1998 C.Hoesle-Kienzlen <Conrad_Hoesle@csi.com>
package BoxPack;
import java.awt.*;
import java.awt.event.*;
import borland.jbcl.layout.*;
import borland.jbcl.control.*;
public class TextBox extends Dialog {
Panel messagePanel = new Panel();
Panel textPanel = new Panel();
Panel buttonPanel = new Panel();
Button button1 = new Button();
static Frame frm = new Frame();
BorderLayout borderLayout1 = new BorderLayout();
FlowLayout flowLayout1 = new FlowLayout();
GridBagLayout gridBagLayout1 = new GridBagLayout();
private TextBox(Frame frame, String title, boolean modal) {
super(frm, title, modal);
try {
jbInit();
}
catch (Exception e) { e.printStackTrace(); }
add(messagePanel, "Center");
}
public TextBox() {
this(frm, "", true);
}
public void jbInit() throws Exception {
this.addWindowListener(new TextBox_this_windowAdapter(this));
messagePanel.setLayout(borderLayout1);
messagePanel.add(textPanel, "Center");
textPanel.setLayout(gridBagLayout1);
messagePanel.add(buttonPanel, "South");
buttonPanel.setLayout(flowLayout1);
buttonPanel.add(button1, null);
button1.setLabel(" OK ");
button1.addActionListener(new TextBox_button1_actionAdapter(this));
}
// Create labels, one label per line, delimiter is \n
public void setMessage(String txt) {
int n = 0; // label counter
int p = 0; // points to start of substring
int i = 0; // points to next newline character
int L = txt.length();
Label lines[] = new Label[NumLines(txt)]; // count # of lines
String sub;
while ((i=txt.indexOf("\n",p)) <= L) {
if (i < 0) sub=txt.substring(p).trim();
else sub=txt.substring(p,i).trim();
lines[n] = new Label(sub);
textPanel.add(lines[n], new GridBagConstraints2(0, n, 1, 1, 0.0, 0.0,
GridBagConstraints.NORTHWEST,GridBagConstraints.HORIZONTAL,
new Insets(0, 5, 0, 5), 0, 0));
n++;
if (i < 0 | L <= (p=++i)) break;
}
pack(); // resize and center on screen
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((d.width - this.getPreferredSize().width) / 2,
(d.height - this.getPreferredSize().height) / 2);
}
// Count number of lines in string, delimiter is \n
int NumLines(String str) {
int p = 0, i = 0, n = 0, L = str.length();
while ((i=str.indexOf("\n",p)) < L) {
n++;
if ( i < 0 | L <= (p=i+2)) break;
}
return n;
}
// Set the button label, default is <OK>
public void setButtonLabel(String lab) {
button1.setLabel(lab);
}
//Close the dialog
void button1_actionPerformed(ActionEvent e) {
dispose();
}
void this_windowClosing(WindowEvent e) {
dispose();
}
} //*********** End of class TextBox **************************
class TextBox_this_windowAdapter extends WindowAdapter {
TextBox adaptee;
TextBox_this_windowAdapter(TextBox adaptee) {
this.adaptee = adaptee;
}
public void windowClosing(WindowEvent e) {
adaptee.this_windowClosing(e);
}
}
class TextBox_button1_actionAdapter implements java.awt.event.ActionListener {
TextBox adaptee;
TextBox_button1_actionAdapter(TextBox adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button1_actionPerformed(e);
}
}
--------------------------------------- snap
----------------------------------------
Cheers,
... Conrad
John Papadomichelakis wrote:
> Hi JBers..
>
> I know it may sound funny... but...
>
> How can I place text in a second line in a Message compoment?
>
> I tried to use line feeds and the rest I found in the JDK manual with no
> luck.
> Did I forgot something?
>
> I only want to display a message in two lines!
>
> --
>
> Papadomichelakis John
> "Unite for Java! - http://www.javalobby.org"
> __________________________________________________
> email : michelos@freemail.gr
> TEL : 30 31 758 429, 30 31 709 796
> __________________________________________________
>
> ------------------------------------------------------------------------
>
> John Papadomichelakis <michelos@freemail.gr>
> BIA
>
> John Papadomichelakis
> BIA <michelos@freemail.gr>
> HTML Mail
> 9 Filippou Work: 267 923
> Euosmos Home: 758 429
> Thessaloniki Netscape Conference Address
> 56224 Netscape Conference DLS Server
> Hellas
> Additional Information:
> Last Name Papadomichelakis
> First NameJohn
> Version 2.1
Technical Details
Message-ID:
<3501D65B.FC3B5B48@csi.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3501B98E.486C7DC6@freemail.gr>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,2917bcd8de6880b3 X-Google-Attributes: gidf74b3,public From: Conrad Hoesle-Kienzlen <Conrad_Hoesle@csi.com> Subject: Re: Adding a new line to a message box... Date: 1998/03/08 Message-ID: <3501D65B.FC3B5B48@csi.com> X-Deja-AN: 331813751 Content-Transfer-Encoding: 7bit References: <3501B98E.486C7DC6@freemail.gr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: CHK IT-Consulting Newsgroups: borland.public.jbuilder.java.language