Results 1 to 9 of 9
- 08-12-2009, 11:39 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 6
- Rep Power
- 0
-
What do you mean by "when a JTextField is selected"? Do you mean when it gains focus? When a portion of the text is highlighted?
Do you have a small compilable program that encapsulates your problem? If not, I would suggest that you create this and post it here, and we can use it as starting off point for discussion.
- 08-13-2009, 12:07 AM #3
Member
- Join Date
- Aug 2009
- Posts
- 6
- Rep Power
- 0
Sorry if I was being vague, yes I mean when a JTextField gains focus.
I have a code portion but its way too long and there's a lot of stuff that comes with it but here's a gist of what I'm talking about
I've got it so that when you add text to the textfield its displayed in an JTextArea but I also want to display some text in another JTextArea when the focus is one the TextField because i have multiple TextFields and want to display some information when they are "selected" or when they gain focus.Java Code:import java.net.URL; import java.awt.*; import javax.swing.*; import javax.swing.text.*; import java.awt.event.*; import javax.swing.event.*; import java.util.ArrayList; import java.io.*; import java.util.*; public class CreateMemo implements ActionListener { JDesktopPane desktop; private InternalWorkFrame frame; protected JLabel actionLabel; protected JTextField nameCorres; static final String CORRES = "Corres"; private final static String newline = "\n"; protected JTextArea textArea; protected JTextArea preview; public CreateMemo(JDesktopPane desktop) { this.desktop = desktop; } protected void createMemoFrame() { frame = new InternalWorkFrame(); frame.setLayout(new BorderLayout()); nameCorres = new JTextField(10); nameCorres.addActionListener(this); nameCorres.setActionCommand(CORRES); //Create some labels for the fields. JLabel corresLabel = new JLabel("Name of Correspondent "); corresLabel.setLabelFor(nameCorres); //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); c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last c.fill = GridBagConstraints.NONE; //reset to default c.weightx = 0.0; //reset to default textControlsPane.add(corresLabel, c); c.gridwidth = GridBagConstraints.REMAINDER; //end row c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; textControlsPane.add(nameCorres, c); preview = new JTextArea(); preview.setEditable(false); JScrollPane scrollPane = new JScrollPane(preview); scrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setPreferredSize(new Dimension(450, 400)); scrollPane.setMinimumSize(new Dimension(300, 10)); //Put the editor pane and the text pane in a split pane. JPanel rightPane = new JPanel(new GridLayout(1,0)); rightPane.add(scrollPane); rightPane.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Memo Preview"), BorderFactory.createEmptyBorder(5,50,5,50))); //Create a text area. textArea = new JTextArea(); textArea.setFont(new Font("Serif", Font.ITALIC, 16)); textArea.setEditable(false); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); JScrollPane areaScrollPane = new JScrollPane(textArea); areaScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); areaScrollPane.setPreferredSize(new Dimension(250, 250)); areaScrollPane.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Info on Requirements"), BorderFactory.createEmptyBorder(5,5,5,5)), areaScrollPane.getBorder())); //Put everything together. JPanel leftPane = new JPanel(new BorderLayout()); leftPane.add(textControlsPane, BorderLayout.PAGE_START); leftPane.add(areaScrollPane, BorderLayout.CENTER); frame.add(leftPane, BorderLayout.LINE_START); frame.setVisible(true); desktop.add(frame); try { frame.setSelected(true); } catch (java.beans.PropertyVetoException e) {} } public void actionPerformed(ActionEvent e) { if(CORRES.equals(e.getActionCommand())) { String text = nameCorres.getText(); preview.append(text + newline); } //Make sure the new text is visible, even if there //was a selection in the text area. preview.setCaretPosition(preview.getDocument().getLength()); } }
Hope that makes sense.
Thank you in advance for any help you can give meLast edited by Frys82; 08-13-2009 at 12:15 AM.
-
To help us help you, I'd suggest that you take some time to create an SSCCE, a very small program that is compilable, runnable, has no extraneous code that's unrelated to your problem and either demonstrates your problem or sets things up so we can try to solve your problem using this small test program. Please look at the link for details on what these critters require. Again, good luck. Oh, and please use code tags (see my signature).
-
for instance,
Java Code:import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import javax.swing.*; public class FuSwing { private static final int ROWS = 5; private static final int COLS = 4; private static final int FIELD_COUNT = ROWS * COLS; private JPanel mainPanel = new JPanel(); private JTextField[] textFields = new JTextField[FIELD_COUNT]; private JTextArea textarea = new JTextArea(10, 30); public FuSwing() { JPanel fieldPanel = new JPanel(new GridLayout(ROWS, COLS, 10, 10)); int index = 0; FocusListener focusListener = new MyFocusAdapter(); for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { String textFieldName = "TextField " + index; textFields[index] = new JTextField("[" + col + ", " + row + "]"); textFields[index].setName(textFieldName); textFields[index].addFocusListener(focusListener); JLabel label = new JLabel(textFieldName); JPanel p = new JPanel(); p.add(label); p.add(textFields[index]); fieldPanel.add(p); index++; } } textarea.setEditable(false); textarea.setFocusable(false); mainPanel.setLayout(new BorderLayout()); mainPanel.add(fieldPanel, BorderLayout.NORTH); mainPanel.add(new JScrollPane(textarea), BorderLayout.CENTER); } public JComponent getPanel() { return mainPanel; } private class MyFocusAdapter extends FocusAdapter { public void focusGained(FocusEvent e) { textarea.append(((JTextField) e.getSource()).getName() + " has focus\n"); textarea.append("Text Contained: " + ((JTextField) e.getSource()).getText() + "\n\n"); } } private static void createAndShowGUI() { JFrame frame = new JFrame("FuSwing Application"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new FuSwing().getPanel()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
- 08-13-2009, 01:04 AM #6
Member
- Join Date
- Aug 2009
- Posts
- 6
- Rep Power
- 0
OK here it is in all its entirety. There's two JTextFields and two text areas and when I switch focus between the two I want one of the text areas to display a message, if that's at all possible.
Hope this is clearer
Java Code:import java.awt.*; import javax.swing.*; import javax.swing.text.*; import java.awt.event.*; import javax.swing.event.*; import java.util.*; public class CreateMemo extends JFrame implements ActionListener { protected JTextField name; protected JTextField add; static final String NAME = "Name"; static final String ADD = "Address"; private final static String newline = "\n"; protected JTextArea infoText; protected JTextArea preview; public CreateMemo() { setLayout(new BorderLayout()); //JTextFields name and address name = new JTextField(10); name.addActionListener(this); name.setActionCommand(NAME); add = new JTextField(10); add.addActionListener(this); add.setActionCommand(ADD); //labels for the JTextField. JLabel nameLabel = new JLabel("Name"); nameLabel.setLabelFor(name); JLabel addLabel = new JLabel("Address: "); addLabel.setLabelFor(add); //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 = {nameLabel, addLabel }; JTextField[] textFields = {name, add}; //add labels to textfields addLabelTextRows(labels, textFields, gridbag, textControlsPane); c.gridwidth = GridBagConstraints.REMAINDER; //last c.anchor = GridBagConstraints.WEST; c.weightx = 1.0; textControlsPane.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Document Data"), BorderFactory.createEmptyBorder(5,150,5,150))); //The infomation TextArea. JTextArea textArea = new JTextArea(); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); JScrollPane areaScrollPane = new JScrollPane(textArea); areaScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); areaScrollPane.setPreferredSize(new Dimension(250, 250)); areaScrollPane.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Information"), BorderFactory.createEmptyBorder(5,5,5,5)), areaScrollPane.getBorder())); //The preview TextArea. preview = new JTextArea(); preview.setEditable(false); JScrollPane scrollPane = new JScrollPane(preview); scrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setPreferredSize(new Dimension(450, 400)); scrollPane.setMinimumSize(new Dimension(300, 10)); JPanel rightPane = new JPanel(new GridLayout(1,0)); rightPane.add(scrollPane); rightPane.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Memo Preview"), BorderFactory.createEmptyBorder(5,50,5,50))); //Put everything together. JPanel leftPane = new JPanel(new BorderLayout()); leftPane.add(textControlsPane, BorderLayout.PAGE_START); leftPane.add(areaScrollPane, BorderLayout.CENTER); 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 void actionPerformed(ActionEvent e) { // shows the JTextField data in the JTextArea if(NAME.equals(e.getActionCommand())) { String text = name.getText(); preview.append(text + newline); } if(ADD.equals(e.getActionCommand())) { String text2 = add.getText(); preview.append(text2 + newline); } } private static void createAndShowGUI() { CreateMemo frame = new CreateMemo(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setTitle("Memo"); frame.pack(); } public static void main(String[] args) { SwingUtilities.invokeLater( new Runnable() { public void run() { createAndShowGUI(); } }); } }
-
Thanks for posting your code. I think though for anyone to help we still need to know more about the details of just what you want to happen here. Please describe in as much detail as possible in a way that assumes that we know nothing at all about your project, and have no ability to read your mind. Best of luck.
-
Perhaps what you want to do is give your two JTextFields and your Information JTextArea DocumentListeners that listen for changes to the documents such as addition of text, removal of text and such. These changes can then be immediately displayed in the MemoPreview window. This would allow the MemoPreview window to respond immediately to the changes made in the other text components. Is this what you are looking for? If so, then a FocusListener as I suggested previously is not what you need, but again it would require several DocumentListeners that listen to the Text Component documents. Again best of luck.
-
For instance:
Java Code:import java.awt.*; import javax.swing.*; import javax.swing.text.*; import java.awt.event.*; import javax.swing.event.*; import java.util.*; public class CreateMemo extends JFrame // implements ActionListener { { protected JTextField name; protected JTextField add; static final String NAME = "Name"; static final String ADD = "Address"; private final static String newline = "\n"; protected JTextArea infoText; protected JTextArea preview; public CreateMemo() { setLayout(new BorderLayout()); // JTextFields name and address name = new JTextField(10); // name.addActionListener(this); name.setActionCommand(NAME); add = new JTextField(10); // add.addActionListener(this); add.setActionCommand(ADD); infoText = new JTextArea(); preview = new JTextArea(); DocumentListener myDocListener = new MyDocumentListener(); name.getDocument().addDocumentListener(myDocListener); add.getDocument().addDocumentListener(myDocListener); infoText.getDocument().addDocumentListener(myDocListener); // labels for the JTextField. JLabel nameLabel = new JLabel("Name"); nameLabel.setLabelFor(name); JLabel addLabel = new JLabel("Address: "); addLabel.setLabelFor(add); // 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 = {nameLabel, addLabel}; JTextField[] textFields = {name, add}; // add labels to textfields addLabelTextRows(labels, textFields, gridbag, textControlsPane); c.gridwidth = GridBagConstraints.REMAINDER; // last c.anchor = GridBagConstraints.WEST; c.weightx = 1.0; textControlsPane.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Document Data"), BorderFactory .createEmptyBorder(5, 150, 5, 150))); // The infomation TextArea. // JTextArea textArea = new JTextArea(); infoText.setLineWrap(true); infoText.setWrapStyleWord(true); JScrollPane areaScrollPane = new JScrollPane(infoText); areaScrollPane .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); areaScrollPane.setPreferredSize(new Dimension(250, 250)); areaScrollPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory .createCompoundBorder(BorderFactory .createTitledBorder("Information"), BorderFactory .createEmptyBorder(5, 5, 5, 5)), areaScrollPane.getBorder())); // The preview TextArea. // preview = new JTextArea(); preview.setEditable(false); preview.setWrapStyleWord(true); preview.setLineWrap(true); JScrollPane scrollPane = new JScrollPane(preview); scrollPane .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setPreferredSize(new Dimension(450, 400)); scrollPane.setMinimumSize(new Dimension(300, 10)); JPanel rightPane = new JPanel(new GridLayout(1, 0)); rightPane.add(scrollPane); rightPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory .createTitledBorder("Memo Preview"), BorderFactory .createEmptyBorder(5, 50, 5, 50))); // Put everything together. JPanel leftPane = new JPanel(new BorderLayout()); leftPane.add(textControlsPane, BorderLayout.PAGE_START); leftPane.add(areaScrollPane, BorderLayout.CENTER); 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); } } private class MyDocumentListener implements DocumentListener { public MyDocumentListener() { updatePreviewDocument(); } public void changedUpdate(DocumentEvent e) { updatePreviewDocument(); } public void insertUpdate(DocumentEvent e) { updatePreviewDocument(); } public void removeUpdate(DocumentEvent e) { updatePreviewDocument(); } private void updatePreviewDocument() { String previewStr = "Name: " + "\t" + name.getText() + "\n"; previewStr += "Address: " + "\t" + add.getText() + "\n\n"; previewStr += "Information: \n" + infoText.getText(); preview.setText(previewStr); } } private static void createAndShowGUI() { CreateMemo frame = new CreateMemo(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setTitle("Memo"); frame.pack(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
Similar Threads
-
Displaying a selected image with a JFileChooser into a textarea
By Jonte79 in forum AWT / SwingReplies: 2Last Post: 04-24-2009, 08:10 AM -
Applet - Displaying an HTML page with a selected resolution
By Java Tip in forum Java TipReplies: 0Last Post: 03-10-2008, 02:36 PM -
Displaying text in a JTextField after pressing a button
By RLRExtra in forum New To JavaReplies: 5Last Post: 01-17-2008, 09:01 PM -
Show Text Fields on combo Box selected value
By smajidali26 in forum AWT / SwingReplies: 0Last Post: 11-29-2007, 09:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks