#How to display a 2D array using JTable and take in data from textFields

61 messages · Page 1 of 1 (latest)

civic heath
#

Hello! I am currently trying to display the results of my calculator using a TableModel.
I know my code looks wacky but I just want to get it working and I'm frustrated.
Thank you so much for anyone that could help me spot any logical errors I may have missed and for helping in solving my predicament.

Currently it only displays the column title .
I appreciate any tips I can get to get my midterms project over. ❤️

vale gazelleBOT
#

Hey, @civic heath!
Please remember to /close this post once your question has been answered!

civic heath
short sparrow
# civic heath

replace Object rows[][] = {} with :

Object rows[][] = Arrays.stream(array).map(x -> Arrays.stream(x).boxed().toArray(Object[]::new)).toArray(Object[][]::new);

This will basically convert your primitive double array to an Object array which you can then pass as constructor argument to the DefaultTableModel.

civic heath
#

Sample outputs:

#

How to display a 2D array using JTable and take in data from textFields

short sparrow
# civic heath Sample outputs:

The code you linked is about GUI and creation of the JTable. ^This however looks like a scanner was used to get the user input.

civic heath
#

however, I need to get it working with GUI 😅

short sparrow
#

have you confirmed the contents of the array passed into the tableView method ?

civic heath
short sparrow
civic heath
#

this is the expected output

#

I'm unsure why this says 'output is always false'

short sparrow
#

because the loop is only up to the length and you check if j is equal to the length.

civic heath
short sparrow
civic heath
#

and afterwards, I wish to display the result into a jtable

short sparrow
#

It seems a little bit convoluted at the moment.

civic heath
#

Forgive me for the rustiness of the code, I'm clearly a novice

short sparrow
#

its not that. I am just trying to understand what we are actually trying to achieve here.

civic heath
#

I need to take in input from my text fields, and from what I have tried it doesn't seem to work. And once passed through a method to solve the matrix, I wish to display the results into a Jtable.

#

Rn, from your previous question wherein you asked me to see if the array has taken an input. Apparently, there is an error within my for-loops and I wish to ask for help regarding so.

short sparrow
civic heath
#

this is the current output in the gui

civic heath
short sparrow
#

I see its just weird that you don't get any kind of output even though the array contains something when the method tableView()is called.

short sparrow
short sparrow
#

What is contained in the array passed to the method. Currently I assign the array to be the data of the table but I dont know if that is what you intended

civic heath
short sparrow
civic heath
short sparrow
civic heath
short sparrow
civic heath
#

to retrieve the text from the input textfields

short sparrow
#

could you give me input and expected output for that for-loop ?

civic heath
#

Example: I input the following in the GUI. What the loops should do is rightHandSide should only contain [21,8]. while leftHandSide should contain [2,18], [1,2]

#

however, this is their current output

short sparrow
#

how are the Jtextfields ordered ?

#

0,1,2 is the first row and 3,4,5 is the second row ?

civic heath
#

yes

short sparrow
#

alright let me quickly think of something

short sparrow
# civic heath Example: I input the following in the GUI. What the loops should do is rightHand...

Alright this code should work

        double[] temp = new double[testnum];
        for(int i = 0, right = 0, left = 0, leftArr = 0; i < input.length; i++ ) {
            if((i+1) % (testnum+1) == 0) {
                rightHandSide[right] = Double.parseDouble(input[i].getText());
                right++;
            } else {
                temp[leftArr] = Double.parseDouble(input[i].getText());
                leftArr++;
                if(leftArr == testnum) {
                    leftHandSide[left] = temp;
                    temp = new double[testnum];
                    leftArr = 0;
                    left++;
                }
            }
        }
civic heath
#

Sorry I cant seem to get my copy paste working but I got an exception error

#
            String[] options = {"Gauss-Seidel", "Jacobi"};
            StringBuilder string = new StringBuilder();
            this.inputPanel.setLayout(new GridLayout(testNum, col));
            JOptionPane.showMessageDialog(null, "Creating matrix");

            //create textField matrix
            for (int a = 0; a < col; a++) {
                for (int b = 0; b < testNum; b++) {
                    input[b] = new JTextField(3);
                    input[b].setPreferredSize(new Dimension(100, 20));
                    if (b == testNum - 1) {
                        string = new StringBuilder("x" + (b + 1) + " " + "=");
                    } else {
                        string = new StringBuilder("x" + (b + 1) + " " + "+");
                    }

                    this.inputPanel.add(input[b]);
                    this.inputPanel.add(new JLabel(String.valueOf(string)));
                }
                input[a] = new JTextField(3);
                this.inputPanel.add(input[a]);
            }```
#

I believe its how my my text fields are initialized

civic heath
#

Update: I was able to fix it by configuring my textfields into a 2d array phew

#

now for my gui