How do i get my data from a 2d array and add it to a JTable
hi,
I have some textboxes that users will be able to enter information into. When they click ADD, the information is supposed to go into a JTable. I am having difficulty figuring out how to add this info from the textboxes to the 2d array and how to then add the info to the JTable. Here is my code:
Code:
public class Buttons extends JPanel
{
JPanel panel = new JPanel();
JFrame frame = new JFrame();
private JButton addButton;
private JButton modifyButton;
private JButton deleteButton;
public Buttons()
{
super();
addButton = new JButton("Add");
modifyButton = new JButton("Modify");
deleteButton = new JButton("Delete");
panel.setLayout(new FlowLayout());
panel.add(addButton);
panel.add(modifyButton);
panel.add(deleteButton);
add(panel);
}
}
Code:
public class CustomerInformation extends JPanel
{
JPanel panel = new JPanel();
// Declare JLabels
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JLabel label4;
private JLabel label5;
// Declare JTextFields
private JTextField text1;
private JTextField text2;
private JTextField text3;
private JTextField text4;
private JTextField text5;
private JTextField text6;
//Properties
public void setText1(JTextField _text1)
{
text1 = _text1;
}
public JTextField getText1()
{
return text1;
}
public void setText2(JTextField _text2)
{
text2 = _text2;
}
public JTextField getText2()
{
return text2;
}
public void setText3(JTextField _text3)
{
text3 = _text3;
}
public JTextField getText3()
{
return text3;
}
public void setText4(JTextField _text4)
{
text4 = _text4;
}
public JTextField getText4()
{
return text4;
}
public void setText5(JTextField _text5)
{
text5 = _text5;
}
public JTextField getText5()
{
return text5;
}
public CustomerInformation()
{
// Instantiate labels
label1 = new JLabel("First Name");
label2 = new JLabel("Last Name");
label3 = new JLabel("Customer Status");
label4 = new JLabel("Dependendt Age");
label5 = new JLabel("Customer ID");
// Instantiate text boxes
text1 = new JTextField(10);
text2 = new JTextField(10);
text3 = new JTextField(10);
text4 = new JTextField(10);
text5 = new JTextField(10);
// Set the Grid Layout
panel.setLayout( new GridLayout(5, 2));
// Add items to grid
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(text3);
panel.add(label4);
panel.add(text4);
panel.add(label5);
panel.add(text5);
add(panel);
}
}
Code:
public class CustomerDisplay extends JPanel
{
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JTextField text1 = new JTextField();
//}
public CustomerDisplay()
{
MyTableModel myModel = new MyTableModel();
JTable table = new JTable(myModel);
//table.setPreferredScrollableViewportSize(new Dimension(500,700));
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane);
add(panel);
}
private class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"First Name", "Last Name", "Customer Status", "Dependent Age", "Customer ID"};
private Object[][] data = {{"","","","",""}};
public int getColumnCount1() {
return columnNames.length;
}
public int getRowCount1() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt1(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int c)
{
return getValueAt1(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col)
{
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
return false;
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return columnNames.length;
}
public int getRowCount(int row, int col) {
// TODO Auto-generated method stub
return (Integer) data[row][col];
}
@Override
public Object getValueAt(int row, int col) {
// TODO Auto-generated method stub
return data[row][col];
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return data.length;
}
}
}
Code:
public class Customer extends JPanel
{
JPanel panel = new JPanel();
public Customer()
{
panel.setLayout(new BorderLayout());
panel.add(new Buttons(), BorderLayout.NORTH);
panel.add(new CustomerInformation(), BorderLayout.CENTER);
panel.add(new CustomerDisplay(), BorderLayout.SOUTH);
add(panel);
}
}