I have a JList that becomes populated from click on buttons.

My question that i need help with is, is it possible to use a function of the JList to get the string from a selected index and make a substring?

thanks.

Recommended Answers

All 5 Replies

I know that there is a getselectedvalue because i have a working add and remove buttons for the Jlist.

Where i am stuck is how to take the string from the selected point and make a substring from it.

When you added elements to your JList, what kind of elements did you add? What was their type? Because if you added Strings and you know that method, why don't simply take the selected value and do whatever you want.

String s = (String)list.getSelectedValue();
if (s!=null) {
  // do what you want with the String.
}

My addedElement was (Bananas.........$3.00)
I tried what you said and i'm not sure whats wrong with it because it still compiles but number isn't updated.

here is my code for what i want. let me know what looks wrong.

public void dotheRemove()
{
    s = (String)tallyList.getSelectedValue();
    substring = s.substring(70);

    removedDouble = Double.parseDouble(substring);

    subTotal = subTotal - removedDouble;
    subTotalMathLabel.setText("$"+ subTotal);



}

The code should do what you tell it to do.Try to add some System.out.printlns between the commands in order to see the values of the variables. Try to print all the variables that you have and then after you take the value and change the subtotal, print them again in order to debug.
Print every variable as soon as you get it and see what happens.

s = (String)tallyList.getSelectedValue();
System.out.println("s="+s);
substring = s.substring(70);
System.out.println("substring="+substring);
....

The text's value should be updated, provided of course that the code runs with no exceptions. Do you have try-catch statements? If you do, do you print the exceptions that you get?

Also your the whole logic is wrong. What if instead of "Bananas.........$3.00" you have something else:
"Apples.........$4.00"
Then the substring will not work. Remember, I asked what type of object you add at the list. The add method and the get method of the list take as argument an Object, not a String. The String is an Object so the code works. But it means that you can also do this:

EXAMPLE - USE YOUR OWN NAMES AND CLASS DEPENDING ON WHAT YOU WANT TO DO.

class FruitBasket {
  private String fruit = null; 
  private double price = 0.0;

  public FruitBasket() {
     // constructor
  }

  // get, set methods

  // PAY ATTENTION TO THIS:
  // toString is a method inherited by the super class: java.lang.Object and you override it:
  public String toString() {
    return fruit + "......$" + price;
  }
}
FruitBasket fb = new FruitBasket();
// call set methods to set the fruit name and the price
// add the fb to the list

When you add an object to the list, the whole object is added not just what you see. What is displayed is always what the toString method returns, that is why you override it in order to be displayed whatever you want. But when you take it, you have access to all of its attributes:

FruitBasket temp = (FruitBasket)tallyList.getSelectedValue();

double removedDouble = temp.getPrice();
System.out.println(removedDouble+", "+subTotal);

subTotal = subTotal - removedDouble;
subTotalMathLabel.setText("$"+ subTotal);
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.