It's a bit long
Actually it's not that it isn't working, it is I don;t know the best way to do it.
package trunk.textConversation;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*; //for layout managers and more
import java.awt.event.*;//for action events
public class Textconversation extends JPanel implements ActionListener {
/**
*
*/
//The name of the user
public static String name = "Guest";
//The message of the user
public static String message;
//The text to output
public static String[] initString;
//The style the text will be in
public static String[] initStyles = {"regular"};
public static JFrame frame;
private static final long serialVersionUID = 1;
//private String newline = "\n";
protected static final String textFieldString = "JTextField";
protected static final String buttonString = "JButton";
protected JLabel actionLabel;
public Textconversation() {
setLayout(new BorderLayout());
//Create a regular text field.
JTextField textField = new JTextField(10);
textField.setActionCommand(textFieldString);
textField.addActionListener(this);
//Create some labels for the fields.
JLabel textFieldLabel = new JLabel();
textFieldLabel.setLabelFor(textField);
//Create a label to put messages during an action event.
actionLabel = new JLabel("Type text in a field and press Enter.");
actionLabel.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
//Lay out the text controls and the labels.
JPanel textControlsPane = new JPanel();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
textControlsPane.setLayout(gridbag);
JLabel[] labels = {textFieldLabel};
JTextField[] textFields = {textField};
addLabelTextRows(labels, textFields, gridbag, textControlsPane);
c.gridwidth = GridBagConstraints.REMAINDER; //last
c.anchor = GridBagConstraints.WEST;
c.weightx = 1.0;
textControlsPane.add(actionLabel, c);
textControlsPane.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Text Fields"),
BorderFactory.createEmptyBorder(5,5,5,5)));
//Create a text pane.
JTextPane textPane = createTextPane();
JScrollPane paneScrollPane = new JScrollPane(textPane);
paneScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
paneScrollPane.setPreferredSize(new Dimension(250, 155));
paneScrollPane.setMinimumSize(new Dimension(10, 10));
//Put the editor pane and the text pane in a split pane.
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
paneScrollPane,
textControlsPane);
splitPane.setOneTouchExpandable(true);
splitPane.setResizeWeight(0.5);
JPanel rightPane = new JPanel(new GridLayout(1,0));
rightPane.add(splitPane);
rightPane.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Styled Text"),
BorderFactory.createEmptyBorder(5,5,5,5)));
//Put everything together.
JPanel leftPane = new JPanel(new BorderLayout());
add(leftPane, BorderLayout.LINE_START);
add(rightPane, BorderLayout.LINE_END);
}
private void addLabelTextRows(JLabel[] labels, JTextField[] textFields, GridBagLayout gridbag, Container container) {
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.EAST;
int numLabels = labels.length;
for (int i = 0; i < numLabels; i++) {
c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last
c.fill = GridBagConstraints.NONE;//reset to default
c.weightx = 0.0;//reset to default
container.add(labels[i], c);
c.gridwidth = GridBagConstraints.REMAINDER;//end row
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
container.add(textFields[i], c);
}
}
public static void textOutput() {
Xmlreader.readElement();
initString = new String[Xmlreader.conversation.length];
for (int s = 0; s < Xmlreader.conversation.length; s++) {
initString[s] = Xmlreader.conversation[s] + "\n";
}
}
public void actionPerformed(ActionEvent e) {
//After user clicked enter
String prefix = "You typed \"";
if (textFieldString.equals(e.getActionCommand())) {
JTextField source = (JTextField)e.getSource();
actionLabel.setText(prefix + source.getText() + "\"");
message = source.getText();
Xmlreader.addElement();
} else if (buttonString.equals(e.getActionCommand())) {
Toolkit.getDefaultToolkit().beep();
}
}
public JTextPane createTextPane() {
textOutput();
JTextPane textPane = new JTextPane();
textPane.setEditable(false);
StyledDocument doc = textPane.getStyledDocument();
addStylesToDocument(doc);
try {
for (int i=0; i < initString.length; i++) {
doc.insertString(doc.getLength(), initString[i],
doc.getStyle(initStyles[0]));
}
} catch (BadLocationException ble) {
System.err.println("Couldn't insert initial text into text pane.");
}
return textPane;
}
protected void addStylesToDocument(StyledDocument doc) {
//Initialize some styles.
Style def = StyleContext.getDefaultStyleContext().
getStyle(StyleContext.DEFAULT_STYLE);
Style regular = doc.addStyle("regular", def);
StyleConstants.setFontFamily(def, "SansSerif");
Style s = doc.addStyle("italic", regular);
StyleConstants.setItalic(s, true);
s = doc.addStyle("bold", regular);
StyleConstants.setBold(s, true);
s = doc.addStyle("small", regular);
StyleConstants.setFontSize(s, 10);
s = doc.addStyle("large", regular);
StyleConstants.setFontSize(s, 16);
s = doc.addStyle("icon", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
ImageIcon pigIcon = createImageIcon("", "");
if (pigIcon != null) {
StyleConstants.setIcon(s, pigIcon);
}
s = doc.addStyle("button", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
ImageIcon soundIcon = createImageIcon("", "");
JButton button = new JButton();
if (soundIcon != null) {
button.setIcon(soundIcon);
} else {
button.setText("BEEP");
}
button.setCursor(Cursor.getDefaultCursor());
button.setMargin(new Insets(0,0,0,0));
button.setActionCommand(buttonString);
button.addActionListener(this);
StyleConstants.setComponent(s, button);
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path,
String description) {
java.net.URL imgURL = Textconversation.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
frame = new JFrame("Conversation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
frame.add(new Textconversation());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatching thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
Message was edited by:
Dennis56