简体   繁体   中英

Questions Combobox in a JTable

I have a question regarding an issue that arose while programming something for school.

We would like to make a summary, from Monday to Sunday, where the boss of a small company can see his employees and where he can assign dayparts to those people. We used a JTable for this. In every cell we would like to implement a Combobox with the dayparts (Morning, afternoon, evening).

So when clicking on a cell, we want to be able to click on the combobox that shows up, select a daypart, and then we want to want to be able to put the name, they asigned day, and the daytime, in a database. (For now, putting them into variables or anything would be fine, we'll put it in the database ourselves.)

This is a screenshot of our GUI so far: Link to our GUI

So far, so good. We didn't put any effort really in the looks of the GUI, so please bear with me while we focus on the functionality =). Our only problem is that we need the program to know what date and what person is linked to the cel that's being altered, so that we can put the name, date and the new daypart in a database.

We tried this: We used a clicklisteren when you clicked on a cel in the list. We put some code in that method and it was able to see the coordinates, and therefore get the name and the date. That worked. Then we tried to implement the combobox. We succeeded there too. However, when the cel becomes a combobox, the click method wont be used any more.

So here is our question: Is this a proper way of getting what we want? If so, how can we fix the list so we will be able to find out what the name and the date is, when changing the combobox, so we can put this in a database? If not, what should we change? What kind of other listener or other codes should we use instead to get our goal?

Here is some of our code:

    private javax.swing.table.DefaultTableModel model; //table model instantieren --teminste, straks.
    model = new javax.swing.table.DefaultTableModel(); //Hier wordt de tabel gedefinieerd.


    //We make the columns here:
    model.addColumn("Naam werknemer:");                                    
    model.addColumn("Maandag");
    model.addColumn("Dinsdag");
    model.addColumn("Woensdag");
    model.addColumn("Donderdag");
    model.addColumn("Vrijdag");
    model.addColumn("Zaterdag");
    model.addColumn("Zondag");

    //We make the combobox here:
    myCombo = new JComboBox(); 
    myCombo.addItem("-"); //not planned
    myCombo.addItem("ochtend"); //add items
    myCombo.addItem("middag");
    myCombo.addItem("avond");

   //This puts our combox in every cell of the first column
    TableColumn tableColumn = jTable1.getColumnModel().getColumn(1);


    //mouseclick listener
    jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            jTable1MouseClicked(evt);
        }
     });
    //actionlisteneren bij action performed
    myCombo.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jComboBox1ActionPerformed(evt);
        }
    });


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

            Point pnt = evt.getPoint();
            int row = jTable1.rowAtPoint(pnt);                  //This is the X 
            int col = jTable1.columnAtPoint(pnt);               //this is the Y -> Date
            String columnpje = ""+col;
            naambijcel = ""+jTable1.getValueAt(row,0);          //This variable will hold a name


            if (columnpje.equals("1")){
                datumbijcel = planningbeheermanager.getColumnArray().get(0); 
            }else if (columnpje.equals("2")){  
                datumbijcel = planningbeheermanager.getColumnArray().get(1);
            }else if (columnpje.equals("3")){       
                datumbijcel = planningbeheermanager.getColumnArray().get(2);    
            }else if (columnpje.equals("4")){
                datumbijcel = planningbeheermanager.getColumnArray().get(3);
            }else if (columnpje.equals("5")){
                datumbijcel = planningbeheermanager.getColumnArray().get(4);
            }else if (columnpje.equals("6")){
                datumbijcel = planningbeheermanager.getColumnArray().get(5);
            }else if (columnpje.equals("7")){
                datumbijcel = planningbeheermanager.getColumnArray().get(6);
            }

            //System.out.println(obj1);
            //System.out.println(row +" "+ col);
}

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // selectbox ding ("" zorgen ervoor dat het object in een string kan worden gezet.
    System.out.println("Dagdeel: "+myCombo.getSelectedItem());
    System.out.println("Naam: "+naambijcel);
    System.out.println("Datum: "+datumbijcel);
    }  

If you would like to see more of our code or if you have any other questions, then please feel free to ask!

I can't be completely sure, but to me, it looks like you are not properly using the TableModel. The operations you do in your jComboBox1ActionPerformed should actually be performed in setValueAt() .

Maybe, extending AbstractTableModel would be a better fit than the DefaultTableModel, but again, can't be sure without the complete code.

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