I inserted a text field and now I just want it to say "working" within the text field. I do not have any errors, but when the display comes up the text field is empty. The first line of the code below was generated by netbeans. I wrote the second line.
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("working");
}
Looks like you are trying to do that in the wrong method --> jTextField1ActionPerformed
try moving this to a OnClickEvent or in the initComponents of the App
jTextField1.setText("working");
Related
I have a text area that replicates a console output in my GUI. Id like for the user to select highlight and copy the output but not allow them to insert. Is there a way this could be done?
Right now I have a simple textArea editor and Ive tried terminalText.setDisable(true);. While this disables userinput it completely disables users from highlighting text as well.
I set my terminal text doing the following:
public void printToConsole(String s){
consoleBuilder.append(s);
terminalText.setText(consoleBuilder.toString());
}
I found that this disables input; however I am now unable to enter text using setText() method above and I cannot highlight:
terminalText.setTextFormatter(new TextFormatter<String>((Change c) -> {
return null ;
}));
I am building gui in netbeans... Or trying to is more like it. I keep getting an error in my code in the gray area that netbeans adds which evidently is a area that I can not edit.
it currently says this...
answerBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Answer Call" }));
answerBox.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
}
public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
answerBoxPopupMenuWillBecomeInvisible(evt);
}
public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
}
});
the error is on this line
answerBoxPopupMenuWillBecomeInvisible(evt);
In order to fix it when it says I need to remove answerBox from the beginning of the line, and I don't recall even adding a popupmenu in the first place so I am trying to figure out how to
hot to edit this line of code to see if it fixes my problem?
how can I delete things like this from the designer mode if they are giving me problems?
It appears that the event method answerBoxPopupMenuWillBecomeInvisible may no longer exist.
You could go into the "Events" tab of the property sheet and remove the associated event.
Failing that, you could open the file in something like NotePad++ and simply remove the entire listener registration code block
I wrote a autocomplete combobox program in which I search for the words entered by the user inside a file. The program works fine, however, the combobox editor doesn't return anything when something is typed in it. I don't know why is that.. Here is the chunk of code that deals with the problem.
// in GUI class constructor
InstantSearchBox = new JComboBox();
InstantSearchBox.setEditable(true);
/*****/
KeyHandler handle = new KeyHandler();
InstantSearchBox.getEditor().getEditorComponent().addKeyListener(handle);
// Keylistener class (KeyPressed method)
try
{
dataTobeSearched = InstantSearchBox.getEditor ().getItem ().toString ();
// the string variable is empty for some reason
System.out.println ("Data to be searched " + dataTobeSearched);
}
catch (NullPointerException e)
{
e.printStackTrace ();
}
Regards
Don't use a KeyListener. The text typed has not beeen added to the text field when at the time a keyPressed event is generated.
The better way to check for changes to the text field is to add a DocumentListener to the Document of the text field. See the section from the Swing tutorial on How to Write a Document Listener for more information.
You should use dataTobeSearched = (String) InstantSearchBox.getSelectedItem();
Despite its name, for editable comboboxes, this method just returns what text is entered.
The editor is only used internally by JComboBox to temporarily capture the input as they are typing. Once they have typed, the editor is cleared down and the text transferred back to the combobox model.
This allows editors to be shared amongst multiple comboboxes all at once - they just jump in when they are needed, capture input, jump back out again and clear down when editing is finished.
Use InstantSearchBox.getSelectedItem() instead of InstantSearchBox.getEditor().getItem().
I am creating a TextBox dynamically ,I get the values from server ,values could be 5,some times 10 etc , so No of textboxes i create will be different..
After all textboxes created .
There is an update button in page , When i click on this update button , Whatever changes user may have entered in any of the textBox,should Need to go to the server .. That I am not able to do ..
below is the code where i create the textBoxes
public void fetchData(){
public void onSuccess(ArrayList<Details> result) {
for(int i =0;i<result.size();i++){
name = new TextBox();
name.setText(result.get(i).getName());
verticalPanel.add(name);
namesList.add(name);
}}
Suppose name value is at this time : admin
Now user goees to the UI and change admin to adminNew
then click update button
Here what i do on update Button
public void Update(){
for(int i =0;i<namesList.size(); i++){
String updatedNanme = namesList.get(i).getText());
}
}
Now how will I get the updated name which user have changed from UI(i.e adminNew), in updatedName field.
Right now i am getting the old name(i.e admin) which i got from fetchData Method.
thanks
You should get textbox's value by calling getText(), not the "old value". I think there's a problem with your textbox list. maybe instantiate the textbox two times!
I suggest you debug your code, put a breakpoint on when textbox is instantiated and when the update occures. See if both are the same instance (for example if you're using eclipse watching a variable, id of the variable is shown in front of it. this is the object's id for VM. check if id of textbox that was instantiated is the same as id of textbox you are getting value from)
Could it be possible that you override the content of text box with the user entry ? Your update method will always set updatedNanme to the content of the last element from your namesList array.
And how do you access the updatedNanme string ? This string is only available in the for loop ?
I'm new to Java programming and am facing a (most likely) easy problem that I don't seem to be able to get across nor understand.
I have three different java files, one where I create an interface (SimulatorGui.java), other where I am creating a panel to use on the jTabbedPanel created in the interface (CollisionPanel.java - CollisionPanel class) and a third one, where I run a code that will create the output needed (Collision.java - Colision class).
In the Collision.java main method, I am doing the following:
public static void main (String[] args) {
//<editor-fold defaultstate="collapsed" desc="Simulation start procedures">
Tally statC = new Tally ("Statistics on collisions");
Collision col = new Collision (100, 50);
col.simulateRuns (100, new MRG32k3a(), statC);
//</editor-fold>
new SimulatorGUI().setVisible(true);
CollisionPanel update = new CollisionPanel();
update.updatepanel();
The first block, will create the desired output. I then want to send that output to the updatepanel! I am not passing any arguments to the method as I am still trying to debug this. updatepanel method is created in the file CollisionPanel as following:
public void updatepanel(){
System.out.println ("debug");
jTextArea1.setText("update\n");
}
What happens then is that when I run the Collision.java file it will output the "debug" text but won't set the text to the jTextArea1 (append doesn't work aswell). I then created a button to try and do so and in that case it works. In CollisionPanel.java:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
updatepanel();
}
This does the trick! I've searched and tried different things but can't seem to understand why this won't work.
Thanks in advance for your help, hope I've made the problem clear!
Okay I think I've eventually got the problem, and it is because of the IDE, you see in your main method you initiate a new CollisionPanel, which is wrong, netbeans has already added and initiated that panel in the SimulatorGUI, so now what you need to do is add a get method in the SimulatorGUI to get the initiated panel, then call the update method on that panel.
So add this to SimulatorGUI:
public CollisionPanel getCollisionPanel1() {
return collisionPanel1;
}
replace your old updatePanel() method with:
void updatepanel(String str) {
System.out.println ("debug");
jTextArea1.setText(str);
// jTextArea1.revalidate();
jLabel1.setText("test");
}
after that change your main too look like this:
SimulatorGUI simulatorGUI = new SimulatorGUI();
simulatorGUI.setVisible(true);
CollisionPanel cp=simulatorGUI.getCollisionPanel1();
cp.updatepanel("Hi");
and dont forget to remove the old updatePanel() method call from your CollisionPanel constructor, because now you can simply call cp.updatePanel("text here"); in your SimulatorGUI class instead of calling it only in the constructor.
I hope this is easy to grasp, if you're unsure let me know
Where do you add your CollisionPanel to the main GUI? I fear that this is your problem, and that you need to do this for your code to work. In fact where do any of your three classes get a reference to the others? For different classes to work in a program, there must be some communication between them. Understand that if you create a CollisionPanel object inside the GUI, and create another CollisionPanel object inside of the main method, calling a method on one object will have no effect on the other since they are two completely distinct entities.
For example, this code:
new SimulatorGUI().setVisible(true);
CollisionPanel update = new CollisionPanel();
update.updatepanel();
It appears that you are in fact calling updatePanel() on a CollisionPanel, but it's not on any CollisionPanel that is visualized in your GUI.
Consider giving SimulatorGUI a method that allows one to pass the CollisionPanel into it so that it can use it. This may in fact be a constructor parameter:
CollisionPanel update = new CollisionPanel();
SimulatorGUI simulatorGUI = new SimulatorGUI(update);
update.updatePanel();
Meaning SimulatorGUI's constructor would have to look something like:
public SimulatorGUI(CollisionPanel update) {
this.update = update;
// add update to GUI somewhere
}
There are three different levels when developping a GUI:
The view: the graphical component
The Model: the code that you run
The controller: checks if are update on the model in order to refresh the view.
So When you first start your program, the view will have the value assigned in the code; for instance say you created your JTextArea with the initial value type here. The view will show the JTextArea with the text type here.
When a change is made to the model, the view is not aware of it, it is job of the controller to check for update on the model and then refresh the view.
So this:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
updatepanel();
}
Will generate an event that say a property has been modify. So the controller will update the view.
Other than that, the change will not appear on the view.
Hope this helps..