Article View: borland.public.jbuilder.java.language
Article #41How to make multi-line dialogs
From: Conrad Hoesle-Kienzlen
Date: Sat, 14 Mar 1998 00:00
Date: Sat, 14 Mar 1998 00:00
132 lines
4630 bytes
4630 bytes
Hi everybody,
since I needed something like this and have seen questions about it here
and in other newsgroups, here is what I have put together:
A dialog class that can be given a string with embedded \n newlines. It
displays them line below line and is quite small. It even can be made
into a bean since it has a parameterless constructor. Enjoy.
...Conrad
-------------- 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@compuserve.com>
package dialogpk;
import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;
import borland.jbcl.layout.*;
import borland.jbcl.control.*;
public class TextBox extends Dialog {
private Panel messagePanel = new Panel();
private Panel textPanel = new Panel();
private Panel buttonPanel = new Panel();
private Button button1 = new Button();
private static Frame frm = new Frame();
private BorderLayout borderLayout1 = new BorderLayout();
private FlowLayout flowLayout1 = new FlowLayout();
private GridBagLayout gridBagLayout1 = new GridBagLayout();
public TextBox(Frame frame, String title, boolean modal) {
super(frm, title, modal);
try {
jbInit();
}
catch (Exception e) { e.printStackTrace(); }
}
public TextBox(String title) {
this(frm, title, true);
}
public TextBox() {
this(frm, "", true);
}
public void jbInit() throws Exception {
this.addWindowListener(new TextBox_this_windowAdapter(this));
textPanel.setLayout(gridBagLayout1);
messagePanel.setLayout(borderLayout1);
messagePanel.add(textPanel, "Center");
button1.setLabel(" OK ");
button1.addActionListener(new TextBox_button1_actionAdapter(this));
buttonPanel.setLayout(flowLayout1);
buttonPanel.add(button1, null);
messagePanel.add(buttonPanel, "South");
this.add(messagePanel, "Center");
}
// Set the background color of the diaolog box
public void setBoxColor(Color col) {
messagePanel.setBackground(col);
}
// Get the background color of the dialog box
public Color getBoxColor() {
return messagePanel.getBackground();
}
// Create labels, one label per line, delimiter is \n
public void setMessage(String txt) {
StringTokenizer str = new StringTokenizer(txt, "\n");
int n = 0; // y-coordinate in GB-Constraints
while (str.hasMoreTokens()) {
textPanel.add(new Label(str.nextToken()),
new GridBagConstraints2(0, n++, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,
new Insets(0, 5, 0, 5), 0, 0));
}
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);
}
// Set the button label, default is <OK>
public void setButtonLabel(String lab) {
button1.setLabel(lab);
}
// Get the button label
public String getButtonLabel() {
return button1.getLabel();
}
//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);
}
}
Technical Details
Message-ID:
<350AF60C.516BC457@csi.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: ff01d,dc69b8eb8caff9b4,start X-Google-Attributes: gidff01d,public X-Google-Thread: f74b3,dc69b8eb8caff9b4,start X-Google-Attributes: gidf74b3,public From: Conrad Hoesle-Kienzlen <Conrad_Hoesle@csi.com> Subject: How to make multi-line dialogs Date: 1998/03/14 Message-ID: <350AF60C.516BC457@csi.com> X-Deja-AN: 334089648 Content-Transfer-Encoding: 7bit Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: CHK IT-Consulting Newsgroups: borland.public.jbuilder.java.language,borland.public.jbuilder.applet-issues