如何使用 SWT 和 MySql 在树视图中显示列表值
How to display list values in tree view using SWT and MySql
请原谅我的英语不好。
java 和 SWT 的新功能。
我试图在 SWT 中而不是在 jface 中显示树视图。从数据库获取值,我应该显示树视图。
树状视图,
模板 -
页码 -
布局编号-
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class TreeEx
{
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/Tree";
static final String USER = "root";
static final String PASS = "root123";
public static void main(String[] args)
{
Connection conn = null;
Statement stmt = null;
int TemplateID=0;
int templateId =0 ;
int pageNumber = 0;
int PageNumber=0;
int layoutId = 0;
int LayoutId=0;
List<Integer> a1 = new ArrayList();
List a2 = new ArrayList();
List a3 = new ArrayList();
try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql = "SELECT templateID,PageNumber,layoutid FROM templatepage, templatelayout WHERE templatelayout.pageid = templatepage.PageNumber";
String sql1="SELECT count(distinct templateID )TemplateID FROM templatepage, templatelayout WHERE templatelayout.pageid = templatepage.PageNumber";
String sql2="SELECT count(distinct PageNumber )PageNumber FROM templatepage, templatelayout WHERE templatelayout.pageid = templatepage.PageNumber";
String sql3="SELECT count(distinct layoutid )LayoutId FROM templatepage, templatelayout WHERE templatelayout.pageid = templatepage.PageNumber";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next())
{
templateId=rs.getInt("templateID");
pageNumber=rs.getInt("PageNumber");
layoutId=rs.getInt("layoutid");
a1.add(rs.getInt("templateID"));
a2.add(rs.getInt("PageNumber"));
a3.add(rs.getInt("layoutid"));
System.out.print("templateID: " + templateId);
System.out.print(", PageNumber: " + pageNumber);
System.out.print(", layoutid: " + layoutId+"\n");
System.out.println(a1);
}
rs.close();
ResultSet rs1 = stmt.executeQuery(sql1);
while(rs1.next())
{
TemplateID=rs1.getInt("TemplateID");
System.out.println("Distinct TemplateID"+TemplateID);
}
rs1.close();
ResultSet rs2 = stmt.executeQuery(sql2);
while(rs2.next())
{
PageNumber=rs2.getInt("PageNumber");
System.out.println("Distinct PageNumber"+PageNumber);
}
rs2.close();
ResultSet rs3 = stmt.executeQuery(sql3);
while(rs3.next())
{
LayoutId=rs3.getInt("LayoutId");
System.out.println("Distinct LayoutId"+LayoutId);
}
rs3.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if(stmt!=null)
conn.close();
}catch(SQLException se)
{
}
try
{
if(conn!=null)
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
}
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("SWT Trees");
shell.setLayout(new FillLayout());
shell.setSize(400, 300);
Tree tree = new Tree(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
for (int i = 0; i <TemplateID; i++)
{
TreeItem treeItem = new TreeItem(tree, 0);
treeItem.setText("template "+a1);
for (int j = 0; j <PageNumber; j++)
{
TreeItem subTreeItem = new TreeItem(treeItem, SWT.NONE);
subTreeItem.setText("pageNumber " +a2);
for (int k = 0; k <LayoutId; k++)
{
TreeItem subSubTreeItem = new TreeItem(subTreeItem, SWT.NONE);
subSubTreeItem.setText("layoutid "+a3);
System.out.println("**********"+layoutId);
}
}
}
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
我的代码显示如下,
Temaple[1,1,1]
PageNumber[1,1,1,]
layoutid[1,2,3]
layoutid[1,2,3]
layoutid[1,2,3]
It should be
Template1
Pagenumber1
layoutid3
layoutid3
layoutid3
好的。很难找出问题所在,因为有一些不相关的代码。首先,请不要发送 SQL 查询,尝试对问题建模。但。如果是这样,请在 finally{} 块中调用类似 close() 的方法。 ;)
问题是您有 for 循环,但您没有以正确的方式执行它。您循环了三次,但值与您使用的值相同(完整的 ArrayList)。你通过 a1 得到完整的数组;
使用 get() 方法获取实际元素或使用 for-each 循环!
treeItem.setText("template " + a1.get(i));
请原谅我的英语不好。 java 和 SWT 的新功能。 我试图在 SWT 中而不是在 jface 中显示树视图。从数据库获取值,我应该显示树视图。
树状视图,
模板 - 页码 - 布局编号-
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class TreeEx
{
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/Tree";
static final String USER = "root";
static final String PASS = "root123";
public static void main(String[] args)
{
Connection conn = null;
Statement stmt = null;
int TemplateID=0;
int templateId =0 ;
int pageNumber = 0;
int PageNumber=0;
int layoutId = 0;
int LayoutId=0;
List<Integer> a1 = new ArrayList();
List a2 = new ArrayList();
List a3 = new ArrayList();
try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql = "SELECT templateID,PageNumber,layoutid FROM templatepage, templatelayout WHERE templatelayout.pageid = templatepage.PageNumber";
String sql1="SELECT count(distinct templateID )TemplateID FROM templatepage, templatelayout WHERE templatelayout.pageid = templatepage.PageNumber";
String sql2="SELECT count(distinct PageNumber )PageNumber FROM templatepage, templatelayout WHERE templatelayout.pageid = templatepage.PageNumber";
String sql3="SELECT count(distinct layoutid )LayoutId FROM templatepage, templatelayout WHERE templatelayout.pageid = templatepage.PageNumber";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next())
{
templateId=rs.getInt("templateID");
pageNumber=rs.getInt("PageNumber");
layoutId=rs.getInt("layoutid");
a1.add(rs.getInt("templateID"));
a2.add(rs.getInt("PageNumber"));
a3.add(rs.getInt("layoutid"));
System.out.print("templateID: " + templateId);
System.out.print(", PageNumber: " + pageNumber);
System.out.print(", layoutid: " + layoutId+"\n");
System.out.println(a1);
}
rs.close();
ResultSet rs1 = stmt.executeQuery(sql1);
while(rs1.next())
{
TemplateID=rs1.getInt("TemplateID");
System.out.println("Distinct TemplateID"+TemplateID);
}
rs1.close();
ResultSet rs2 = stmt.executeQuery(sql2);
while(rs2.next())
{
PageNumber=rs2.getInt("PageNumber");
System.out.println("Distinct PageNumber"+PageNumber);
}
rs2.close();
ResultSet rs3 = stmt.executeQuery(sql3);
while(rs3.next())
{
LayoutId=rs3.getInt("LayoutId");
System.out.println("Distinct LayoutId"+LayoutId);
}
rs3.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if(stmt!=null)
conn.close();
}catch(SQLException se)
{
}
try
{
if(conn!=null)
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
}
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("SWT Trees");
shell.setLayout(new FillLayout());
shell.setSize(400, 300);
Tree tree = new Tree(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
for (int i = 0; i <TemplateID; i++)
{
TreeItem treeItem = new TreeItem(tree, 0);
treeItem.setText("template "+a1);
for (int j = 0; j <PageNumber; j++)
{
TreeItem subTreeItem = new TreeItem(treeItem, SWT.NONE);
subTreeItem.setText("pageNumber " +a2);
for (int k = 0; k <LayoutId; k++)
{
TreeItem subSubTreeItem = new TreeItem(subTreeItem, SWT.NONE);
subSubTreeItem.setText("layoutid "+a3);
System.out.println("**********"+layoutId);
}
}
}
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
我的代码显示如下,
Temaple[1,1,1]
PageNumber[1,1,1,]
layoutid[1,2,3]
layoutid[1,2,3]
layoutid[1,2,3]
It should be
Template1
Pagenumber1
layoutid3
layoutid3
layoutid3
好的。很难找出问题所在,因为有一些不相关的代码。首先,请不要发送 SQL 查询,尝试对问题建模。但。如果是这样,请在 finally{} 块中调用类似 close() 的方法。 ;)
问题是您有 for 循环,但您没有以正确的方式执行它。您循环了三次,但值与您使用的值相同(完整的 ArrayList)。你通过 a1 得到完整的数组; 使用 get() 方法获取实际元素或使用 for-each 循环!
treeItem.setText("template " + a1.get(i));