简体   繁体   中英

Fill combobox from a column in jtable

This is a school assigment and I am not allowed to use SQL. I have a JTable with the following columns:

Cruise_name;Ship_name;Departure_location;Departure_date

I am saving these items in a .txt file called cruising.txt which looks like this:

Atlantis;SOS FOR LOVE;Koper;1.1.2020;
Program;M.Hamilton;Neverland;1.1.2020;
Atlantis;Derpina;Lonely ;1.1.2020;
...
...

On a separate tab I need to populate a combobox with Cruise_name items I either get from that table directly (somehow) or make an ArrayList that reads the first item in every line from the .txt file. At this moment I don't know how to do either. I could really use some help, please!

The thing is that you're gonna need to read that file anyway. So my suggestion would be as follows: create a parser that reads from the file and creates objects of type Cruise (with your given attributes) - each line should result in another Cruise object. So the parser will return to you a list of Cruise objects.

Once you have this list, you can populate the table following this tutorial and in the same time populate your combobox following this tutorial .

I managed to do it like this... To fill up the combobox everytime i click on the tab.

private void jTabbedPane1MouseClicked(java.awt.event.MouseEvent evt) {

cbCruises.removeAllItems();

JTable table = this.tblCruises;

        int rowcount = table.getModel().getRowCount();
        for(int i = 0;i<rowcount;i++){
            cbCruises.addItem((String)table.getModel().getValueAt(i, 0));
        }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM