我目前正在研究Eclipse上的应用程序,我需要的是按下一个按钮然后打开一个JFrame,其中包含来自JTable上MySQL DB的值。我最近在这里问了一个关于我在尝试使用绝对布局(空布局)显示列时出现的错误的问题,并且我被告知最好使用布局。问题是我在现有表格上有多个按钮和东西,改变布局会隐藏一些功能,我无法找到解决方案。所以我想到了一些东西。由于JTable存在绝对布局问题,而布局管理器不能帮助我使用我的多个按钮,我可以从现有表单中删除JTable(空布局)并保留按钮。结果,当用户按下"按名称排序"或者"添加控制台"我在现有表单上有mySQL应用程序的按钮,然后会弹出一个新的JFrame并显示正确的JTable。有可能这样做吗?
JTable创建过程:
package newWindow;
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
public class newWindow {
public static void main(String[] args) {
new newWindow();
}
public newWindow() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {}
try
{
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test1?user=mySQL&password=7777");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM consoles INNER JOIN hardware ON consoles.id=hardware.id");
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
String[] cols = new String[columnCount];
int i;
for (i=1;i<= columnCount;i++)
{
cols[i-1] = md.getColumnName(i);
}
DefaultTableModel model = new DefaultTableModel(cols,0);
while (rs.next())
{
Object[] row = new Object[columnCount];
for (i = 1 ; i <= columnCount ; i++)
{
row[i-1] = rs.getObject(i);
}
model.addRow(row);
}
JFrame frame = new JFrame("Promitheas");
JTable table = new JTable(model);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout());
JScrollPane scrollPane = new JScrollPane(table);
frame.getContentPane().add(scrollPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
conn.close();
stmt.close();
rs.close();
} catch (SQLException case1) { case1.printStackTrace();
} catch (Exception case2) { case2.printStackTrace();}
}
});
}
}
按名称按钮排序:
try{
conn = DriverManager.getConnection("jdbc:mysql://localhost/test1?user=mySQL&password=7777");
String[] gender = { "ASC", "DESC"};
int response = JOptionPane.showOptionDialog(null,"Ascending or Descending Order?","Make a choice...",0,JOptionPane.INFORMATION_MESSAGE,null,gender,gender[0]);
stmtsortname = conn.createStatement();
String sql = null;
if (response == JOptionPane.YES_OPTION)
sql = "ASC";
if (response == JOptionPane.NO_OPTION)
sql = "DESC";
rssortname = stmtsortname.executeQuery("SELECT * FROM consoles INNER JOIN hardware ON consoles.id=hardware.id ORDER BY consoles.name "+sql);
.
.
.
create an newJTable overwriting the old
.
.
} catch ...
答案 0 :(得分:0)
创建一个类,将您想要传输的数据作为参数传递。 你的魔法设置表并设置可见的真实并用你的逻辑替换blala
public class NewJFrame extends JFrame{
public NewJFrame(Data data){
blala
this.setVisible(true);
}
}
然后在按钮上有ActionListner或其中的类似内容
new NewJFrame(data);
答案 1 :(得分:0)
答案
JButton btnStartTable = new JButton("Start Table");
btnStartTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0){
try
{
conn = DriverManager.getConnection("jdbc:mysql://localhost/test1?user=mySQL&password=7777");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM consoles INNER JOIN hardware ON consoles.id=hardware.id");
md = rs.getMetaData();
columnCount = md.getColumnCount();
String[] cols = new String[columnCount];
for (i=1;i<= columnCount;i++)
{
cols[i-1] = md.getColumnName(i);
}
model = new DefaultTableModel(cols,0);
while (rs.next())
{
Object[] row = new Object[columnCount];
for (i = 1 ; i <= columnCount ; i++)
{
row[i-1] = rs.getObject(i);
}
model.addRow(row);
}
frame2 = new JFrame();
frame2.setVisible(true);
JTable table = new JTable(model);
f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f2.getContentPane().setLayout(new GridLayout());
JScrollPane scrollPane = new JScrollPane(table);
f2.getContentPane().add(scrollPane);
f2.pack();
f2.setLocationRelativeTo(null);
f2.setVisible(true);
conn.close();
stmt.close();
rs.close();
} catch (SQLException case1) { case1.printStackTrace();
} catch (Exception case2) { case2.printStackTrace();}}
});
btnStartTable.setBounds(10, 11, 126, 23);
frame.getContentPane().add(btnStartTable);