Em meu projeto faço a criação de um JDialog e outros componentes(ScrollPane, Panel, Jlabels e JTextField) dentro dele em tempo de execução baseados nos valores de uma matriz que construo com o decorrer da programação da aplicação.
Minha dúvida é a seguinte, nessa nova interface que criei eu precisava alterar o valor do JTextField e gravá-lo assim geraria meu arquivo .json. Isso é possível?
Fico no aguardo e desde já agradeço.
É uma pergunta a ser feita ao seu espelho.
Como é que você referencia os componentes criados em tempo de execução? Guarda referências a ele em listas, ou em variáveis?
A principio guardei a criação dos componentes em uma matriz.
Vou postar o código para melhor entendimento:
package com.fourspaces.couchdb.view;
import com.fourspaces.couchdb.Database;
import com.fourspaces.couchdb.Document;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class AddToJScrollPane {
/**
* @param args
*/
private static Document doc;
private static Database db;
private static JFrame frame;
public AddToJScrollPane(Frame parent){
this.doc = ((NewJFrame)parent).doc;
this.db = ((NewJFrame)parent).db;
this.frame = ((NewJFrame)parent);
}
public static void main(String[] args) {
final JDialog dialog = new JDialog(frame, true);
JScrollPane jScrollPane = new JScrollPane();
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.setViewportBorder(new LineBorder(Color.RED));
JPanel pContainer = new JPanel();
GridLayout experimentLayout = new GridLayout(0,2);
pContainer.setLayout(experimentLayout);
String[] t = doc.toString().split(Pattern.quote(","));
String dad = "";
final String w[][] = new String[t.length][2];
for (int i=0; i<t.length;i++){
System.out.println(t[i].toString());
w[i] = t[i].toString().split(Pattern.quote(":"));
}
final String d[][] = new String[w.length][2];
final Object componentes[][] = new Object[w.length][2];
for(int i=0;i<w.length;i++){
for(int j=0;j<2;j++){
if(j==0){
d[i][j] = w[i][j].toString().replaceAll(Pattern.quote("\""), "").replaceAll(Pattern.quote("{"), "");
JLabel key = new JLabel(String.valueOf(d[i][j]));
pContainer.add(key, BorderLayout.LINE_START);
componentes[i][j] = key.getText();
} else{
d[i][j] = w[i][j].toString().replaceAll(Pattern.quote("\""), "").replaceAll(Pattern.quote("}"), "");
JTextField value = new JTextField("Value:");
value.setText(String.valueOf(d[i][j]));
if ((j-1==0) && (d[i][j-1].equals("_rev")))
value.setEnabled(false);
pContainer.add(value, BorderLayout.LINE_END);
componentes[i][j] = value.getText();
}
}
}
JPanel pContainer2 = new JPanel();
GridLayout experimentLayout2 = new GridLayout(0,2);
pContainer2.setLayout(experimentLayout2);
/***************************************************************/
JScrollPane ScrollTroll = new JScrollPane(pContainer);
JPanel jPanel2 = new JPanel();
jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
JButton jbSave = new JButton("Save Document");
jbSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i=0; i<componentes.length;i++){
for(int j=0;j<2;j++){
//doc.put( d[i][j].toString(),d[i][j].toString() );
System.out.println(">>"+String.valueOf(componentes[i][j].toString()));
}
}
try {
db.saveDocument(doc);
dialog.dispose();
} catch (IOException ex) {
Logger.getLogger(AddToJScrollPane.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
JButton jbCancel = new JButton("Cancel");
jbCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.dispose();
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(108, Short.MAX_VALUE)
.addComponent(jbSave)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbCancel)
.addGap(116, 116, 116))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jbSave)
.addComponent(jbCancel))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(dialog.getContentPane());
dialog.getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(ScrollTroll, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(ScrollTroll, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
/***************************************************************/
dialog.setResizable(false);
dialog.setSize(500, 500);
dialog.setAlwaysOnTop(true);
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
}
RESOLVIDO!
A resposta estava debaixo do meu nariz…
Segue código:
@Override
public void actionPerformed(ActionEvent e) {
String key = null, value = null;
for (int i=0; i<componentes.length;i++){
for(int j=0;j<2;j++){
if(j==0)
//System.out.println(">>"+String.valueOf(((JLabel)componentes[i][j]).getText()));
key = String.valueOf(((JLabel)componentes[i][j]).getText());
else
value = String.valueOf(((JTextField)componentes[i][j]).getText());
}
if (!key.equals(""))
doc.put( key, value);
}
try {
db.saveDocument(doc);
dialog.dispose();
} catch (IOException ex) {
Logger.getLogger(AddToJScrollPane.class.getName()).log(Level.SEVERE, null, ex);
}
}