0

I have a table which has only field names and no data, I want to enter data from inputs by the user, how do I do that?

My actual program is too long, so this is a mini program i wrote to post here.

Example of things i want to add into the table is this: {"men's clothings","RM 5",input,total}

input is from the first button, total is from my setter getter file in my actual program

I wanted to use a List but it seems that it is uncompatible with my Object[][] order.

What i want to achieve is generating an order list after user choosing the items from the buttons.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    class test implements ActionListener
    {
private Object[][]order={   };  // I need to add data here in order to put 
                                         //in the JTable

private JFrame f;               // for 1st frame with the buttons
private JTable table;
private JScrollPane pane;
private JButton button1;    
private JFrame f2;              //pop up window after button2 
private JButton button2;
private String input;           //input when press button1

    public test()
    {

    button1=new JButton("press here first");
    button2=new JButton("press here after above");

    //first frame config

    f=new JFrame();
    f.setSize(500,500);
    f.setVisible(true);
    f.setLayout(new GridLayout(2,0));
    f.add(button1);
    f.add(button2);


    button1.addActionListener(this);
    button2.addActionListener(this);
     }

    public static void main(String args[])
{
test lo=new test(); 
}


public void actionPerformed(ActionEvent e)
    {

  if(e.getSource()==button1)    // for the first button
        {
    input=JOptionPane.showInputDialog(null,"how many do you want?");
            //user input on the quantity
        }

  if(e.getSource()==button2)
     {
    window2();
     }

    } 


public JFrame window2()         //second window config
     {
    String[] title={"item","Price","Qty","total"};
    table=new JTable(order,title);
    pane=new JScrollPane(table);

    f2=new JFrame();
    f2.setSize(500,500);
    f2.setVisible(true);
    f2.setLayout(new FlowLayout());
    f2.add(pane);
    f2.pack();

    return f2;
     }

    }
4
  • Ok I'm confused here. Are you having trouble storing the information in order[][] or creating the GUI panes to allow for user-input? Please reword the question and realign your code so that it is much easier to follow. Commented Jun 16, 2013 at 4:50
  • 1
    When posting code here, it's usually a good idea to post well formatted code, code with reasonable and regular indentation so that those helping you don't have to struggle to read and understand it. Your efforts to fix this will be greatly appreciated by all. Commented Jun 16, 2013 at 4:54
  • @Tdorno i have problem in adding items to order[][], I want to add items in it for my JTable, but now camickr showed me another way to do it Commented Jun 16, 2013 at 6:49
  • 1
    @HovercraftFullOfEels thanks for the comment, i will learn how to indent correctly next time. Commented Jun 16, 2013 at 6:50

1 Answer 1

3

You should be creating your table as follows:

DefaultTableModel model = new DefaultTableModel(title, 0);
JTable table = new JTable( model );

This will create a table with just the heading and 0 rows of data.

Then when you want to add a new row of data you would use:

model.addRow(...);

You can add data to the DefaultTableModel as a Vector or an Array.

If you want to use a List then you need to use a custom TableModel model. You can check out the List Table Model.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.