JCombobox should show the ID list and display the results
-
So far, I have a program that contains home data that are for sale. I can change the data by pressing the button.
edit
and then keep them. Also calleddialog
There is an opportunity to add a new home with data to the main intraffic and to remove the house accordingly.Houses that have already been retained in the programme have in the first place included data
two dimension array
thus:String[][] records = {{"1", "113 The Maltings", "Dublin 8", "2", "1", "155500.00", "House1.jpg", "(087) 9011135"}, {"2", "78 Newington Lodge", "Dublin 14", "3", "2", "310000.00", "House2.jpg", "(087) 9010580"}, {"3", "62 Bohernabreena Road", "Dublin 24", "3", "1", "220000.00", "House3.jpg", "(087) 6023159"}, {"4", "18 Castledevitt Park", "Dublin 15", "3", "3", "325000.00", "House4.jpg", "(087) 9010580"}, {"5", "40 Dunsawny Road", "Swords", "3", "19", "245000.00", "House5.jpg", "(087) 9011135"} };
I have a array:
ArrayList<House> houseList = new ArrayList<House>();
And here's the designer in the driveway class:
public HouseApplication() { super("Estate Agent Application"); for (int i = 0; i < records.length; i++) { houseList.add(new House(Integer.parseInt(records[i][0]), records[i][1], records[i][2], Integer.parseInt(records[i][3]), Integer.parseInt(records[i][4]), Double.parseDouble(records[i][5]), records[i][6], records[i][7])); } currentItem = 0; initComponents(); }
I can't handle the next assignment. ID generated independently and not produced
hardcoded
♪ I can't put it in the designer just like that.variable
e.g.:int id,
... It is further necessary to open a dialogue in whichJCombobox
ID-generated houses that are already in the system. So far, there's work, but when I choose ID, and I press the ok, what I chose is not displayed.String [] id = new String[records.length]; for (int i = 0; i < records.length; i++) { id[i] = records[i][0]; }
String i = (String) JOptionPane.showInputDialog( HouseApplication.this, "Choose house id: ", "ID", JOptionPane.PLAIN_MESSAGE, null, id, records); int ID = Integer.parseInt(i);
And in the end, when I'm from
JComboBox
Choose the right thing for me.ID
And pushing for OK, the program has to open the exact one.record
on which there is relevantID
♪UPDATE 1
That's my choice.
And even if I'm picking up any meaning and pushing on the ok, it still doesn't open what I chose.
It turns out that if ID's normal, I can try to create another class. When I create this class and write the code you gave me,Objects.requireNonNull( house );
makes the following mistake.Objects cannot be resolved
♪
What needs to be done to solve this problem?
-
Method:
JOptionPane.showInputDialog( Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue): Object
The last two parameters accept the range of values from which the user will choose and the default value chosen (in the case of the deleting list, the element to be shown at the opening of the window).
In your case, there's a list.
houseList
I'm guessing,List<House>
and we need to pick one of his elements.House chosenOne = (House)JOptionPane.showInputDialog( HouseApplication.this, "Choose now...", "The Choice of a Lifetime", JOptionPane.QUESTION_MESSAGE, null, houseList.toArray(), // преобразуем список в массив null // элемент по-умолчанию не важен ); if ( chosenOne == null ) return; //пользователь нажал "Cancel"
int id = chosenOne.getId(); // или как там у вашего объекта взять ид
Important: text in the drop-off element is taken by method
toString
(i.e.House.toString()
the default element is compared with the list elements by the challengeequals
♪If method
house.toString()
returning the unsuitable line, you can turn the objects.House
in its facility:class HouseWrapper {
House house;
public HouseWrapper( House house ) {
Objects.requireNonNull( house );
this.house = house;
}public House house() { return house; } @Override public String toString() { // возвращает строку, использующую поля House в нужном формате return house.getName() + " [" + house.getPhone() + "]"; } @Override public boolean equals( Object other ) { if ( other == null ) return false; if ( other == this ) return true; if ( !HouseWrapper.class.equals( other.getClass() ) ) return false; return Objects.equals( house, ((HouseWrapper)other).house ); } @Override public int hashCode() { return house.hashCode(); }
}
Use:
searchItem.addActionListener( event -> {
// преобразуем список House в массив HouseWrapper
HouseWrapper[] choices = houseList.stream()
.map( HouseWrapper::new )
.toArray( HouseWrapper[]::new );HouseWrapper chosenOne = (HouseWrapper)JOptionPane.showInputDialog( null, "Choose now...", "The Choice of a Lifetime", JOptionPane.QUESTION_MESSAGE, null, choices, null ); if ( chosenOne == null ) return; House chosenHouse = chosenOne.house(); // что-то сделать с выбором пользователя
}