以下是我的程序代码。
JTable
没有使用SQL语句的输出进行更新。
在Swing GUI上没有填充JTable
。
import java.net.UnknownHostException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Properties;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.ImageObserver;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.text.DefaultCaret;
public class GUIFrameExtenderImpl extends JFrame implements ActionListener {
/**
*
*/
public static AnimatedLabel busyIcon;
public static JLabel busyLabelText;
public static Thread busyThread = null;
public static Connection staticConnection=null;
private static final long serialVersionUID = 1L;
static JTable table ;
static QueryTableModel qtm;
public static String DATABASE_URL_CONNECTION = null;
private static JButton executeButton = new JButton("Execute");
private static JTextField machineName = new JTextField(50);
static JScrollPane scrollBar = new JScrollPane(table,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
private static JTextField machineNameField = new JTextField(20);
public static void addComponentsToPane(Container pane) {
pane.setLayout(null);
// Create all the labels, combo boxes, buttons, and text fields/areas
JLabel comboAndTextLabel = new JLabel("Infra Deployer");
JLabel driverLabel = new JLabel("Machine:");
//JLabel SQLResultLabel = new JLabel("SQL Execution Result");
/*JButton clearButton = new JButton("Clear Debug Table");
JButton clearResultArea = new JButton("Clear Result Window");
JButton executeButton = new JButton("Run Postscripts");*/
qtm = new QueryTableModel();
table = new JTable(qtm);
busyIcon=new AnimatedLabel("./images/", 100,12) ;
busyLabelText=new JLabel("Processing...");
pane.add(machineName);
pane.add(comboAndTextLabel);
pane.add(driverLabel);
//pane.add(table);
pane.add(machineNameField);
pane.add(executeButton);
//pane.add(new JLabel("HELLOOOO")) ;
//pane.add(busyIcon);
//pane.add(busyLabelText);
////busyIcon.setVisible(false);
// busyLabelText.setVisible(false);
pane.add(table);
pane.add(scrollBar);
machineName.setEditable(false);
Insets insets = pane.getInsets();
// The next giant block of code takes each component and moves them to
// the
// desired space on the GUI. It uses absolute positioning
// This section moves the 5 labels on the top left
comboAndTextLabel
.setFont(comboAndTextLabel.getFont().deriveFont(15.0f));
Dimension size = comboAndTextLabel.getPreferredSize();
comboAndTextLabel.setBounds(8 + insets.left, 0 + insets.top,
size.width, size.height + 2);
size = driverLabel.getPreferredSize();
driverLabel.setBounds(8 + insets.left, 26 + insets.top, size.width,
size.height);
// This section moves the combo boxes and the text fields on the top
// left
size = machineName.getPreferredSize();
machineName.setBounds(90 + insets.left, 22 + insets.top,
size.width - 257, size.height);
size = machineNameField.getPreferredSize();
machineNameField.setBounds(4440 + insets.right, 23 + insets.top,
size.width - 1, size.height + 3);
machineNameField.setEditable(false);
table(8 + insets.left, 17 + insets.bottom,
size.width + 1500, size.height + 950);
table.setDragEnabled(false);
// consoleOutput.setText("asdasdasfdsadfsdfsdfsdfsd");
size = scrollBar.getPreferredSize();
scrollBar.setBounds(8 + insets.left, 50 + insets.bottom,
size.width + 1900, size.height + 890);
size = executeButton.getPreferredSize();
executeButton.setBounds(400 + insets.right, 20 + insets.top,
size.width , size.height );
// size = scrollingArea.getPreferredSize();
// scrollingArea.setBounds(8 + insets.left, 217 + insets.bottom,
// size.width + 769, size.height + 249);
// puts border around result text area
//
// size = clearResultButton.getPreferredSize();
// clearResultButton.setBounds(15 + insets.left, 820 + insets.top,
// size.width, size.height);
java.net.InetAddress localMachine = null;
try {
localMachine = java.net.InetAddress.getLocalHost();
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
machineName.setText(localMachine.getHostName());
executeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//DataModify dm = new DataModify();
//dm.viewwindow();
qtm.setHostURL("jdbc:oracle:thin:@indlin2338:1521:OPTABP9");
qtm.setQuery("select * from ape3_epcext_offer_Details where offer_id = '8455034'");
// //table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
System.out.println("starting" + qtm.getColumnCount());
}
});
}
private static void table(int i, int j, int k, int l) {
// TODO Auto-generated method stub
}
public static Connection getDBConnection(String driver, String url,String username,String password,boolean isDBConnected)
{
Connection con = null;
if (isDBConnected==true){
try
{
Class.forName(driver);
con = DriverManager.getConnection(url,username,password);
}
catch (Exception e)
{
System.out.println("Exception from getDBConnection()");
e.printStackTrace();
}
}
return con;
}
// Create the GUI and show it.
static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("Infra Deployer Application 1.0");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Either using insets to shape and define size of frame.
//Insets insets = frame.getInsets();
// frame.setSize(1200 + insets.left + insets.right + 240, 850 + insets.top
// + insets.bottom);
//We are using Dimension and overriding the frame not to pack and set the size according to end user screen resolution.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setBounds(0,0,screenSize.width, screenSize.height);
frame.setVisible(true);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
//frame.pack(); // Don't pack when using Dimension, pack when using insets
frame.setResizable(false);
// Set up the content pane.
addComponentsToPane(frame.getContentPane());
// Size and display the window.
table.revalidate();
frame.setVisible(true);
// dispose of window when user quits application (this overrides
// the default of HIDE_ON_CLOSE)
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
// ensure database connection is closed when user quits application
frame.addWindowListener(new WindowAdapter() {
// disconnect from database and exit when window has closed
public void windowClosed(WindowEvent event) {
//tableModel.disconnectFromDatabase();
System.exit(0);
}
});
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}