i create JTable in that table value are retrieve from data base
(Except fifth column) in fifth column .i enter value through keyboard
in fifth column but when getting value again from jtable at that time
i am not getting last entered value in fifth column please suggest
something
my method code bellow
public class TableDataDelete implements ActionListener
{
public void actionPerformed(ActionEvent ee) {
int b = table.getRowCount();
System.out.println("row count" + b);
for (int i = 0; i <b; i++) {
try
{
String str = (String) table.getModel().getValueAt(i, 3);
String str1 = (String) table.getModel().getValueAt(i, 5);
if (!(str1 == null)) {
((DefaultTableModel)table.getModel()).removeRow(i);
table.repaint();
System.out.println("remove row no is"+i);
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("array index out of bound exception"+e);
}
}
}
}
working code when i enter value of five cells it can get only first four cell from the column cant getting last entered cell value
You should use this code to stop cell editing to get last value
if(table.getCellEditor() != null){
table.getCellEditor().stopCellEditing();
}
Related
I am having trouble deleting the previous row of my second table of my Products Stock In GUI in Java.
The flow of the functionality of my GUI is this:
If the user will select a row in the first table of the GUI, the values of those rows will be reflected on the second rows
If the user will select another/different row in the first table, the previous reflected row which was selected in the first table must be deleted and is replaced by the current selected row.
To sum it up, I have to replace/delete the previous reflected rows if I will select a different row in the first table then replace that previous selected row into the current selected row.
Here's my source code:
private void firstTableMouseClicked(java.awt.event.MouseEvent evt) {
DefaultTableModel secondTblmodel = (DefaultTableModel) secondTable.getModel();
int selectPRoww = firstTable.getSelectedRow();
for (int x = 0; x < ProductAllData2[selectPRoww].length; x++) {
//ProductAllData2 is a 3D array
if (ProductAllData2[selectPRoww][x][0] != null) {
if (setRowCountID2 == 0)
secondTblmodel.setRowCount(0);
//Reflects and displays the values of the selected
// row from the first table to the second table
secondTblmodel.addRow(ProductAllData2[selectPRoww][x]);
// deletes previous row if index of selected row is
// greater than the previous selected row (doesn't work)
if (selectPRoww > selectPRoww) {
secondTblmodel.removeRow(selectPRoww - 1);
}
// deletes previous row if index of selected row is
// less than the previous selected row (doesn't work)
if (selectPRoww < selectPRoww) {
secondTblmodel.removeRow(selectPRoww + 1);
}
setRowCountID2++;
}
}
}
Am I missing something in my functionality or do I need to modify the for loops?
In my opinion, your logic should not be like this.
As the second table (View) depends on the selection of the first table, then the second table should always be cleared, then you do this logic.
//Reflects and displays the values of the selected
// row from the first table to the second table
secondTblmodel.addRow(data);
Important Point: Please use ListSelectionListener on table1, rather than depending on mouse clicks.
Example of usage:
table1.getSelectionModel().addListSelectionListener(new SharedListSelectionHandler());
class SharedListSelectionHandler implements ListSelectionListener {
public void valueChanged(ListSelectionEvent e) {
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
int firstIndex = e.getFirstIndex();
int lastIndex = e.getLastIndex();
boolean isAdjusting = e.getValueIsAdjusting();
output.append("Event for indexes "
+ firstIndex + " - " + lastIndex
+ "; isAdjusting is " + isAdjusting
+ "; selected indexes:");
if (lsm.isSelectionEmpty()) {
output.append(" <none>");
} else {
// Find out which indexes are selected.
int minIndex = lsm.getMinSelectionIndex();
int maxIndex = lsm.getMaxSelectionIndex();
for (int i = minIndex; i <= maxIndex; i++) {
if (lsm.isSelectedIndex(i)) {
output.append(" " + i);
}
}
}
output.append(newline);
}
}
How to Write a List Selection Listener | The Java™ Tutorials
Similar StackOverflow: Deleting row from JTable after valueChanged event is triggered
Here I want average of row5, row6, row7, then I want to check with another table whether calculation is right or wrong. How to achieve this?
for(WebElement rowElement:TotalRowCount)
{
List<WebElement> TotalColumnCount=rowElement.findElements(By.xpath("td/span"));
int ColumnIndex=1;
for(WebElement colElement:TotalColumnCount)
{
String s=colElement.getText().replace("%", "").replace(",","");
if(!s.isEmpty()&&!s.equals("NA"))
{
if(s.contains(".")||s.contains("-"))
{
double d=Double.parseDouble(s);
System.out.println("Row "+RowIndex+"Double value is"+d);
}
else
{
int x1=Integer.parseInt(s);
System.out.println("Row "+RowIndex+"Integer value"+x1);
}
ColumnIndex=ColumnIndex+1;
}
else{/*do nothing*/}
}
RowIndex=RowIndex+1;
}
Output:
Row 5Integer value140
Row 6Integer value51
Row 6Integer value52
Row 6Integer value51
Row 6Integer value36
Row 7Double value is30.0
Row 7Double value is30.65
Row 7Double value is29.38
public void setForm(mhs m)
{
String[] jurusan = {"TI","SI"};
JComboBox<String> cjurusan = new JComboBox<String> (jurusan);
String st;
st = ((String)m.getJurusan()).toUpperCase();
for(int i = 0; i < cjurusan.getItemCount(); i++)
{
String jur = ((String) cjurusan.getItemAt(i)).toUpperCase();
if(jur.equals("SI"))
{
cjurusan.setSelectedIndex(1);
}
else
{
cjurusan.setSelectedIndex(0);
}
}
tnim.setText(m.getNim());
tnama.setText(m.getNama());
cjurusan.setBounds(110,70,90,20);
add(cjurusan);
}
}
I want to set jcombobox's value according the data record in jtable, till now, I just get the data value at the first time, for second time the jcombobox's value is the same with last. please help. many thanks.
This is my sample image program : screenshoot
This question already has an answer here:
JTable Java Error Stack OverFlow when setvalue to a specific column
(1 answer)
Closed 6 years ago.
I am getting stackoverflow error when I try to change one of columns data when 2 columns of the columns has been edited.
I have 3 columns which are items, quantity, price.
I want to calculate the price when items and quantity has an input. Below is my code:-
itemsTable.getModel().addTableModelListener(new TableModelListener() {
#Override
public void tableChanged(TableModelEvent e) {
for (int i = 0; i < itemsTable.getRowCount(); i++) {
if (itemsTable.getValueAt(i, 0) != null) {
String item = itemsTable.getValueAt(i, 0).toString();
double price = Double.parseDouble(selectedItem.substring(item.indexOf("RM") + 2, item.length())); //get price from the cell
double qty = Double.parseDouble(itemsTable.getValueAt(i, 1).toString()); //get quantity from cell
itemsTable.setValueAt(price * qty, i, 2); //calculate price * qty and set price
}
}
}
});
I set a combobox that loaded with database data to items column and a JSpinner for quantity
What am I doing wrong? I am new to JTable.
EDIT
Answer:(credits to Titus)
#Override
public void tableChanged(TableModelEvent e) {
if (e.getType() == TableModelEvent.UPDATE && e.getColumn() != 2) {
for (int i = 0; i < itemsTable.getRowCount(); i++) {
if (itemsTable.getValueAt(i, 0) != null) {
...
}
}
}
}
Also wouldn't hurt to limit the loop to going from row e.getFirstRow() thru e.getLastRow(), and maybe updating price in an invokeLater process as well. But titus hit it head on: gotta stop the infinite cascade of change invokes handler which makes change that invokes handler which makes change...
My code only works for the 1st row. I want to add a row if it is not in the JTable. Class_assing_tb is my JTable.
I want to add values, which I get from JCombobox, to JTable when I click "Add Item" button. It can only input a maximum of 4 items. I want to do it like this, if I add an item that is already in the JTable I want to give a message "Denied" else add the item to JTable.
int count = Class_assing_tb.getRowCount();
if (count == 0) {
addrow(); //this is to Command For Add new Row
} else if (count == 4) {
System.out.println("maximum Row Count");
// msg ****** Maximum Classes
} else {
int a = Class_assing_tb.getRowCount();
DefaultTableModel tm2 = (DefaultTableModel) Class_assing_tb.getModel();
loop:
for (int i = 0; i < a; i++) {
//System.out.println("Row Count is" + a);
// System.out.println("Sttate is"+i);
if (tm2.getValueAt(i, 0).equals(mng_stu_classatnd.getSelectedItem()) & tm2.getValueAt(i, 2).equals(mng_stu_batch.getSelectedItem()) & tm2.getValueAt(i, 3).equals(mng_stu_type.getSelectedItem())) {
System.out.println("Denied");
break loop;
} else {
addrow();//this is to Command For Add new Row
// continue loop;
}
}
}
Try something like this:
//Global Declaration
private Vector<Vector<String>> data; //used for data from database
private Vector<String> header; //used to store data header
//Display only header on form load
//create header for the table
header = new Vector<String>();
header.add("Column");
model=new DefaultTableModel(data,header);
table = new JTable(model);
//in actionPerformed()
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==add){
int count=table.getRowCount();
for(int i=1;i<=count;i++){
if(table.getValueAt(i,0).equals(jComboBox.getSelectedItem())){
JOptionPane.showMessageDialog((Component) null,"Duplicate...");
return;
}
}
if(count==4){
JOptionPane.showMessageDialog((Component) null,"Maximum Limit is crossed...");
}else{
Object d= jComboBox.getSelectedItem();
model.addRow(d);
}
}
}