Constructing a java table from a vector as parameter

Asked

Viewed 587 times

0

Hello! I’m having trouble constructing a table. Here’s the thing, the user defines a series of variables that are stored in an array. I would like the variables stored in this vector to constitute the name of the table columns. The problem does not involve sql, but variables passed through a java interface built by neatbeans. Thanks in advance.

a string vector (called column) is used with the function Concatena vector, which takes two arraysLists and stores in a third. Then go to the result arrayList. the values of the result arraylist is passed to the vector string str, returning its value to the variable column.

It follows below the Code:

Arrays declaration:

public ArrayList<String> colInput = new ArrayList<String>();
public ArrayList<String> colOutput = new ArrayList<String>();

Storage in the arrays:

private void jButton1AddInputActionPerformed(java.awt.event.ActionEvent evt) {                                                 
       // TODO add your handling code here:
       int i = jList4.getSelectedIndex();
       if (i == -1) {
         JFrame frame = new JFrame("Warning");
         JOptionPane.showMessageDialog(frame, palavras.getString("Please, select a variable"));
       } else {
         tipoBotao = INICIAL;
         String aux = Table.getVars().get(i).toString();
         colInput.add(aux); 
         //System.out.println(colInput.get(j)); j++;
         pressionarVariavelInput(aux);
         escreverInput();
         //escreverFormula();
     }
 } 


 private void jButton2AddOutputActionPerformed(java.awt.event.ActionEvent evt) {                                                  
       // TODO add your handling code here:
       int i = jList4.getSelectedIndex();
       if (i == -1) {
           JFrame frame = new JFrame("Warning");
           JOptionPane.showMessageDialog(frame, palavras.getString("Please, select a variable"));
       } else {
           tipoBotao = INICIAL;
           String aux = Table.getVars().get(i).toString();
           colOutput.add(aux);
           //System.out.println(colOutput.get(k));k++;
           pressionarVariavelOutput(aux);
           escreverOutput();
           //escreverFormula();
       }
   }       

Table call:

String[] coluna = this.ConcatenaVetor(colInput, colOutput);
Object dados[][]= new Object[][]{};       
jTable2 = new javax.swing.JTable(dados, coluna);
jScrollPane7.setViewportView(jTable2);

Function Concatenate vector:

String[] ConcatenaVetor(ArrayList colInput, ArrayList colOutput){
   int tam = colInput.size() + colOutput.size();
    ArrayList<String> resultado = new ArrayList<String>();
    resultado.addAll(colInput);
    resultado.addAll(colOutput);
    String[] str= new String[10];
    for(int i = 0; i < colInput.size(); i++){   
        str[i] = resultado.get(i);  
     }
    return str;

 } 
  • Add the code in the question, without seeing it is difficult to deduce a possible solution. Preferably, a code that is reproducible

  • 1
  • Renan, it is a little difficult to help without any code, try to post the declaration of your method (as you receive this vector)

  • This question seems to be duplicate the other, but since the other one was not well formulated and already has an answer, it is better to leave this one. Renan, what problem faced in the added code?

  • Exception in thread "AWT-Eventqueue-0" java.lang.Nullpointerexception at javax.swing.Jtable$1.getColumnName(Jtable.java:685)

  • Renan this piece of error does not help to identify the problem. Add at the end of the question the complete stack of errors. Your code is not reproducible, add a code that is possible to simulate your screen.

  • Solved. I used the Defaulttablemodel creation to create columns as the user sets the variables. Only by removing the initial table content and creating an empty table.

  • @Renanalboy Add an answer to this question with your solution.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.