Results 1 to 4 of 4
  1. #1
    MS-POWER is offline Member
    Join Date
    Oct 2016
    Posts
    24
    Rep Power
    0

    Default How to insert data from JcomboBox to JTable directly

    Hi every one

    I need help please ...

    I want to insert data in Jtable row by selecting item from Jcombobox directly, without creating textfield in my JFrame ...
    ,

    at present in my code , I selected Item from combobox then get value from database then set value to JTextField then I insert it in row.

    how to get data and insert it in row directly without show it in TextField first .

    ......................

    private void jComboBox7ServicesActionPerformed(java.awt.event.A ctionEvent evt) {


    if(jComboBox5EMPLOYEES.getSelectedItem() != ("Select Employee"))
    {

    String anyname = (String) jComboBox7Services.getSelectedItem();
    String sql = "select* from services where Service_Name_English =?";
    try {
    pst = conn.prepareStatement(sql);
    pst.setString(1, anyname);
    rs = pst.executeQuery();
    if (rs.next()) {



    String add1 = rs.getString("Service_Name_English");
    jTextField5.setText(add1);
    String add2 = rs.getString("Service_Number");
    jTextField6.setText(add2);
    String add3 = rs.getString("Service_Price");
    jTextField7.setText(add3);

    DefaultTableModel model =(DefaultTableModel)jTableInvoiceItems.getModel();
    model.addRow(new Object[]{jTextField5.getText(),jTextField6.getText(),jText Field7.getText()});


    }

    } catch (Exception e) {

    } finally {
    try {

    rs.close();
    pst.close();

    } catch (Exception e) {
    }

    }
    }

    else{

    JOptionPane.showMessageDialog(this,"Select Employee First " );

    }


    thanks

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: How to insert data from JcomboBox to JTable directly

    Why don't you use that add1, add2 and add3 variable directly in the addRow() method then?
    Java Code:
    String add1 = rs.getString("Service_Name_English");
    String add2 = rs.getString("Service_Number");
    String add3 = rs.getString("Service_Price");
    ...
    model.addRow(new Object[]{add1, add2, add3});
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    MS-POWER is offline Member
    Join Date
    Oct 2016
    Posts
    24
    Rep Power
    0

    Default Re: How to insert data from JcomboBox to JTable directly

    Quote Originally Posted by SurfMan View Post
    Why don't you use that add1, add2 and add3 variable directly in the addRow() method then?
    Java Code:
    String add1 = rs.getString("Service_Name_English");
    String add2 = rs.getString("Service_Number");
    String add3 = rs.getString("Service_Price");
    ...
    model.addRow(new Object[]{add1, add2, add3});

    thanks a lot

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to insert data from JcomboBox to JTable directly

    Occasionally, older classes don't adapt to newer capabilities. Like the table model not taking
    variable arguments in add row. Sometimes, I make a helper method to repackage the arguments
    (this may not be quicker to type but I think it looks cleaner).

    Java Code:
    import java.util.Arrays;
    
    public class HelperDemo {
       public static void main(String[] args) {
          Model model = new Model();
          addRow(model, "alpha", "beta", "gamma");
          addRow(model, "foo", "bar");
       }
    
    // helper method
       private static void addRow(Model model, Object... obj) {
          model.addRow(obj);
       }
    }
    
    class Model {
       public void addRow(Object[] ob) {
          // do something with objects
          System.out.println(Arrays.toString(ob));
       }
    }
    So my static method takes the model instance and a variable number of entries and
    invokes the actual method.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. how to insert data into jTable in netbeans
    By suhailt in forum NetBeans
    Replies: 13
    Last Post: 03-26-2014, 07:09 PM
  2. Add New Row Then Insert a data into JTable
    By mcajavaprogramer in forum AWT / Swing
    Replies: 2
    Last Post: 04-25-2012, 07:00 AM
  3. insert data from text field to jtable
    By mackinas in forum New To Java
    Replies: 2
    Last Post: 06-09-2010, 04:30 AM
  4. Replies: 0
    Last Post: 02-21-2010, 11:51 AM
  5. Replies: 3
    Last Post: 02-28-2009, 09:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •