I have a problem with Java Textfield that is when I cover all text in the JTextField and input new text immediately(do not pass backspace) into the JTextField, then I use function getText() I get the previous string not current string. Please help for some solutions. Thanks in advance.
I just tested the problem you described by adding a keyListener to a JTextField and printing the getText() method's return value to the console.
What I found out is that it is always one character behind if you want to use the getText() method right in the keyTyped or keyPressed event (I didn't know this because I usually just use a button to confirm I'm done entering the text and bind a KeyEvent to the Return key to trigger the button if a user wants to confirm by hitting enter)
I think this is due to the textField updating its text value AFTER the event is shot.
I assume this is what you did since you didn't provide sample code, so I'll delete this answer if it's not.
The work around to this is to implement what you want to do in the keyReleased method instead.
public void keyReleased(Event e)
{
System.out.println(myTextField.getText());
}
Don't use a KeyListener. The character has NOT been added to the Document when the keyPressed() event is fired.
Add an ActionListener to a JButton. This way the user clicks on the button when text is finised being entered.
Also, in the future post a SSCCE with you question so we can better understand what you are trying to do.
for example :
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class TextLabelMirror {
private JPanel mainPanel = new JPanel();
private JTextField field = new JTextField(20);
private JTextField field1 = new JTextField(20);
public TextLabelMirror() {
field.getDocument().addDocumentListener(new DocumentListener() {
#Override
public void changedUpdate(DocumentEvent e) {
updateLabel(e);
}
#Override
public void insertUpdate(DocumentEvent e) {
updateLabel(e);
}
#Override
public void removeUpdate(DocumentEvent e) {
updateLabel(e);
}
private void updateLabel(DocumentEvent e) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
field1.setText(field.getText());
}
});
}
});
mainPanel.setLayout(new GridLayout(1, 0, 10, 0));
mainPanel.add(field);
mainPanel.add(field1);
}
public JComponent getComponent() {
return mainPanel;
}
private static void createAndShowUI() {
JFrame frame = new JFrame("TextLabelMirror");
frame.getContentPane().add(new TextLabelMirror().getComponent());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowUI();
}
});
}
}
Related
This question already has answers here:
Value Change Listener to JTextField
(14 answers)
Closed 2 years ago.
I want to know if there is a way to notice if a listener is changing the text of a JTextField or if it is done by .setText(); I need to seperate between both cases, beacause it need to do different things when accesed by a user or by the programm.
I assume you use a DocumentListener to hook into user's input. You can remove this document listener while you call the setText from your program.
Take a look at the following example. When the button is pressed, the text is changed without the printing message.
public class DocumentListenerExample extends JFrame {
private JTextField textField;
private DocumentListener textFieldDocumentListener;
public DocumentListenerExample() {
super("");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
textField = new JTextField(20);
textFieldDocumentListener = new DocumentListener() {
#Override
public void removeUpdate(DocumentEvent e) {
System.out.println("Text changed by user");
}
#Override
public void insertUpdate(DocumentEvent e) {
System.out.println("Text changed by user");
}
#Override
public void changedUpdate(DocumentEvent e) {
System.out.println("Text changed by user");
}
};
textField.getDocument().addDocumentListener(textFieldDocumentListener);
add(textField);
JButton button = new JButton("Change text");
button.addActionListener(e -> {
textField.getDocument().removeDocumentListener(textFieldDocumentListener);
textField.setText(String.valueOf(Math.random()));
textField.getDocument().addDocumentListener(textFieldDocumentListener);
});
add(button);
pack();
setLocationByPlatform(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new DocumentListenerExample().setVisible(true);
});
}
}
I'm tying to make a program that acts similar to the Windows Command Prompt, or a terminal. It's basically just a JFrame with a JTextArea as output, and a JTextField as input. It looks like this:
I want to be able to get input from the JTextField whenever my program calls a method that returns a String, something like:
public static String getInput() {
//Wait for user to enter something, then press the enter key
}
I added an AbstractAction so I can do stuff when the enter key is pressed, but I still could figure out how to return the input as a String whenever I call the method.
Action action = new AbstractAction(){
#Override
public void actionPerformed(ActionEvent e) {
//Clears the JTextField
input.setText("");
}
};
I could put something like userInput = input.getText() in public void actionPerformed(), so it would just set a variable to whatever has been entered every time, and use userInput whenever I want to, but I want the user to have time to read whats on the screen, then have the program wait for a response, instead of just using the last thing they entered right away.
I tried to use a userInput variable and a boolean variable like this:
private static String userInput = "";
private static boolean enterPressed = false;
Action action = new AbstractAction(){
#Override
public void actionPerformed(ActionEvent e) {
userInput = input.getText()
enterPressed = true;
input.setText("");
}
};
...
public static String getInput() {
enterPressed = false;
while(!enterPressed){
//Do Nothing
}
enterPressed = false;
return userInput;
}
When I called output.setText(getInput());, it worked like I wanted to, except that the while(!enterPressed){} made my processor work a lot harder than it should need to. I'm pretty sure there's probably a lot better way of doing this.
Here's my whole code right now:
public class ConsoleFrame {
//Objects for Console Frame
JFrame frame = new JFrame();
JTextArea output = new JTextArea();
JTextField input = new JTextField();
BoxLayout boxLayout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
JScrollPane scrollPane = new JScrollPane(output);
DefaultCaret caret = (DefaultCaret)output.getCaret();
Action action = new AbstractAction(){
#Override
public void actionPerformed(ActionEvent e) {
input.setText("");
}
};
ConsoleFrame(){
input.addActionListener(action);
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
frame.setLayout(boxLayout);
frame.add(scrollPane);
frame.add(input);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 250);
frame.setLocationRelativeTo(null);
input.setMaximumSize(new Dimension(Integer.MAX_VALUE, 10));
output.setEditable(false);
output.setAutoscrolls(true);
}
public static String getInput() {
return null;
}
}
So, how could I stop the program until the user presses enter, every time I call getInput();?
I want to be able to get input from the JTextField whenever my program
calls a method that returns a String, something like:
I added an AbstractAction so I can do stuff when the enter key is
pressed, but I still could figure out how to return the input as a
String whenever I call the method.
public String getInput() {
return input.getText();
}
So, how could I stop the program until the user presses enter, every time I call getInput();?
You don't. Swing, like most UI frame works is event driven, that is, something happens and your respond to it.
So, with that in mind you should consider using some kind Observer Pattern, where you use a call back to be notified of some kind of change which you are interested in, like your ActionListener for example.
Instead, you could provide some kind of listener, which interested parties would register with and when the field changes you would notify them, for example...
import java.util.EventListener;
import java.util.EventObject;
public class InputEvent extends EventObject {
private final String text;
public InputEvent(Object source, String text) {
super(source);
this.text = text;
}
public String getText() {
return text;
}
}
public interface InputObsever extends EventListener {
public void inputChanged(InputEvent evt);
}
So, we now have an observer who will be notified when ever the input is changed/updated.
Then we simply need a way to un/register and fire the event...
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.EventListenerList;
import javax.swing.text.DefaultCaret;
public class ConsoleFrame {
//Objects for Console Frame
JFrame frame = new JFrame();
JTextArea output = new JTextArea();
JTextField input = new JTextField();
BoxLayout boxLayout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
JScrollPane scrollPane = new JScrollPane(output);
DefaultCaret caret = (DefaultCaret) output.getCaret();
Action action = new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
fireInputChanged(input.getText());
input.selectAll();
}
};
private EventListenerList listenerList = new EventListenerList();
ConsoleFrame() {
input.addActionListener(action);
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
frame.setLayout(boxLayout);
frame.add(scrollPane);
frame.add(input);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 250);
frame.setLocationRelativeTo(null);
input.setMaximumSize(new Dimension(Integer.MAX_VALUE, 10));
output.setEditable(false);
output.setAutoscrolls(true);
}
public String getInput() {
return input.getText();
}
public void addInputObsever(InputObsever obsever) {
listenerList.add(InputObsever.class, obsever);
}
public void removeInputObsever(InputObsever obsever) {
listenerList.remove(InputObsever.class, obsever);
}
protected void fireInputChanged(String text) {
InputObsever[] listeners = listenerList.getListeners(InputObsever.class);
if (listeners.length > 0) {
InputEvent evt = new InputEvent(this, text);
for (InputObsever obsever : listeners) {
obsever.inputChanged(evt);
}
}
}
}
Now, the point here is, when you want to know when the text has been input/changed, you register an observer to the instance of the ConsoleFrame
console.addInputObsever(new InputObsever() {
#Override
public void inputChanged(InputEvent evt) {
// Do what ever you need to do...
}
});
EDIT: First half removed. It was brought to my attention that it was erroneous.
The best option is to enclose a call to whatever you need your computer to execute after the user inputs text inside your actionPerformed method. So when the user inputs text and presses enter, the program automatically continues from there.
Action action = new AbstractAction(){
#Override
public void actionPerformed(ActionEvent e) {
userInput = input.getText()
enterPressed = true;
input.setText("");
//call next method;
}
};
This requires some more formatting work on your behalf, but it could be helpful depending on your project. This link has more information on this strategy.
Hope this helped.
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
yourFunction(jTextField1.getText());
jTextField1.setText("");
}
});
Btw just a tip, you may append the JTextArea to get the feel of a console window
I want to close my JDialog by hitting the "enter" key on my keyboard. how can I do that? thank you!
NOTE:
I want to do this, without any button involved.
THank you!
One way:
You could give it a close JButton
whose ActionListener has code that closes the dialog,
And make that button the default button for the dialog's rootpane.
e.g.,
myDialog.getRootPane().setDefaultButton(exitButton);
Option two:
Use Key Bindings to bind the enter key to exit code in an AbstractAction.
e.g.,
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class DemoDialog {
public static void main(String[] args) {
JFrame frame = new JFrame("Frame");
frame.add(Box.createRigidArea(new Dimension(400, 300)));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
final JDialog dialog = new JDialog(frame, "Dialog", true);
// set binding
int condition = JPanel.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = ((JPanel) dialog.getContentPane()).getInputMap(condition);
ActionMap actionMap = ((JPanel) dialog.getContentPane()).getActionMap();
String enter = "enter";
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), enter);
actionMap.put(enter, new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
dialog.dispose();
}
});
dialog.add(Box.createRigidArea(new Dimension(200, 200)));
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
}
I would like to say first that 'Hovercraft Full Of Eels' solution is more elegant than this one and more closely in the spirit of the JDialog and Swing API. However, to offer an alternative here is a basic example of using a KeyListener on your JDialog that will do as you need without adding a button;
public class Test {
public static void main(String[] args) {
JDialog jd = new JDialog();
// Add and define the KeyListener here!
jd.addKeyListener(new KeyListener(){
#Override
public void keyTyped(KeyEvent e) {
// Nothing
}
#Override
public void keyPressed(KeyEvent e) {
// Nothing
}
#Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
JDialog d = (JDialog)e.getSource();
d.dispose();
}
}
});
// End key listener code
jd.setVisible(true);
}
}
The important/relevant code is between the two main comments. This is a compilable example, so you can copy paste this into a new file and run it to view the effects.
I have a JPanel form which contains a JList and some JButton.
The JPanel looks like this
When I click the Add List button, a separate JFrame form is displayed.
The JFrame form will look like this
When the add button on the JFrame is clicked, I need to add the value of the JTextfield (named List Name) to the JList on the previous JPanel. I wonder how to pass the value from the JFrame to the JPanel? Any suggestion would be appreciated.
Here is a code of the JPanel form (using Designer GUI)
package multimediaproject;
public class musicPlayerPanel extends javax.swing.JPanel {
public musicPlayerPanel() {
initComponents();
}
private void initComponents() {
//...here is the generated code by using designer GUI
}
// Variables declaration - do not modify
//..generated code
// End of variables declaration
}
Here is the code of JFrame form (using Designer GUI)
package multimediaproject;
public class addListFrame extends javax.swing.JFrame {
public addListFrame() {
initComponents();
this.setLocation(515, 0);
setVisible(true);
}
private void initComponents() {
//..here is the generated code by using Designer GUI
}
private void addBtnActionPerformed(java.awt.event.ActionEvent evt){
//some validation
if(...)
{
//validation
}
else
{
//IF VALUE IS CORRECT, ADD the List Name JTextfield value to the JList on the previous JPanel
errorMessage.setText("");
}
}
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new addListFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
//....generated code
// End of variables declaration
}
UPDATE with your code.
You can take advantage of PropertyChangeListener and PropertyChangeSupport (This classes implements Observer Pattern).
I give you an example you for guidance:
public class MusicPlayerPanel extends JPanel {
private JList list;
private JButton addButton;
private PropertyChangeListener listener = new MyPropertyChangeListener();
//..in some place
addButton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
JFrame form = new FrameForm();
form.addPropertyChangeListener(FrameForm.BUTTON_CLICKED,listener);
form.setVisible(true);
}
});
//in another place
private class MyPropertyChangeListener implements PropertyChangeListener{
#Override
public void propertyChange(PropertyChangeEvent evt){
if(evt == null)
return;
if(evt.getPropertyName().equals(FrameForm.BUTTON_CLICKED)){
String value = (String) evt.getNewValue();
((DefaultListModel)list.getModel()).addElement(value);
}
}
}
}
And the frame form like this:
public class AddListFrame extends JFrame{
private JTextField textfield;
private JButton submitButton;
public static final String BUTTON_CLICKED ="buttonClicked";
// in some place
submitButton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent evt){
firePropertyChange(BUTTON_CLICKED,null,textfield.getText());
}
});
}
Note: In java by convention, classes start with uppercase and follow a camel style. This is very important for readability.
OK.This is easy if i understand your problem.
Step 1:Create setter and getter for your JList object reference.
Step 2:On button click , when you open new JFrame pass your panel reference in constructor of class which inherits JFrame.
Step 3:By doing this you are able to call getter method on panel reference.
Step 4:Now you have reference of JList,do what you want.
I hope this is best solution of your problem
"when I click the add button, a separate jFrame form is displayed. The jFrame contain a jTextfield and a submit button."
Did you seriously create an entirely new JFrame for a JTextField and a JButton?!
Have you not heard of JOptionPane? That's exactly what you are trying to replicate. The only code you need is this:
String s = JOptionPane.showInputDialog(component, message);
model.addElement(s);
The first line will cover all your code for your custom JFrame.
Take a look at this example
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class JOPDemo extends JPanel {
JList list;
JButton button = new JButton("Add Name");
String name;
DefaultListModel model;
public JOPDemo() {
model = new DefaultListModel();
list = new JList(model);
setLayout(new BorderLayout());
add(list, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
name = JOptionPane.showInputDialog(this, "Enter a name");
model.addElement(name);
}
});
}
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
private static void createAndShowGui() {
JFrame frame = new JFrame();
frame.add(new JOPDemo());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Edit
What you can do is have an inner class which is your JFrame. Personally, I'd go with a JDialog though.
From comment: Have your frame as an inner class of your main class. That way it can access all the variables from your main class. You can have a String listName also in the GUI class. From the other frame when you hit add, it sets the listName in GUI class, then adds to the list.
public class GUI {
String listName;
JList list;
InnerFrame inner = new InnerFrame();
private class InnerFrame extends JFrame {
JButton addButton;
}
}
I am using Netbeans, and I want it so a text field is only editable when the user clicks the check box. I have it so when they select the check box it makes the text field editable, but how do I make it so when they de-select the check box the text field becomes un-editable again?
The code I used to make it editable is -
private void chk4By6MouseClicked(java.awt.event.MouseEvent evt) {
txt4By6.setEditable(true);
}
Use ItemListener, so that you can enable or disable the JTextField depending on if JCheckBox is SELECTED or DESELECTED respectively.
A sample program :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ModifyTextField
{
public static void createAndDisplayGUI()
{
JFrame frame = new JFrame("MODIFY TEXTFIELD");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
final JPanel contentPane = new JPanel();
final JTextField tfield = new JTextField(10);
tfield.setEnabled(false);
final JCheckBox cbox = new JCheckBox("Enable TEXTFIELD", false);
ItemListener itemListener = new ItemListener()
{
public void itemStateChanged(ItemEvent ie)
{
tfield.setEnabled(ie.getStateChange() == ItemEvent.SELECTED)
}
};
cbox.addItemListener(itemListener);
contentPane.add(cbox);
contentPane.add(tfield);
frame.getContentPane().add(contentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndDisplayGUI();
}
});
}
}
Outcome :
and
See: http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html
Read the whole thing if you really want to learn how this works.
Have you considered:
private void chk4By6MouseClicked(java.awt.event.MouseEvent evt) {
txt4By6.setEditable(!txt4By6.isEditble());
}
Or maybe even... (assuming chk4By6 is a swing checkbox)
private void chk4By6MouseClicked(java.awt.event.MouseEvent evt) {
txt4By6.setEditable(chk4By6.isSelected());
}
And a third method might be:
private void chk4By6ActionPerformed(ActionEvent evt) {
txt4By6.setEditable(chk4By6.isSelected());
}