-2

Am currently working on my end of the year project and i am building a hotel reservation system. So here is my Question i want to show the bookings a customer has i made the query in the server (it is a 3 tier application GUI-SERVER-Database). What i want do is create a Jtable whith columns

String[] columnNames = { "starting date", "ending date", "user ID", "amount of single rooms", "amount of double rooms", "amount of triple rooms", "amount of family rooms", };

and each row will be a list, from the below :

List myList4 = new ArrayList(); MY list of objects is : [[8, 2017-02-02, 2017-03-02, 3, 3, 0, 1, 0], [17, 2017-02-03, 2017-03-05, 3, 4, 5, 6, 7]];

i am very confused on what i have to do, i was thinging about just populating 8 JLists but it is looking horible, and i need the user to be able to select each row-Reservation so he can delete it or update it.

  • Please edit your question and show what you have attempted to do with a JTable. Until you do, your question is essentially “someone write all of my JTable code for me” which is likely to be closed as too broad. – VGR Mar 10 '17 at 14:36

2 Answers2

0

If you have a JTable, you most likely don't need any JLists... See e.g. How to add row in JTable? on how to populate your JTable.

You probably should read a tutorial on JTables to lessen your confusion... (e.g. the official tutorial at https://docs.oracle.com/javase/tutorial/uiswing/components/table.html)

Your last question/requirement likely qualifies as "too broad" and doesn't relate to the title, therefore just some hints: Implementing editing depends on your requirements. Either you can make your table cells editable or open a dialog for one entry on a double click on a (read only) row. The probably simplest hack would be to make the cells editable, add a column with a "delete" checkbox and write the whole table back to the database on a click on a "Save" button - but that's far from scalable and elegant.

Community
  • 1
  • 1
sruetti
  • 532
  • 2
  • 7
  • I do understand that i have asked in a very wrong way, the problem am facing is when i call the Jtable constructor JTable table = new JTable(listOfLists, columnNames); it tells me that i need to change listofLists to object[][] and then i dont know how to populate that.I will find a way for the rest but this is my issue . – thanos denkseropoulos Mar 10 '17 at 14:56
  • The constructor you mention wants an array of array of Object - actually what you have in your question. If your problem is to convert Lists into arrays you should have a look at the toArray() method on the List interface (or consult the answers to **that** question on SO). The first link I gave shows you how to use alternative constructors for JTable that might be more intuitive. – sruetti Mar 10 '17 at 15:20
0

MY list of objects is...

So you need to parse the data so you can add it to the table.

The basic code might be something like:

String columnNames = { "Starting Date", "Ending Date", ... };
DefaultTableModel model = new DefaultTableModel(columnNames, 0);

for (int i = 0; i < myList4.size(); i++)
{
    List row = myList4.get(i);
    model.addRow( row.toArray(...) );
}

JTable table = new JTable( model );
camickr
  • 321,443
  • 19
  • 166
  • 288