Java - PROBLEMA CON MÉTODOS Y JFRAME

 
Vista:
Imágen de perfil de Luis

PROBLEMA CON MÉTODOS Y JFRAME

Publicado por Luis (2 intervenciones) el 22/09/2017 20:10:22
Estimados, buenas tardes desde Uruguay.

Estoy teniendo un problema a la hora de pasar datos entre un método y otro. Mi intención es utilizar los valores de las variables ruta y libro (líneas 113 y 114), los cuales fueron obtenidos desde un formulario.

public static void LeerArchivo() está para recorrer archivos con un rango determinado con los for. Espero alguien pueda ayudarme. Dejo el código.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import java.awt.Font;
import java.io.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.awt.Toolkit;
 
public class inicial {
static String ruta;
	private JFrame frmDetectarDiferencias;
	private JTextField txtRuta;
	private JTextField txtNomArchivo;
 
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					inicial window = new inicial();
					window.frmDetectarDiferencias.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
 
	/**
	 * Create the application.
	 */
	public inicial() {
		initialize();
	}
	public static void LeerArchivo() throws FileNotFoundException, IOException{
 
		double valor1 = 0, valor2 = 0, dif;
		int i, j; //i es fila j es columna
		String[][] libros = new String[109][8];
		String diferencia;
 
		dif = valor1-valor2;
		diferencia=String.valueOf(dif);
 
		try(FileInputStream file = new FileInputStream(new File(ruta))){
			XSSFWorkbook libro1 = new XSSFWorkbook(file);
			XSSFSheet hoja1 = libro1.getSheet("Hoja1");
			for(i=2;i<=110;i++){
				for(j=1;j<=8;j++){
					Row r = hoja1.getRow(i);
					Cell c = r.getCell(j);
					libros[i-2][j-1]=c.getStringCellValue();
					System.out.println(libros[i-2][j-1]);
				}
			}
		}
	}
 
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmDetectarDiferencias = new JFrame();
		frmDetectarDiferencias.getContentPane().setLocation(0, 0);
		frmDetectarDiferencias.setIconImage(Toolkit.getDefaultToolkit().getImage("P:\\logo1trans.gif"));
		frmDetectarDiferencias.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 10));
		frmDetectarDiferencias.setTitle("DETECTAR DIFERENCIAS");
		frmDetectarDiferencias.setBounds(100, 100, 381, 253);
		frmDetectarDiferencias.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmDetectarDiferencias.getContentPane().setLayout(null);
 
		JPanel panel = new JPanel();
		panel.setBounds(10, 11, 341, 136);
		frmDetectarDiferencias.getContentPane().add(panel);
		panel.setLayout(null);
 
		JLabel lblInstruccion = new JLabel("Carpeta donde est\u00E1 almacenado el archivo:");
		lblInstruccion.setVerticalAlignment(SwingConstants.TOP);
		lblInstruccion.setBounds(10, 11, 339, 23);
		panel.add(lblInstruccion);
 
		txtRuta = new JTextField();
		txtRuta.setFont(new Font("Tahoma", Font.PLAIN, 12));
		txtRuta.setBounds(10, 28, 321, 29);
		panel.add(txtRuta);
		txtRuta.setColumns(10);
 
		JLabel lblNombreDelArchivo = new JLabel("Nombre del archivo:");
		lblNombreDelArchivo.setVerticalAlignment(SwingConstants.TOP);
		lblNombreDelArchivo.setBounds(10, 68, 339, 23);
		panel.add(lblNombreDelArchivo);
 
		txtNomArchivo = new JTextField();
		txtNomArchivo.setFont(new Font("Tahoma", Font.PLAIN, 12));
		txtNomArchivo.setColumns(10);
		txtNomArchivo.setBounds(10, 87, 321, 29);
		panel.add(txtNomArchivo);
		JButton btnAceptar = new JButton("Aceptar");
		btnAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ruta=txtRuta.getText();
				String libro = txtNomArchivo.getText();
 
				try {
					inicial.LeerArchivo();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
 
 
			}
		});
		btnAceptar.setBounds(126, 158, 107, 44);
		frmDetectarDiferencias.getContentPane().add(btnAceptar);
	}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 755
Bronce
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

PROBLEMA CON MÉTODOS Y JFRAME

Publicado por Yamil Bracho (2315 intervenciones) el 22/09/2017 20:43:01
Puedes tenerlas como static (ya tienes ruta) y simplemente haces inicial.ruta y inicial.libro
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
2
Comentar
Imágen de perfil de Luis

PROBLEMA CON MÉTODOS Y JFRAME

Publicado por Luis (2 intervenciones) el 22/09/2017 20:52:52
Me ha funcionado a la perfección. No se me había ocurrido. Gracias por tu colaboración.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar