Hello Developers i am using two buttons and a table in jframe
when i click on a button new table with different no of rows should be generated and and while clicking on rows of table it should display the no of rows and columns
again when i click on another button it should create the new table with new no of rows
and while clicking on it again it should display the row no and column no
I am using the following code for it. First time on table creation it generates the correct result but when the table is created again then on clicking any row it gives the row no and column no -1 . and array index out of bounds exception whats wrong in my code plz help
JTable table;
JScrollPane jsp;
Button b1 = new JButton("1");
Button b2 = new JButton("2");
add(b1);
add(b2);
b1.addActionListener (this);
b1.addActionListener (this);
public void actionPerformed(ActionEvent ae) {
int i = 0;
if (ae.getActionCommand().equals("1")) {
i = 1;
}
if (ae.getActionCommand().equals("2")) {
i = 2;
}
String title[] = {""};
Object obj[][] = new Object[i][1];
table = new JTable(obj, title);
jsp = new JScrollPane(table);
add(jsp);
table.addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me) {
// first time it returns the true result but on new table creation
//i and j are returned -1 .
int i = table.getSelectedRow();
int j = table.getSelectedColumn();
System.out.println("i is" + i);
System.out.println("j is" + j);
}
There's a few other problems with this example, but to solve your immediate problem, you need to get the source of the MouseEvent and do your operations on that:
public void mouseClicked(MouseEvent me) {
// first time it returns the true result but on new table creation
//i and j are returned -1 .
JTable table = (JTable)me.getSource();
int i = table.getSelectedRow();
int j = table.getSelectedColumn();
System.out.println("i is" + i);
System.out.println("j is" + j);
}
The problem was in your ActionListener you were reassigning table to a new table (which doesn't have any row selected). So if you click on the first table, it's still going to do it's operations on the second table (which doesn't have any row selected).
Related
I use:
Java 10 SE
Java Swing
Eclipse IDE
I have JTable, the contents gets loaded at runtime dynamically. It has some JComboBoxes. If I select the JComboBox, and then attempt to reload the table, the JComboBox appears visible at the time when the table loading is in progress.
Besides that, if the JComboBox's contents gets updated (elsewhere in different table, when the combo supposed to reflect that new contents), that new contents does not get visible staright away after loading the JTable dynamically.
The snap-shot sample of the app:
That's, the table being loaded at runtime up, and in the middle you have vsisble JComboBox persistent from the previous selection.
How to:
Get rid off that persistent JComboBox
Make the data visible instantly, upon update under the combo, once you load the table dynamically
I have the public final class TableColumnEditor extends DefaultCellEditor{
which returns the JComboBox on a specific column:
else if(row == ROW_2_DEVS_WORK_WEEKEND) {
ProjectMetrics metrics = new ProjectMetrics();
JComboBox<String> combo = new JComboBox<>();
combo.setBackground(Color.WHITE);
for(String devs : metrics.identifyDevsThatWorkAtWeekend()) {
combo.addItem(devs);
}
return combo;
}
I have the public final class TableColumnRenderer extends DefaultTableCellRenderer{
which makes sure that the view displays the JComboBox under that specific column:
else if(row == ROW_2_DEVS_WORK_WEEKEND) {
ProjectMetrics metrics = new ProjectMetrics();
JComboBox<String> combo = new JComboBox<>();
combo.setBackground(Color.WHITE);
for(String devs : metrics.identifyDevsThatWorkAtWeekend()) {
combo.addItem(devs);
break;
}
return combo;
}
The table gets loaded dynamically right here (non-essential things removed):
public static void reloadTableDynamically(JTable metricsTable){
DefaultTableModel model = (DefaultTableModel)metricsTable.getModel();
if(projectData.isEmpty()) {
metricsTable.clearSelection();
int rowCount = model.getRowCount();
for(int item = (rowCount - 1); item >= 0; item--) {
model.removeRow(item);//clears previous rows
}
metricsTable.repaint();
return;
}
model.getDataVector().clear();
int rowCount = constantRows + ((devsTask.size() == 0) ? 1 : devsTask.size());
try {
new Thread(()-> {
int lastRowID = 0;
int devsTaskID = 0;
for(int item = 0; item < rowCount; item++) {
Object[] input = null;
if(item == 0) {
input = new Object[] {"", metrics.getProjectDateRange(), "" };
}//similar branches removed
else {
devsTaskID++;
input = new Object[] {"", devsTask.get(devsTaskID).getDeveloper(), ""};
}
model.addRow(input);
metricsTable.scrollRectToVisible(new java.awt.Rectangle(metricsTable.getCellRect(lastRowID++, 0, true)));
metricsTable.repaint();
try {
Thread.sleep(Config.getInstance().getReloadInOutTable());
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
metricsTable.scrollRectToVisible(new java.awt.Rectangle(metricsTable.getCellRect(projectData.size() - 1, 0, true)));
metricsTable.repaint();//so that to reach the last row
}).start();
}
catch(Exception e) {
}
}
What do you think?
Well, I figured out how to overcome this problem.
Firstly, the JComboBox gets updated on EDT(Event Despatch Thread).
/**
* #param combo The JComboBox ref.
* #param toDisplay The value to add to it
*/
public static void updateComboBoxOnEventDespatchThread(JComboBox<String> combo, String toDisplay) {
Runnable doComboUpdate = new Runnable() {
public void run() {
combo.addItem(toDisplay);
}
};
SwingUtilities.invokeLater(doComboUpdate);
}
Under the JTable column editor:
else if(row == ROW_2_DEVS_WORK_WEEKEND) {
ProjectMetrics metrics = new ProjectMetrics();
JComboBox<String> combo = new JComboBox<>();
combo.setBackground(Color.WHITE);
Runnable doComboInsert = new Runnable() {
public void run() {
int id = 0;
for(String devs : metrics.identifyDevsThatWorkAtWeekend()) {
UIutils.updateComboBoxOnEventDespatchThread(combo, "("+ ++id +") " + devs);
}
}
};
SwingUtilities.invokeLater(doComboInsert);
return combo;
}
But the main fix, without which both issues do not go away, is following.
That is, I noticed that in order for data to appear under the table instantly, firstly, you need to select any other unrelated table's cell.
That is, the Java thread, which loads the JTable at runtime, does need to have this:
if(model.getRowCount() > 0) {
metricsTable.selectAll();
}
That's probably a hack, but it works for me!
As you can see in the title I'm interested in enter same data of the editing row in the selected row by pressing CTRL + ENTER as in Microsoft Excel.
I tried with this code but it didn't work.(The data in the cell's table must be Object)
Could you please tell me what is wrong in the following code please?
InputMap im = table_1.getInputMap();
ActionMap am = table_1.getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.CTRL_MASK), "pressed");
am.put("pressed", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
int[] columns = table_1.getSelectedColumns();
int[] rows = table_1.getSelectedRows();
int selectedRow = table_1.getEditingRow();
int selectedCol = table_1.getEditingColumn();
Object value = table_1.getValueAt(selectedRow, selectedCol);
for(int row:rows){
for(int column:columns){
table_1.setValueAt(value,row,column);
}
}
I have a JTable which has 2 columns
column 0 username
column 1 password.
for the password column it will be encrypted to SHA256.
Basically what I want to achieve is it will update all the rows in my password column to SHA256 that I have edited after my button is pressed.
so..
I have a RowData class, this will store the text being edited and the position of the text
being edited(rows,columns).
public class RowData {
int rows = 0, columns = 0;
String text = " ";
public RowData(String text,int rows, int columns) {
setEditedRows(rows);
setEditedColumns(columns);
setEditedText(text);
}
public int getEditedRows() {
return rows;
}
public int getEditedColumns() {
return columns;
}
public String getEditedText() {
return text;
}
public void setEditedRows(int rows) {
this.rows = rows;
}
public void setEditedColumns(int columns) {
this.columns = columns;
}
public void setEditedText(String text) {
this.text = text;
}
}
I wrote a TableModelListener.. I have an List to store the text and the rows and columns
after the table has changed
table.getModel().addTableModelListener(new TableModelListener() {
#Override
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel) e.getSource();
//System.out.println(model.getValueAt(row, column));
if(column == 1) {
String data = (String) model.getValueAt(row, column);
System.out.println(data);
dataList.add(new RowData(data,row,column));
}
}
});
In my button I loop through the list and retrieve the rows, and columns and text
and set the password to SHA256 to the JTable.
updateBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (table.getCellEditor() != null) {
table.getCellEditor().stopCellEditing();
for(int i = 0; i < dataList.size(); i++) {
String text = dataList.get(i).getEditedText();
int rows = dataList.get(i).getEditedRows();
int columns = dataList.get(i).getEditedColumns();
//System.out.println(dataList.get(i).getEditedText() + " " + dataList.get(i).getEditedRows() + dataList.get(i).getEditedColumns());
table.setValueAt(convertPassword.convertToSHA256(text), rows ,columns);
}
}
}
});
The result I get is I will keep printing the password endlessly in my console.
So I think that my logic is wrong and needed to be corrected.
table.setValueAt(convertPassword.convertToSHA256(text), rows ,columns);
When you change the TableModel the TableModelListener will be invoked again. The TableModelListener is invoked whether you change the data by using the JTable or by updating the TableModel directly.
The solution would be to remove the TableModelListener when you click on your button, at the start of your ActionListener. You would then need to add the TableModelListener back to the TableModel at the end of the code in case the user make further changes.
Another solution is to have 3 columns in the TableModel, username, password and sha256Password. Then you can use the JTable to display only the first two columns. See the removeColumn() method of JTable. Then your conversion code would update the TableModel using:
table.getModel().setValueAt(value, row, 2);
Now the code in your TableModel will be invoked, but because you check for updates to column 1, nothing will happen when you update column 2.
Then when you save the data you save the data from the TableModel.
Edit:
I must click into another cell before I can press my button to edit.
You need to stop the cell editing. See Table Stop Editing for a couple of solutions.
I have a JTable which its one column cell is JComboBox.
But when try to get row count when click the table JComboBox cell, I found the row index always return error value (alway is the last click row index).
public class TableComboBoxTest extends JFrame {
private JTable table;
private DefaultTableModel tableModel;
private Object[][] tableCells;
private final String[] TABLE_COLUMNS = {"No.1"};
private final String[] YES_NO_SELECTION = {"Yes", "No"};
public TableComboBoxTest() {
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
tableModel = new DefaultTableModel(tableCells, TABLE_COLUMNS);
table = new JTable(tableModel);
DefaultCellEditor cellEditor = null;
JComboBox selA = new JComboBox(YES_NO_SELECTION);
cellEditor = new DefaultCellEditor(selA);
cellEditor.setClickCountToStart(1);
table.getColumn(TABLE_COLUMNS[0]).setCellEditor(cellEditor);
JScrollPane jsp = new JScrollPane();
jsp.getViewport().add(table, null);
pane.add(jsp, BorderLayout.CENTER);
TableCellEditor tce = null;
addRow("Yes");
outputDefaultSelection(0, 0);
addRow("No");
outputDefaultSelection(1, 0);
System.out.println("");
selA.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
JComboBox cb = (JComboBox) e.getSource();
String sel = (String) cb.getSelectedItem();
int rowIndex = table.getSelectedRow();
rowIndex = table.convertRowIndexToModel(rowIndex);
if (rowIndex == -1) {
return;
}
outputDefaultSelection(rowIndex, 0);
System.out.println("Select: " + sel + " at " + rowIndex);
}
}
});
}
private void addRow(String v1) {
Vector<String> vec = new Vector<String>();
vec.add(v1);
tableModel.addRow(vec);
tableModel.fireTableDataChanged();
}
private void outputDefaultSelection(int row, int col) {
TableCellEditor tce = table.getCellEditor(row, col);
System.out.println("Default " + row + "-" + col + " Selection: " + tce.getCellEditorValue());
System.out.println("Default " + row + "-" + col + " Value: " + table.getModel().getValueAt(row, col));
}
public static void main(String[] args) {
TableComboBoxTest stt = new TableComboBoxTest();
stt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
stt.setSize(200, 100);
stt.setVisible(true);
}
}
Default 0-0 Selection: Yes
Default 0-0 Value: Yes
Default 1-0 Selection: Yes
Default 1-0 Value: No*
When Click on first row and select "Yes", no change event trigger.
When Click on second row, change event trigger! and row number is wrong: 0
Default 0-0 Selection: No
Default 0-0 Value: Yes
Select: No at 0*
When continue to click on first row, change event trigger! and row number is wrong: 1
Default 1-0 Selection: Yes
Default 1-0 Value: No
Select: Yes at 1
How can I get correct clicking cell number?
And for the itemStateChanged process, I also found if cell set value is same with default column value ("Yes"), when click it event will not be trigger. But if cell set value to "No", click it will cause change event. That means model data is different with default selected data. How to make them consistent?
Thanks~
That means model data is different with default selected data. How to make them consistent?
It just means that the model has not yet been updated with the newly selected value from the combo box.
This can be demonstrated by using the following:
final String sel = (String) cb.getSelectedItem();
final int rowIndex = table.convertRowIndexToModel(table.getSelectedRow());
if (rowIndex == -1) {
return;
}
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
outputDefaultSelection(rowIndex, 0);
System.out.println("Select: " + sel + " at " + rowIndex);
}
});
Now, the display code will be added to the end of the Event Dispatch Thread, which means it will be executed after all other events are finished executing so the TableModel will now be updated.
However, that is not the best solution. If you want to know when data has been changed in a cell then add a TableModelListener to the TableModel. Don't use an ItemListener.
I am facing a problem with the functionality of removing a row(s) form one table view to another table view currently I have two button one add row form the bottom table to the above and the next add from the above the the botton. the button that add from bottom to top called add and the button that add from top to bottom called remove in both once the row is copied across it get deleted from the original table.
The problem I am facing is:
when I add all the row from the bottom table to the top table
if I sort Column 0 in top table ,then try to remove the row from the top table to the bottom table
the row do not get deleted from the top table although it is added in the bottom table.
Could you please Help me solve this problem or advice me on a better way?
Here is my current code:
Remove button:
btnRemove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
spTable.removeAll();
x = resultTable.getSelectedRows();
String[] cName= {"Column 0","Column 1","Column 2","Column 3","Column 4","Column 5","Column 6","Column 7","Column 8"};
if(rTable.getRowCount()==0){
model=addToNew(resultTable,x,cName,model);
}else{ model =addToExisting(resultTable,rTable, x, model);
}
deletRows(x,resultTable);
rTable.setModel(model);
JScrollPane spS = new JScrollPane(rTable);
rTable.getColumnModel().getColumn(1).setPreferredWidth(290);
spS.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
spS.setPreferredSize(new Dimension(800,200));
rTable.setFillsViewportHeight(true);
spTable.add(spS);
validate();
}});
Add button:
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel.removeAll();
x = rTable.getSelectedRows();
String[] cName= {"Column 0","Column 1","Column 2","Column 3","Column 4","Column 5","Column 6","Column 7","Column 8"};
if(resultTable.getRowCount()==0){
model=addToNew(rTable,x,cName,model);
}else{ model =addToExisting(rTable,resultTable, x, model);
}
deletRows(x,rTable);
resultTable.setModel(model);
JScrollPane spS = new JScrollPane(resultTable);
resultTable.getColumnModel().getColumn(1).setPreferredWidth(290);
spS.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
spS.setPreferredSize(new Dimension(800,170));
panel.add(spS);
resultTable.setFillsViewportHeight(true);
validate();}});
deletRow
public void deletRows(int[] selected, JTable t){
for(int i = selected.length-1;i>=0;i--){
int y = selected[i];
((DefaultTableModel)t.getModel()).removeRow(y);
validate();}}
/**
* this method allow to add rows to an New table
*/
public DefaultTableModel addToNew(JTable t1,int[] selected,String[] ColName, DefaultTableModel m){
String[] name = ColName;
int col = name.length;
m =new DefaultTableModel();
for(int i= 0;i<col;i++){
m.addColumn(name[i]);
}
Object[] data= new Object[col];
for(int i =0; i<selected.length;i++){
int y= selected[i];
for (int z =0; z<col;z++){
if( t1.getValueAt(y, z)!= null){
String value = t1.getValueAt(y, z).toString();
data[z]= value;
}else{
data[z]=null;
}
}
m.addRow(data);
}
return m;
}
/* this method allow to add rows to an Existing table */
public DefaultTableModel addToExisting(JTable t1,JTable t2, int[] selected, DefaultTableModel m){
m =(DefaultTableModel)t2.getModel();
Object[] data= new Object[m.getColumnCount()];
for(int i =0; i<selected.length;i++){
int y= selected[i];
for (int z =0; z<m.getColumnCount();z++){
if( t1.getValueAt(y, z)!= null){
String value = t1.getValueAt(y, z).toString();
data[z]= value;
}else{
data[z]=null;
}
}
m.addRow(data);
}
return m;
}