【发布时间】:2018-03-30 23:23:56
【问题描述】:
我知道有很多同名的问题,我都阅读了它们,但没有一个对我有用。我的问题是,起初 JTextArea 必须为空,但是当我单击 JList 元素时它必须显示气象预测。但是当我用他的监听器单击 JList 上的那个元素时,JTextArea 不会更新,它保持为空。这是我的代码:
public void textoInfoMunicipio (){
//JTextArea area = new JTextArea(30,50);
area.setEditable(false);
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(area,BorderLayout.CENTER);
content.setBorder(BorderFactory.createLineBorder(Color.black)); //con esto le ponemos un borde negro en los margenes del JPANEL
content.setBorder(BorderFactory.createTitledBorder("Predicciones"));
IPrediccionService ps = new AEMETPrediccionService();
List<IPrediccion> lista;
try {
if(arranque_app == false && municipio_seleccionado != null){ // si la aplicacion ya ha arrancado, entonces podemos mostrar la info de las predicciones
String header = ""
+ "********************************************************************* \n"
+ "Predicción para el municipio de "+municipio_seleccionado.getNombre()+"\n"
+ "********************************************************************* \n"
+ "Fecha \t Temp.mín/máx \t Estado del cielo \n";
lista = ps.getPrediccionesByIdMunicipio(municipio_seleccionado.getCodMunicipio()); //DESCOMENTAR, ES PA AHORRAR PETICIONES AL SERVIDOR AEMET
String predicciones = "";
// Recorrido de la lista mostrando solamente las predicciones
for ( IPrediccion p : lista )
predicciones += p.getFecha()+"\t"+p.getTemperaturaMinima()+"/"+p.getTemperaturaMaxima()+"\t\t"+p.getEstadoCielo()+"\n";
area.setText(header+predicciones); //funciona, se ve bien
//content.revalidate();
//content.repaint();
//area.update(area.getGraphics());
}
} catch (Exception e) { //PrediccionServiceException e
e.printStackTrace();
}
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 3;
gbc.gridheight = 8;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
panel_principal.add(content, gbc);
}
In the listener event, I mean, when an element in JList is selected, I call the function above.希望你能帮助我
【问题讨论】:
-
您是否检查过单击 JList 项是否真的进入了侦听器,并带有某种 println
-
如需尽快获得更好的帮助,请发帖minimal reproducible example 或Short, Self Contained, Correct Example。
-
是的,我调试了它,我在 if 和它进入的那行里面放了一个断点,这样代码就可以执行了!
-
是的,它进入了我拥有的监听器