Reputation: 45
I have created a JFrame and I want to create an AbstractTableModel to display the data from database using dbquery in the grey box.
This is the first time I am doing this and I have been struggling for very long, really hope to receive some help! Thank you.
JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean conditionOK = false;
if(chckbxr.isSelected()==false && chckbxr_1.isSelected()==false && chckbxr_2.isSelected()==false && chckbxr_3.isSelected()==false){
JOptionPane.showMessageDialog(frame,"Please select client account");
}
else if(fromDate.getText().equals("")){
JOptionPane.showMessageDialog(frame, "Please enter starting date");
}
else if(toDate.getText().equals("")){
JOptionPane.showMessageDialog(frame, "Please enter end date");
}
else if(Integer.parseInt(fromDate.getText()) > Integer.parseInt(toDate.getText())){
JOptionPane.showMessageDialog(frame, "End date is earlier than starting date");
}
else
conditionOK = true;
if(conditionOK==true){
int fromYear = Integer.parseInt(fromDate.getText().substring(0, 4));
int fromMonth = Integer.parseInt(fromDate.getText().substring(4,6))-1;
int fromDay = Integer.parseInt(fromDate.getText().substring(6,8));
int toYear = Integer.parseInt(toDate.getText().substring(0, 4));
int toMonth = Integer.parseInt(toDate.getText().substring(4,6))-1;
int toDay = Integer.parseInt(toDate.getText().substring(6,8));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdf2 = new SimpleDateFormat("dd MMM yyyy");
DateFormat formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd");
Calendar convertedToDate = Calendar.getInstance();
convertedToDate.set(toYear,toMonth,toDay);
Calendar convertedFromDate =Calendar.getInstance();
convertedFromDate.set(fromYear,fromMonth,fromDay);
int MonthDifferenceCount = 1;
//calculate months difference
for ( MonthDifferenceCount=1; convertedFromDate.compareTo(convertedToDate) <0; MonthDifferenceCount++)
{
convertedFromDate.add(Calendar.MONTH, 1);
convertedFromDate.set(convertedFromDate.DAY_OF_MONTH,convertedFromDate.getActualMaximum(Calendar.DAY_OF_MONTH));
}
convertedToDate.set(toYear,toMonth,toDay);
convertedFromDate.set(fromYear,fromMonth,fromDay);
//DateArray and PreDate Array are used to store date as date format. Shall be use for tradeDate and SetlDate
Date preDateArray[] = new Date[MonthDifferenceCount];
Date DateArray [] = new Date[MonthDifferenceCount];
for (int i=0; i <MonthDifferenceCount; i++)
{
convertedFromDate.add(Calendar.MONTH,-1);
convertedFromDate.set(convertedFromDate.DAY_OF_MONTH,convertedFromDate.getActualMaximum(Calendar.DAY_OF_MONTH));
preDateArray[i] = convertedFromDate.getTime();
try {
preDateArray[i] = (Date)formatter.parse(sdf.format(preDateArray[i]));
} catch (ParseException e1) {
e1.printStackTrace();
}
convertedFromDate.add(Calendar.MONTH, 1);
convertedFromDate.set(convertedFromDate.DAY_OF_MONTH,convertedFromDate.getActualMaximum(Calendar.DAY_OF_MONTH));
DateArray[i] = convertedFromDate.getTime();
try {
DateArray[i] = (Date)formatter.parse(sdf.format(DateArray[i]));
} catch (ParseException e1) {
e1.printStackTrace();
}
convertedFromDate.add(Calendar.MONTH, 1);
}
summaryFromDate = sdf2.format(DateArray[0]);
summaryToDate = sdf2.format(DateArray[DateArray.length-1]);
String DateList[] = new String[MonthDifferenceCount];
String PreDateList[] = new String[MonthDifferenceCount];
for (int i=0; i<DateArray.length; i++)
{
DateList[i] = sdf1.format(DateArray[i]);
PreDateList[i] = sdf1.format(preDateArray[i]);
}
ArrayList<String> cltAccList = new ArrayList<String>();
if(chckbxr.isSelected()==true){
cltAccList.add("10190R");
}
if(chckbxr_1.isSelected()==true){
cltAccList.add("10230R");
}
if(chckbxr_2.isSelected()==true){
cltAccList.add("10280R");
}
if(chckbxr_3.isSelected()==true){
cltAccList.add("10290R");
}
}
}
});
btnSubmit.setBounds(37, 643, 89, 23);
add(btnSubmit);
After submitting the date the AbstractTableModel should actually display the data from the database.
Table Model:
public class MTableModel {
public static class MyModel extends AbstractTableModel {
private List<Object[]> data;
private List<String> columnNames;
public MyModel(List<String> columnNames, List<Object[]> data) {
super();
this.columnNames = columnNames;
this.data = data;
}
@Override
public int getRowCount() {
return data.size();
}
@Override
public int getColumnCount() {
return columnNames.size();
}
@Override
public String getColumnName(int column) {
return columnNames.get(column);
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data.get(rowIndex)[columnIndex];
}
}
protected void initUI() {
JFrame frame = new JFrame(MTableModel.class.getSimpleName());
List<String> columns = Arrays.asList("Name", "Gender");
List<Object[]> data = new ArrayList<Object[]>();
for (int i = 0; i < 50; i++) {
Object[] value = new Object[2];
data.add(value);
}
JTable table = new JTable(new MyModel(columns, data));
frame.add(new JScrollPane(table));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MTableModel().initUI();
}
});
}
}
Upvotes: 2
Views: 1527
Reputation: 1
I attach you a FULL EXAMPLE of the situation:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
/**
* Example class to learn how to use AbstractTableModel
*
* @author Moreno
*
*/
public class Example extends JFrame {
private static final long serialVersionUID = 1L; // default serialVersionUID
/**
* Example class, is a JFrame
*
*/
public Example() {
super("Example");
TableModelCustom myModel = new TableModelCustom(); //Create my table model
JTable table = new JTable(myModel); //Create JTable
table.setPreferredScrollableViewportSize(new Dimension(480, 70)); //Prefered size scroll
JScrollPane scrollPane = new JScrollPane(table); //Create scroll with table
JButton button = new JButton("Click me"); //Create JButton
JButton button2 = new JButton("Click me2"); //Create JButton2
button.addActionListener(new ActionListener() { //Listener onClick...
@Override
public void actionPerformed(ActionEvent e) {
//Change data
Object[][] data = { { "Rick", "Mourt", "Main Street", 55, true} };
//Send data to the model
myModel.setData(data);
}
});
button2.addActionListener(new ActionListener() { //Listener onClick...
@Override
public void actionPerformed(ActionEvent e) {
//Change data
Object[][] data = { { "Thomas", "Mourt", "Main Street 50", 55, true },
{ "Rich", "Fua", "Main Street 2", 25, false },
{ "Alan", "Poe", "Main Street 3", 12, true } };
//Send data to the model
myModel.setData(data);
}
});
//Add components to the window
add(button, BorderLayout.NORTH); //Add first button
add(scrollPane, BorderLayout.CENTER); //Add scroll pane
add(button2, BorderLayout.SOUTH); //Add first button
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* Model
*
* @author Moreno
*
* @see AbstractTableModel
*/
class TableModelCustom extends AbstractTableModel {
private static final long serialVersionUID = 1L;
// Columns
private String[] columnNames = { "Name", "Surname", "Direction", "Age", "Sport" };
// Data
private Object[][] data = { { "Peter", "Serry", "Rue....", 5, false },
{ "Paul", "Hey", "XXX", 13, true },
{ "Patriks", "--", "XXX", 26, false },
{ "Alice", "Hall", "XXX", 24, true },
{ "Mary", "Houstong", "XXX", 12, false } };
/**
* This method receives the new data vector, and update the table
*
* @param data
* with new values
*/
public void setData(Object[][] data) {
this.data = data; //Save data..
fireTableDataChanged(); //Update table
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}
public static void main(String[] args) {
new Example(); //Create the window
}
}
Please adapt it to your needs
Upvotes: 0
Reputation: 7126
Your value
arrays are getting initialized to a pair of null
entries by default.
Object[] value = new Object[2];
Try this instead:
for (int i = 0; i < 50; i++) {
Object[] value = new Object[]{"Name" + i, Math.random() < .5 ? "M" : "F"};
data.add(value);
}
Upvotes: 2