Re: Swing Text components
Here is the code snippet:
----------------------------------------------------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
public class NewMessage extends JFrame implements ActionListener,
KeyListener {
private Clipboard clipboard;
private JTextField tofield;
private JTextField ccfield;
private JTextField subfield;
private TextArea bodyarea;
public NewMessage() {
...
Container contents = this.getContentPane();
contents.setLayout(new BorderLayout());
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
JMenuBar menubar = new JMenuBar();
contents.add(menubar, BorderLayout.NORTH);
...
JSplitPane messpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
messpane.setEnabled(false);
messpane.setDividerSize(0);
contents.add(messpane, BorderLayout.CENTER);
JPanel labelpane = new JPanel(new GridLayout(0, 1));
messpane.add(labelpane, JSplitPane.LEFT);
JLabel fromlabel = new JLabel(" From: ");
labelpane.add(fromlabel);
JLabel tolabel = new JLabel(" To: ");
labelpane.add(tolabel);
JLabel cclabel = new JLabel(" Cc: ");
labelpane.add(cclabel);
JLabel sublabel = new JLabel(" Subject: ");
labelpane.add(sublabel);
JPanel textpane = new JPanel(new GridLayout(0, 1));
messpane.add(textpane);
frombox = new JComboBox();
textpane.add(frombox);
tofield = new JTextField(50);
textpane.add(tofield);
ccfield = new JTextField(50);
textpane.add(ccfield);
subfield = new JTextField(50);
textpane.add(subfield);
JPanel bodypane = new JPanel();
contents.add(bodypane, BorderLayout.SOUTH);
int fontHeight = getFontMetrics(getFont()).getHeight();
bodyarea = new TextArea("", (this.getHeight() - (fontHeight * 9))
/ fontHeight, ((getWidth() - 30) / 7),
TextArea.SCROLLBARS_VERTICAL_ONLY);
bodypane.add(bodyarea);
addKeyListener(this);
setVisible(true);
}
public static void main(String args[]) {
new NewMessage();
}
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("Exit")) {
this.dispose();
}
else if (ae.getActionCommand().equals("Copy")) {
clipboard.setContents(new StringSelection(""), null);
...
clipboard.setContents(new StringSelection(selText), null);
}
else if (ae.getActionCommand().equals("Cut")) {
clipboard.setContents(new StringSelection(""), null);
String selText = "'";
...
clipboard.setContents(new StringSelection(selText), null);
}
else if (ae.getActionCommand().equals("Paste")) {
if (tofield.hasFocus())
JOptionPane.showMessageDialog(this, "To: field has focus");
else if (ccfield.hasFocus())
JOptionPane.showMessageDialog(this, "CC: field has focus");
else if (subfield.hasFocus())
JOptionPane.showMessageDialog(this, "Sub: field has focus");
else if (bodyarea.hasFocus())
JOptionPane.showMessageDialog(this, "Bodyarea has focus");
System.out.println(getFocusOwner().toString());
}
}
public void addKeyListener(KeyListener arg0) {
tofield.addKeyListener(arg0);
ccfield.addKeyListener(arg0);
subfield.addKeyListener(arg0);
}
}
-------------------------------------------------------------------------------------------------------
Here is a link to the complete code: www.cse.iitb.ac.in/~sameer/prob.htm
This file can be run standalone simply by "javac NewMessage.java" and
"java NewMessage".
Hope someone can figure out the problem from this. Thank you