I have a jbutton ,which once clicked ,counts the clicks:

    int clicked ;
        private void jButtonScanActionPerformed(java.awt.event.ActionEvent evt) {                                            

       clicked++;
      }

My question is how to attach this click output(once the button is clicked ) to jtext field to see the click count?

   private void jTextFieldParcelIDActionPerformed(java.awt.event.ActionEvent evt) {                                                   

    }                                                  

Recommended Answers

All 21 Replies

Use the JTextField's setText method to display the clicked count. You can do that as part of your jButtonScanActionPerformed method

If i do with setText method,code complains about count,it is int count, but setText acepts string,will it work if to Convert count to string?

Yes, just convert the int to a String.
Shortest way is string concatenation because that converts automatically, eg ""+count
... but some people dislike that because it's a bit obscure, in which case the "official" ways are
Integer.toString(count) or String.valueOf(count)

Done and dusted. Would like to ask similar question: after jtext filed i have more jtext fields with labels,and their setVisible disabled in properties.But what i would like to achieve is: once the press the button,discussed above,id appears in jtext and now i would like the other jtext fields make enabled for editing.I tried to do something like this,but it does not enables the field for editing:

private void jTextFieldNameActionPerformed(java.awt.event.ActionEvent evt) {                                               
      if(jTextFieldParcelID.equals(count >0))
      {
          jTextFieldName.setEditable(true);
      }
    }  

Line 4 is OK, but line 2 looks dodgy
(count > 0)is a boolean expression but I guess jTextFieldParcelID is not a Boolean, so they are never equal, so line 4 is never executed

I have managed to fix that problem just via properties disabled editable(tick). and in the button action,once it,s pressed i did setEditable(true) -for the field i want to be editable.
Another question: The Parcel
ID then appears in the “Parcels By County” list. All packages will be added to this list.
So how to add entered values to be in the JList,but only id displayed?
Untitled.png

So at the moment i have created this ,but stuck on the editing value from the jTextFieldParcelID in ArrayList to JList

private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {                                           

      ArrayList<JTextField> list = new ArrayList<>();
       list.add(jTextFieldParcelID);
      list.add(jTextFieldName);
      list.add(jTextFieldAddress);
      list.add(jTextFieldCity);
      list.add(jTextFieldConatcNumber);
      list.add(jTextFieldDescription);
      list.add(jTextFieldPrice);




    }                                          

Sorry, I don't understand what you are trying to do. What is the purpose of your list of JTextFields? How do you imtend to use that?

This is simulation of post office programm,once the button scan clicked the the id is generated in parcelid field.And then add button becomes active.I enter info in the fields provided and once press add button the info (values )from the fields goes to jlist,but only id is showable in jlist on the right.
and only once press display all this that must be shown.So would appreciate if you help me at least to load entered info inot that list and display id.
Untitled.pngUntitled.png
RE:the list you asked about i just was thinking of some kind of storage /container for the values from the jtext field,i migth be wrong.But how elese to link the values from the fields to the jlist for storage?

OK
It looks like you should have a class that encapsulates a Parcel with all its data fields. That's how you implement a "storage/container" in Java. You can create an instance of the class from the data in the text fields. (NB: that's the text in those fields, via getText(), maybe converted to ints or whatever,, not the fields themselves.
They can go into a list of Parcel objects.
You can add those Parcel objects to your JList. By default, the JList will call toString() on each object to get the text to display, but you can override that to just display the ID by using a ListCellRenderer (although it may look better to display more info than that, in which case just implement toString in your Parcel class to return a suitable string for display.)

Thank you .Is there similar examples online to see?

Here's a very simple example. Experiment with somethimg like this:

// container for info about one person
class Person {
   String name;
   int age;
   public person(String name,int age) {
     this,name = name;
     this.age = age;
   }
   public String toString() {
     // controls how a Person will be displayed
     return name + " is " + age;
   }
}

...

// create new person using data from entry fields
// then add the person to a suitable list
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
list.add(new Person(name, age));

Ok,i see now. In your scenario list is a JList?

No, it was the list that you had in your previous code.

Adding items to a JList goes like this:
JLists keep their data in a ListModel (that's defined as an interface). You can write your own, but Java gives you a sensible one called DefaultListModel. You can add and remove stuff from the ListModel and the JList will update itself accordingly automatically. Use it like this:
Create a list model, create the JList using that model, add stuff to the model

DefaultListModel myListModel = new DefaultListModel();
JList myList = new JList(myListModel);
myListModel.add(new Person(name, age)); // as required

How the stuff works with neatbeans- i create class Person after class newJframe i have now and neatbeans wants it to be outer,so i generate class separately do the stuff needed inside,but in main NewJFrame just make it as implements Person?

No, Person isn't an interface, so you can't implement it.
Person is just another class like String or JFrame, except you wrote it not Sun/Oracle. Provided its in the same package as the rest of your code you just use it like you would use String (you probably haven't done packages yet, in which case they will be in the same default package, and don't worry).
Of course you want a Parcel class, not a Person class. That code was just an example to help you understand how it works; you can't use it literally in your app.

OOO ,yes i know what you mean,regarding the class.It just confused me working with jforms and console app,to create class.I will give it a go,and tell later about success.

Getting an error on line 12(last line)-"no suitable method found for addPackage" and i have class Package :

    private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {                                           
           ArrayList<JTextField> list = new ArrayList<>();
       list.add(jTextFieldParcelID);
      list.add(jTextFieldName);
      list.add(jTextFieldAddress);
      list.add(jTextFieldCity);
      list.add(jTextFieldConatcNumber);
      list.add(jTextFieldDescription);
      list.add(jTextFieldPrice);
  String name = jTextFieldName.getText();

list.add(new Package(name));

That ignores eveything from our last few posts. Go back and read them all again please.

Will it make any diffrence to join class to jlable if i use drug and drop elements to jframe?

Depends on what you drag and where you drop it!
While you are learning I strongly advise you NOT to use D&D to create your programs. You will never learn how to code for yourself that way, and you will not be able to understand what code is being generated or why. Learn how to code it first, then look at tools that MAY help your productivity (I have never found a drag & drop development tool that actually speeds up the final development of a real application).

How to add space in JOptionPane.showMessageDialog ? I have it like this

JOptionPane.showMessageDialog(null,  parcelsList);

This displays array from arraylist.Once i have multiple items in the array,they are all line by line with no space.
and my second question how to output nicely with no array brackets in the JOptionPane.showMessageDialog output ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.