Hi, I'm having trouble with setting up my table. How do i get the header to show initially. I want "code", "Number" and "Value" as the header. And I want to know if the way i added the table is correct? Can you help me please. Thank you!
import model.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
public class Shares extends JPanel
{
private JButton btnFind = new JButton();
private JTextField txtId = new JTextField(10);
private JTable tableShare = new JTable();
Customers customers = null;
public Shares(Trader trader)
{
customers = trader.customers();
setup();
build();
}
private void setup()
{
setLayout(new FlowLayout());
setBorder(BorderFactory.createLineBorder(Color.blue));
}
private void build()
{
Box box1 = Box.createHorizontalBox();
box1.add(new JLabel("Your id"));
box1.add(txtId);
btnFind.setText("Find");
box1.add(btnFind);
Box box2 = Box.createVerticalBox();
box2.add(box1);
box2.add(Box.createVerticalStrut(20));
setTable();
box2.add(tableShare);
add(box2);
setBtnFind();
}
private void setBtnFind()
{
btnFind.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
btnFindActionPerformed(evt);
}
});
}
private void btnFindActionPerformed(java.awt.event.ActionEvent evt){
Customer customer = customer();
LinkedList<Share> shares= customer.shares();
int j =0;
for(Share share: shares){
tableShare.setValueAt(share.code(), j, 0);
tableShare.setValueAt(share.number()+"", j, 1);
tableShare.setValueAt(share.cost()+"", j, 2);
j++;
}
}
private Customer customer(){
int custId = Integer.parseInt(txtId.getText());
Customer customer = customers.customer(custId);
return customer;
}
private void setTable(){
tableShare.setModel(new DefaultTableModel(
new String[][]{
{null, null, null},
{null, null, null},
{null, null, null},
{null, null, null},
{null, null, null},
{null, null, null},
{null, null, null},
},
new String[]{
"Code","Number","Value"
}
));
}
}