如何使用mongo驱动程序在java中填充jtable
我正在尝试使用java中的mongo驱动程序从获得的数据填充jtable,以下是我的代码:如何使用mongo驱动程序在java中填充jtable,java,swing,mongodb,Java,Swing,Mongodb,我正在尝试使用java中的mongo驱动程序从获得的数据填充jtable,以下是我的代码: Pattern pattern = Pattern.compile(".*"+textArea.getText()+".*", Pattern.CASE_INSENSITIVE); BasicDBObject query = new BasicDBObject("content:encoded", pattern); DBCursor cursor = blogTable.find(query); w
Pattern pattern = Pattern.compile(".*"+textArea.getText()+".*", Pattern.CASE_INSENSITIVE);
BasicDBObject query = new BasicDBObject("content:encoded", pattern);
DBCursor cursor = blogTable.find(query);
while (cursor.hasNext())
{
chatArea.append("\n"+cursor.next().toString()); //I want to populate the jtable here instead of just appending in the JtextArea
}//End of while
此外,我不能投影我想要的字段,我的意思是我只需要一列(即标题),但我得到结果中的所有列
请帮忙
感谢阅读JTabel
下面是一个简单的例子:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Example extends JFrame {
private DefaultTableModel model;
public Example() {
JTable table = new JTable(model = new DefaultTableModel(new Object[][]{},new Object[]{"data"}));
add(new JScrollPane(table));
JButton populate = new JButton("populate");
populate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
//Pattern pattern = Pattern.compile(".*"+textArea.getText()+".*", Pattern.CASE_INSENSITIVE);
//BasicDBObject query = new BasicDBObject("content:encoded", pattern);
//DBCursor cursor = blogTable.find(query);
//while (cursor.hasNext()) {
// model.addRow(new Object[]{cursor.next().toString()});
//}
}
});
add(populate,BorderLayout.NORTH);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
Example frame = new Example();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
填充
按钮从BD和中获取数据,同时
循环向表模型添加新行。读取JTabel
下面是一个简单的例子:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Example extends JFrame {
private DefaultTableModel model;
public Example() {
JTable table = new JTable(model = new DefaultTableModel(new Object[][]{},new Object[]{"data"}));
add(new JScrollPane(table));
JButton populate = new JButton("populate");
populate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
//Pattern pattern = Pattern.compile(".*"+textArea.getText()+".*", Pattern.CASE_INSENSITIVE);
//BasicDBObject query = new BasicDBObject("content:encoded", pattern);
//DBCursor cursor = blogTable.find(query);
//while (cursor.hasNext()) {
// model.addRow(new Object[]{cursor.next().toString()});
//}
}
});
add(populate,BorderLayout.NORTH);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
Example frame = new Example();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
填充
按钮从BD获取数据,并在中循环向表模型添加新行。要获取字段的投影,应传递DBObject进行投影
DBCursor cursor=collection.find(查询,projectionQuery)代码>
投影是键值对形式的DBObject。
在哪里,
key是要投影的字段的名称。
值可以是0或1。
0-表示从结果集中排除特定列。
1-表示结果集中包含特定列
有关更多信息,请参见。有关获取字段投影的信息,应传递DBObject进行投影
DBCursor cursor=collection.find(查询,projectionQuery)代码>
投影是键值对形式的DBObject。
在哪里,
key是要投影的字段的名称。
值可以是0或1。
0-表示从结果集中排除特定列。
1-表示结果集中包含特定列
有关更多信息,请参阅。谢谢,这很有效,我知道我很贪婪,但你知道我如何在mongo查询结果中投射我的结果吗?如果没有,我帮不了你。谢谢,这很有效,我知道我很贪婪,但你知道我如何在mongo查询结果中投射我的结果吗,但是我帮不了你。