Symbianize Forum

Most of our features and services are available only to members, so we encourage you to login or register a new account. Registration is free, fast and simple. You only need to provide a valid email. Being a member you'll gain access to all member forums and features, post a message to ask question or provide answer, and share or find resources related to mobile phones, tablets, computers, game consoles, and multimedia.

All that and more, so what are you waiting for, click the register button and join us now! Ito ang website na ginawa ng pinoy para sa pinoy!

JAVA: Question lang po about JTable

mysteltaine

Novice
Advanced Member
Messages
32
Reaction score
0
Points
26
Mga Sir patulong lang Student here. Possible po ba na Mag edit ng Contents sa JTable and ung changes na ginawa ay automatically na masesave sa Database?

example ung Quantity ay iibahin ko para maging 6. then automatic masasave nya on runtime or pag may save button.


View attachment 290857

Maraming Salamat po.
 

Attachments

  • sy.jpg
    sy.jpg
    23.7 KB · Views: 23
Talaga po? Pwede po makahingi kahit line of code lang? Medyo kinulang kasi ung turo samin so kailangan tlga mag self study... So far eto lang po pinoproblema namin.

Salamat ng marami
 
What I would suggest is to create a class that extends the AbstractTableModel then override these methods:
getColumnCount(), getRowCount(), getValueAt(), getColumnClass(), and setValueAt().

The setValueAt method is where you will put the code to save the data to the database.

If you want more info you should post your code first so we can have a look at it.
 
"public void ShowTable()
{
tblModel = (DefaultTableModel)tblBNew.getModel();
try
{
rs = s.executeQuery("Select VIN,VehicleName,VehicleModel,VehicleYear,Manufacturer,SellingPrice,Quantity from tblVehicleInfo where VehicleType='Brand New'");
ResultSetMetaData md = rs.getMetaData();

int row = tblModel.getRowCount();

while(row>0)
{
row--;
tblModel.removeRow(row);
}
int colcount = md.getColumnCount();
Object[] data = new Object[colcount];
while(rs.next())
{

for(int i=1;i<=colcount;i++)
{
data[i-1] = rs.getString(i);
}
tblModel.addRow(data);
}
tblModel.fireTableDataChanged();
}
catch(Exception e) {System.out.println("Error: "+e);}

}




Eto po ung Code to for viewing the Table. ang Plan po sana is dun sa table mismo mag iinput ng values or mag edit. and masesave using a button.
Ang naturo lang po kasi samin ay Table Viewing.

Maraming salamat!
 
"public void ShowTable()
{
tblModel = (DefaultTableModel)tblBNew.getModel();
try
{
rs = s.executeQuery("Select VIN,VehicleName,VehicleModel,VehicleYear,Manufacturer,SellingPrice,Quantity from tblVehicleInfo where VehicleType='Brand New'");
ResultSetMetaData md = rs.getMetaData();

int row = tblModel.getRowCount();

while(row>0)
{
row--;
tblModel.removeRow(row);
}
int colcount = md.getColumnCount();
Object[] data = new Object[colcount];
while(rs.next())
{

for(int i=1;i<=colcount;i++)
{
data[i-1] = rs.getString(i);
}
tblModel.addRow(data);
}
tblModel.fireTableDataChanged();
}
catch(Exception e) {System.out.println("Error: "+e);}

}




Eto po ung Code to for viewing the Table. ang Plan po sana is dun sa table mismo mag iinput ng values or mag edit. and masesave using a button.
Ang naturo lang po kasi samin ay Table Viewing.

Maraming salamat!


Adding new and/or editing existing records require a lot more code than you posted. Ideally, you will have multiple classes, each with specific purpose. For this project, you should have a class for the table model, utility class where the sql connection and queries will be handled, and another class for the main user interface.

See this example Using JDBC with GUI API. You can download the code here.

Another example that allows you to edit the table cells.

Good luck.
 
Back
Top Bottom