0

i want to update the JTable date by mouse selecting the cell and input the new data inside the JTextField and using a button to update? i am using Vector method, i stuck this problem in 2 weeks but i still not foung the solutions. Please help!

public  class GUI  extends JFrame {
    private JPanel contentPane;
    private JTable table;
    private JTextField textField;contentPane = new JPanel();

public GUI() throws Exception { 

    contentPane.setBackground(Color.LIGHT_GRAY);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    Vector<Vector<Object>> data = new Vector<Vector<Object>>();

    Data entry = new Data();
    Attributes[] x = new Attributes[100];
    for(int i = 0;i<x.length;i++){
        x[i] = new Attributes();
    }
    Data.loadxml(x);


    for(int i = 0;i<x.length;i++){
    Vector<Object> row = new Vector<Object>();
    row.add( x[i].getbrand());
    row.add( x[i].getbuyCurrency());
    row.add( x[i].getbuyPrice());
    row.add( x[i].getcategory());
    row.add( x[i].getid());
    row.add( x[i].getleadTime());
    row.add( x[i].getminOrderQuantity());
    row.add( x[i].getname());
    row.add( x[i].getsellPrice());
    row.add( x[i].getstockOnHand());
    row.add( x[i].getstockOnOrder());
    row.add( x[i].getsubCategory());
    row.add( x[i].getsupplier());
    row.add( x[i].gettargetBatchVolume());
    row.add( x[i].getvolume());
    data.add(row);
    }

    Vector<Object> headers = new Vector<Object>();
    headers.add( "Brand");
    headers.add( "Buy Currency");
    headers.add( "Buy Price");
    headers.add( "Category");
    headers.add( "ID");
    headers.add( "Lead Time");
    headers.add( "Min Order Quantity");
    headers.add( "Name");
    headers.add( "Sell Price");
    headers.add( "Stock On Hand");
    headers.add( "Stock On Order");
    headers.add( "Sub Category");
    headers.add( "Supplier");
    headers.add( "Target Batch Volume");
    headers.add( "Volume");
    contentPane.setLayout(null);

    JTable table = new JTable(data, headers);
    table.getSelectedRow();
    table.getSelectedColumn();
    table.setBounds(12, 13, 1020, 500);
    resizeColumnWidth(table);
    resizeColumnWidthByHeader(table);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setAutoCreateRowSorter(true);
    TableColumn col=table.getColumnModel().getColumn(0);
    contentPane.add(table);


    JScrollPane scrollPane = new JScrollPane(table);


    scrollPane.setBounds(12, 109, 1391, 771);
    contentPane.add(scrollPane);

    JButton btnSubmit = new JButton("Submit");
    btnSubmit.setFont(new Font("Yu Gothic UI Semilight", Font.PLAIN, 15));
    btnSubmit.setBounds(1155, 53, 99, 27);
    contentPane.add(btnSubmit);

    textField = new JTextField();
    textField.setBounds(886, 56, 219, 25);
    contentPane.add(textField);
    textField.setColumns(10);
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • 1
    You need to set the `ActionListener` for your `JButton`otherwise it doesn't know what to do (https://docs.oracle.com/javase/8/docs/api/javax/swing/AbstractButton.html#addActionListener-java.awt.event.ActionListener-). I can't see this anywhere in your code. – d.j.brown Nov 09 '16 at 10:47
  • 1
    Your button's listener can invoke the table's `setValueAt()` method. – trashgod Nov 09 '16 at 11:09
  • 1) `contentPane.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) I can't see any reason in this case to extending frame. Just use an instance of a standard frame. 3) For better help sooner, post a [MCVE]. – Andrew Thompson Nov 09 '16 at 14:20

0 Answers0