Converting from Object[][] to ImageIcon - java

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

Related

Java, jTable search

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());
}
}

JavaFx - Removing Node Stored in Array

I have an array of buttons declared as a class variable that is added to a gridPane. When I try to remove the buttons of the array from the gridPane the event handler is being recognized but the node is not being removed.
Any ideas of how to accomplish this?
for(int i = 0; i < Rows; i++) {
for(int j = 0; j < Cols; j++) {
bArray = new Button[Rows][Cols];
bArray[i][j] = new Button();
bArray[i][j].setMinSize(20,20);
bArray[i][j].setMaxSize(25,25);
gridBoard.setVgap(1); //vertical gap in pixels
gridBoard.add(bArray[i][j], j, i);
bArray[i][j].setOnMouseClicked(e->checkNeighbors());
}
}
// Local Method
public static void checkNeighbors() {
// This print out statement is met, but the removal does not occur
System.out.println("Action is called");
gridBoard.getChildren().remove(bArray[0][1]);
gridBoard.getChildren().remove(bArray[0][2]);
gridBoard.getChildren().remove(bArray[0][3]);
}
You are overriding bArray in each iteration and the only valid references that are left at the end of the for loops are from [Rows][0] to [Rows][Cols].
As an example if Rows = 4 and Cols = 4, that would be that at the end the only valid references are [3][0], [3][1], [3][2] and [3][3], and you are trying to remove [0][1], [0][2], [0][3].
You should move the initialization of bArray before the loops start.
bArray = new Button[rows][cols];
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
bArray[i][j] = new Button();
// ......
}

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);

Getting Index of ArrayList of Array

I made an ArrayList of arrays of JTextField.
Now I want to get the value of the first position of the array that was previously stored in the ArrayList.
ArrayList <JTextField []> text_field;
text_field = new ArrayList <JTextField []> ();
I have tried doing this:
text_field.get (row)[column];
But this didn't work. How should I change it?
And how can I add the JTextField onto the panel?
for (int i = 0; i < text_field.size (); i++) {
for (int j = 0; j < 8; j++) {
}
}
Try this:
((JTextField[]) text_field.get(row))[column];

Categories