我需要安装一个屏幕,其中包含我从数据库中搜索过的信息。
为此,我搜索了信息并存储在ArrayList
中。我需要使用for
循环来浏览ArrayList
和每个循环以挂载JTextArea
。
我尝试让for
循环通过ArrayList
然后安装屏幕,但我无法做任何事情。
屏幕代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
public class TelaSugestao extends JFrame implements ActionListener {
public void actionPerformed(ActionEvent e) {}
private JTextArea txtArea;
private Sugestao sugestao;
private Connection conn;
public TelaSugestao(Connection conn) {
super("Tela sugestao");
txtArea = new JTextArea(3,3);
this.conn = conn;
Container caixa = getContentPane();
caixa.setLayout(new FlowLayout());
caixa.add(txtArea);
setSize(432,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
// Trying to mount the screen every time the For runs the ArrayList.
public void montaTela() {
Sugestao sugestao = new Sugestao();
ArrayList<Sugestao> listaSugestao = sugestao.carregaSugestao(conn);
for (int i = 0; i < listaSugestao.size(); i++) {
}
}
}
我正在寻找这种方式的数据库信息:
public ArrayList<Sugestao> carregaSugestao(Connection conn) {
String sqlSelect = "Select id, sugestao from tabelaTeste where id_sugestao = ?";
ArrayList<Sugestao> listaSugestao = new ArrayList<>();
try (PreparedStatement stm = conn.prepareStatement(sqlSelect);) {
stm.setInt(1, getIdSugestao());
try (ResultSet rs = stm.executeQuery();) {
while (rs.next()){
Sugestao s = new Sugestao();
s.setIdSugestao(rs.getInt("id_sugestao"));
s.setSugestao(rs.getString("sugestao"));
listaSugestao.add(s);
guardaSugestao = carregaSugestao(conn);
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (SQLException e1) {
System.out.print(e1.getStackTrace());
}
return listaSugestao;
}
请您解释一下如何做到这一点?
答案 0 :(得分:0)
从构造函数中删除这两行:metadataWindow::metadataWindow(QWidget *parent) : QWidget(parent)
{
this->setupUI(); // not shown here, but just basic QLabel stuff
}
void metadataWindow::setupListener()
{
const char *metadata_file = "/tmp/my-pipe-file";
// vvvvv This here is blocking vvvvvv
FILE *fd = fopen(metadata_file, "r");
pipe = new QTextStream(fd);
streamReader = new QSocketNotifier(fileno(fd), QSocketNotifier::Read, qApp);
QObject::connect(streamReader, SIGNAL(activated(int)), this, SLOT(onData()));
streamReader->setEnabled(true);
}
方法现在更改为:
txtArea = new JTextArea(3,3); caixa.add(txtArea);
并调用方法: public void montaTela(Container pane){
Sugestao sugestao = new Sugestao();
ArrayList<Sugestao> listaSugestao = sugestao.carregaSugestao(conn);
for(int i = 0; i < listaSugestao.size(); i++){
txtArea=new JTextArea();
//add Text to your txtArea with .append(string)
pane.add(txtarea);
}
}
&lt; - 需要在构造函数中调用。否则为内容窗格制作一个getter