Java, jTable search - java

This is NetBeand GUI code. I need help with that specifically.
How do I get this code to append whole row in witch the asked element is to TextArea:
String trener1 = jTextField9.getText();
DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
int mCol = model.getColumnCount();
int mRow = model.getRowCount();
for(int i = 0; i < mCol; i++){
for(int j = 0; j < mRow; j++){
if(jTable1.getModel().getValueAt(i, j).equals(trener1)){
jTextArea1.append(model.getValueAt(i).toString()+ "\n");
}
}
}
Code should do: I have a list of gym members. Informations about them are in table, their name, age, instructor. When I type the name of the instructor in one TextField, I want all names of members that have that instructor to get appended to TextArea.
Same with their age.

That's it. Solved.
String trener1 = jComboBox5.getSelectedItem().toString();
DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
int Row = model.getRowCount();
for(int i = 0; i < Row; i++){
if(jTable1.getModel().getValueAt(i, 8).equals(trener1)){
jTextArea1.append(model.getValueAt(i, 1).toString());
}
}

Related

How can I insert the i value into the variable?

I have 5 textFields and they are in sequence book1TextField, book2TextField.book3.,book4.,book5. I want variable i to be inserted in these textFields so it will be like book(i)TextField.getText() so that I don't have to do this one by one.
for (int i = 0; i < 5; i++){
bookQuantities[i] = Integer.parseInt(book1TextField.getText());
System.out.println(bookQuantities[i]);
}
When those TextFields are created you should store them in an ArrayList<TextField> to be accessed at a later time. Then you can easily write something like:
for(int i = 0; i < bookTextFields.size(); i++) {
bookQuantities[i] = Integer.parseInt(bookTextFields.get(i).getText());
}
You can use instanceof like this:
int comp = panel.getComponentCount();
int[] bookQuantities = new int [comp];
for (int i = 0; i < comp; i++) {
if (panel.getComponent(i) instanceof JTextField) {
JTextField txt = (JTextField) panel.getComponent(i);
bookQuantities[i] = Integer.parseInt(txt.getText());
System.out.println(bookQuantities[i]);
}
}

Two dimension array, how to programically add new array element? Java

I don't know, if I forgot how, or I just can't figure it out how.
For example :
Object[][] data = {
{"id", "projectname","valueid", "value"},
};
And this is how they should be added, but in loop:
Object[][] data = {
{"id", "projectname","valueid", "value"},
{"id2", "projectname2","valueid2", "value2"},
{"id3", "projectname3","valueid3", "value3"},
};
And so on..
I need a tip only, like a skeleton how it should be. I tried to figure it out, but had no idea how.
Thanks!
You can add a new array to another array like this :
data[1] = new Object[]{"id_1", "projectname_1","valueid_1", "value_1"};
...
data[n] = new Object[]{"id_n", "projectname_n","valueid_n", "value_n"};
You can use this way in any loop for example :
int length = 5;
Object[][] data = new Object[length][];
for(int i = 0; i < length; i++){
data[i] = new Object[]{...some information};
}
for (int i = 1; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
int line = i+1;
data[i][j] = data[0][j]+ line;
}
}

Displaying the string values of 2 dimensional Object array

I am trying to display the contents of an array after iterating through rows and columns of a JTable. I tried Arrays.toString(myTwoDimensionalArrayVariable) but it won't display the string values.
My goal is to check duplicates for every column per row of a destination JTable when user tries to add row values from a source JTable that's why I want to display the contents of the array.
The values on columns are combination of double, String, and int.
int myRowCount = aJTableParameter.getRowCount();
int myColumnCount = aJTableParameter.getColumnCount();
Object[][] myRowValues = new Object[myRowCount][myColumnCount];
for (int j = 0; j < myRowCount; j++) {
for(int i = 0; i< myColumnCount; i++){
myRowValues[j][i] = aDestinationTable.getValueAt(j, i);
}
}
System.out.println(Arrays.toString(myRowValues));
if (Arrays.asList(myRowValues).contains(column1Value)
&& Arrays.asList(myRowValues).contains(column2Value)
&& Arrays.asList(myRowValues).contains(column3Value)
&& Arrays.asList(myRowValues).contains(column4Value)) {
JOptionPane.showMessageDialog(null, "Duplicate, try again.");
}else{
//do something else
}
I only get this output:
run:
Successfully recorded login timestamp
[]
[[Ljava.lang.Object;#35fa3ff2]
[[Ljava.lang.Object;#407c448d, [Ljava.lang.Object;#1e78a60e]
Is there any other alternative than using 2 Dimensional Arrays?
I'd appreciate any help.
Thanks.
IFF your JTable cells contain only Strings, you can define your array as String[][] instead of Object[][] and fill it with your JTable contents using aDestinationTable.getValueAt(j, i).toString().
EDIT: since that's not the case (as per your comment), it's probably better to use a List, like this:
List<List<Object>> objectList = new ArrayList<>();
for (int j = 0; j < 2; j++) {
objectList.add(j, new ArrayList<>());
for (int i = 0; i < 2; i++) {
if (i==0) objectList.get(j).add("string" + j + i);
if (i==1) objectList.get(j).add((double) 37.8346 * j * i);
}
}
System.out.println("OBJECT LIST: "+objectList);
Output:
OBJECT LIST: [[string00, 0.0], [string10, 37.8346]]
Your code should look like this, then:
List<List<Object>> myRowValues = new ArrayList<>();
for (int j = 0; j < myRowCount; j++) {
myRowValues.add(j, new ArrayList<>());
for (int i = 0; i < myColumnCount; i++) {
myRowValues.get(j).add(aDestinationTable.getValueAt(j, i));
}
}
System.out.println(myRowValues);

Converting from Object[][] to ImageIcon

I am trying to convert my object[][] to an ImageIcon. I current have a public Object[][] images; where I store my images. How do I convert this to an ImageIcon so that I can use it to put it into my JTable? I have tried this:
for(int i = 0; i < 100; i++)
{
...
ImageIcon icon = new ImageIcon(images[i].toString());
// add to table
}
Nothing shows in my table and if I print the value out from the cell this shows: [Ljava.lang.String;#72787a6f
Thanks for any answer.
You have a 2D array so you need to use another index to reference the element at row i column j
for (int i = 0; i < images.length; i++) {
for (int j = 0; j < images[i].length; j++) {
ImageIcon icon = new ImageIcon(images[i][j].toString());
...
}
}
It would make more sense to use a String[][] array

Java Getting JTable Value (Per row)

I would like to get the value from the Jtable, and I tried it using the getvalueat however whenever I try to get the value from the JTable it only get the value from the first column of the selected row, I need to get all the value from the Jtable which I selected. Can you please help me with this one
here is my code:
class GetTableValue implements ActionListener{
public void actionPerformed(ActionEvent e){
AbstractButton button = (AbstractButton)e.getSource();
if(e.getActionCommand().equals(button.getActionCommand)){
int row = table.getSelectedRow();
int col = table.getSelectedColumn();
Object data = (Object)table.getValueAt(row, col);
JOptionPane.showMessageDialog(null, data);
}
}
}
This is my action event where the value of the selected table is shown in the JOptionPane unfortunately it only display one value(which is the one you already selected) not the whole row.
This code is for my Jbutton for call the action event(I already excluded my code from the JTable since it fetch the Jtable value from my database)
ActionListener tableAction = new GetTableValue();
buttonEdit = new JButton("EDIT");
buttonEdit.addActionListener(tableAction);
the code is plain and simple, I also search Mr. G(google) about a good tutorial on fetching row, unfortunately there isn't a good tutorial for fetching Jtable value(per row).
If you want all the values from selected row then try this code.
int row = jTable1.getSelectedRow();
int column = jTable1.getColumnCount();
for(int i = 0; i < column; i++) {
System.out.println(jTable1.getValueAt(row, i));
}
You get the all values for selected row, no matter how much columns are there in jtable
If you want all the values from jtable then try:
int row = jTable1.getRowCount();
int column = jTable1.getColumnCount();
for (int j = 0; j < row; j++) {
for (int i = 0; i < column; i++) {
System.out.println(jTable1.getValueAt(j, i));
}
}
Yes you can use Object[] to store the values. For example:
Object[] val = new Object[column];
for (int k = 0; k < val.length - 1; k++) {
for (int j = 0; j < row; j++) {
for (int i = 0; i < column; i++) {
val[k] = jTable1.getValueAt(j, i);
System.out.println(val[k]);
}
}
}
getValueAt will return you the value of the cell (at row/col). Unless you're table model supports it, there is no convenient way (beyond what you are doing) to get the whole row in a single request.
Also, remember, if the table is sorted or filtered, the model indices will not match the view, you need to convert them first, using convertRowIndexToModel and convertColumnIndexToModel
UPDATE
The only way around it is if the table model you're using has a getRow (or equivalent) method. Without know how you are storing the data in the table model it's next to near impossible to give an accurate answer, but a general idea would be...
public class MyAwesomeTableModel extends AbstractTableModel {
// All the usual stuff...
public MyRowData getRowAt(int index) { ... }
}
Now, MyRowData is what ever implementation of the table data you've created. It could be (preferably) a single Object or in the case of the DefaultTableModel an array of objects.
class GetTableValue implements ActionListener{
public void actionPerformed(ActionEvent e){
AbstractButton button = (AbstractButton)e.getSource();
if(e.getActionCommand().equals(button.getActionCommand)){
int row = table.convertRowIndexToModel(table.getSelectedRow());
MyAwesomeTableModel model = (MyAwesomeTableModel)table.getModel();
MyRowData data = model.getRowAt(row);
JOptionPane.showMessageDialog(null, data);
}
}
}
This is all dependent on how you've implemented your TableModel and how you've implemented your row data, but that's the general jist
private void jTable1MousePressed(java.awt.event.MouseEvent evt) {
int selectedRow;
ListSelectionModel rowSM = jTable1.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener()
{
#Override
public void valueChanged(ListSelectionEvent e)
{
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
selectedRow = lsm.getMinSelectionIndex();
int numCols = jTable1.getColumnCount();
model = (DefaultTableModel) jTable1.getModel();
System.out.print(" \n row " + selectedRow + ":");
for (int j = 0; j < numCols; j++)
{
System.out.print(" " + model.getValueAt(selectedRow, j));
}
}
});
}
Using this you can get value of whole row where u click on particular row.
you can try the below code to get selected row value:
int selectedRow = jTableName.getSelectedRow();
selectedRow = jTableName.convertRowIndexToModel(selectedRow);
String val1 = (String)jTableName.getModel().getValueAt(selectedRow, 0);
String val2 = (String)jTableName.getModel().getValueAt(selectedRow, 1);

Categories