简体   繁体   English

如何使用mongo驱动程序在Java中填充jtable

[英]how to populate a jtable in java using mongo driver

I am trying to populate a jtable from the data I get using mongo driver in java ,This is my code: 我正在尝试使用在Java中使用mongo驱动程序获得的数据填充一个jtable,这是我的代码:

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

Also I can't project the field(s) I want , I mean I only need one column (ie title) but I get all the columns in the result 我也不能投影我想要的字段,我的意思是我只需要一列(即标题),但是我得到了结果中的所有列

Please help, 请帮忙,

Thanks 谢谢

Read tutorial for JTabel . 阅读JTabel 教程

Here is simple example for you: 这是为您提供的简单示例:

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);
            }
        });
    }
}

populate button get data from your BD and in while loop add new rows to table model. populate按钮从BD获取数据,然后在while循环中向表模型添加新行。

For getting the projection of fields, you should pass the DBObject for projection, 为了获得字段的投影,您应该传递DBObject进行投影,

DBCursor cursor = collection.find(query, projectionQuery);

The projection is The DBObject in form of key-value pair. 投影是键-值对形式的DBObject。 where, 哪里,

key is the name of field you want to project. key是您要投影的字段的名称。 value can be either 0 or 1. 值可以是0或1。
0 - means exclude the particular column from result set. 0-表示从结果集中排除特定的列。
1 - means include the particular column in result set. 1-表示在结果集中包含特定的列。

For more info, see here . 有关更多信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM