Java : (Swing) JTable
JTable : Used when more than 1 column is requiredPre -Req :
- Make sure Window Builder addon is installed into eclipse
- Create a New Swing Designer >app Window > Save
- In Design Mode > add a JList Component
DefaultTableModel model = new DefaultTableModel();
model.addColumn("A");
model.addColumn("B");
table = new JTable(model);
frame.getContentPane().add(table, BorderLayout.CENTER);
model.addRow(new Object[]{"1", "Deeapk"});
Method 2:
String[] Cols={"A" , "B"};
Object[][] data={
{"1","deepak"},
{"2","Alex"},
};
table = new JTable(data,Cols);
frame.getContentPane().add(table, BorderLayout.CENTER);
Code with Scroll bar :
frame = new JFrame();
DefaultTableModel model = new DefaultTableModel();
table = new JTable(model);
scrollPane = new JScrollPane(table);
scrollBar = new JScrollBar();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout(0, 0));
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
scrollPane.add(scrollBar);
model.addColumn("A");
model.addColumn("B");
for(int i=1;i<50;i++){
model.addRow(new Object[]{i, "Deeapk"});
}
No comments:
Post a Comment