1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
| /*
* Created on Apr 5, 2011
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package day1;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Test extends JFrame {
private JPanel jContentPane = null;
private JComboBox jComboBox = null;
Vector dataCombo = new Vector();
private final Vector columnNames = new Vector();
Vector columnData = new Vector();
private JScrollPane jScrollPane = null;
private JTable jTable = null;
public static void main(final String[] args) {
final Test cn = new Test();
}
/**
* This is the default constructor
*/
public Test() {
super();
initialize();
getDataListCombo();
getDataList();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(432, 200);
this.setContentPane(getJContentPane());
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJComboBox(), null);
jContentPane.add(getJScrollPane(), null);
}
return jContentPane;
}
public void getDataListCombo() {
//item = (String) jComboBox.getSelectedItem();
final int countRows = 0;
// System.out.println(countRows + "----" + item);
// final String data = "jdbc:odbc:WorldEnergy";
// try {
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// final Connection conn = DriverManager.getConnection(data, "", "");
// final Statement st = conn.createStatement();
// final ResultSet rec =
// st.executeQuery("SELECT DISTINCT Country FROM Coal ORDER BY Country");
//
// while (rec.next()) {
// countRows++;
// dataCombo.addElement(rec.getObject(1));
// }
// st.close();
//
// } catch (final SQLException s) {
// System.out.println("SQL Error: "+ s.toString()+ " " + s.getErrorCode()
// + " "
// + s.getSQLState());
// } catch (final Exception e) {
// System.out.println("Error: " + e.toString() + e.getMessage());
// }
dataCombo.addElement("France");
dataCombo.addElement("Germany");
getJComboBox();
}
public void getDataList() {
//item = (String) jComboBox.getSelectedItem();
final int countRows = 0;
//System.out.println(countRows + "----" + item);
// final String data = "jdbc:odbc:WorldEnergy";
// try {
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// final Connection conn = DriverManager.getConnection(data, "", "");
// final Statement st = conn.createStatement();
// final ResultSet rec =
// st.executeQuery("SELECT FIPS, Country, Year FROM Coal " +
// //"WHERE " + "(Country='" + item + "') " +
// "ORDER BY Country");
//
// final ResultSetMetaData md = rec.getMetaData();
// final int columns = md.getColumnCount();
// final Vector columnNames = new Vector(columns);
// for (int i = 1; i <= columns; i++) {
// columnNames.addElement(md.getColumnName(i).toString());
// }
// while (rec.next()) {
// countRows++;
// final Vector row = new Vector(columns);
// for (int i = 1; i <= columns; i++) {
// row.addElement(rec.getObject((i)));
// }
// columnData.addElement(row);
//
// }
// st.close();
//
// } catch (final SQLException s) {
// System.out.println("SQL Error: "+ s.toString()+ " " + s.getErrorCode()
// + " "
// + s.getSQLState());
// } catch (final Exception e) {
// System.out.println("Error: " + e.toString() + e.getMessage());
// }
columnNames.addElement("FIPS");
columnNames.addElement("Country");
columnNames.addElement("Year");
if (jComboBox.getSelectedIndex() == -1) { // au premier passage, afficher tous
Vector row = new Vector(3);
row = new Vector(3);
row.add("0");
row.add("France");
row.add("2009");
columnData.addElement(row);
row = new Vector(3);
row.add("1");
row.add("France");
row.add("2010");
columnData.addElement(row);
row = new Vector(3);
row.add("2");
row.add("France");
row.add("2011");
columnData.addElement(row);
row = new Vector(3);
row.add("3");
row.add("Germany");
row.add("2009");
columnData.addElement(row);
row = new Vector(3);
row.add("4");
row.add("Germany");
row.add("2010");
columnData.addElement(row);
row = new Vector(3);
row.add("5");
row.add("Germany");
row.add("2011");
columnData.addElement(row);
}
if (jComboBox.getSelectedIndex() == 0) { // Afficher uniquement 'France'
Vector row = new Vector(3);
row = new Vector(3);
row.add("0");
row.add("France");
row.add("2009");
columnData.addElement(row);
row = new Vector(3);
row.add("1");
row.add("France");
row.add("2010");
columnData.addElement(row);
row = new Vector(3);
row.add("2");
row.add("France");
row.add("2011");
columnData.addElement(row);
}
if (jComboBox.getSelectedIndex() == 1) { //Afficher uniquement 'Germany'
Vector row = new Vector(3);
row = new Vector(3);
row.add("3");
row.add("Germany");
row.add("2009");
columnData.addElement(row);
row = new Vector(3);
row.add("4");
row.add("Germany");
row.add("2010");
columnData.addElement(row);
row = new Vector(3);
row.add("5");
row.add("Germany");
row.add("2011");
columnData.addElement(row);
}
getJTable();
}
/**
* This method initializes jComboBox
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBox() {
if (jComboBox == null) {
jComboBox = new JComboBox();
final DefaultComboBoxModel model =
new DefaultComboBoxModel(dataCombo);
jComboBox = new JComboBox(model);
jComboBox.setBounds(185, 17, 223, 25);
jComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(
final java.awt.event.ActionEvent e) {
if (jComboBox.getSelectedItem().equals("USA")) {
//getDataList();
System.out.println(columnData.get(2));
}
getDataList();
}
});
}
return jComboBox;
}
/**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
private JTable getJTable() {
//DefaultTableModel td = new DefaultTableModel(columnData);
final Vector cl = new Vector();
cl.addElement("Column AFC");
cl.addElement("Column Country");
cl.addElement("Column Year");
if (jTable == null) {
jTable = new JTable();
//jTable = new JTable(columnData,columnNames);
jTable = new JTable(columnData, cl);
}
return jTable;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
//getDataList();
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(15, 61, 394, 99);
jScrollPane.setViewportView(getJTable());
repaint();
}
//jScrollPane.setViewportView(getJTable());
return jScrollPane;
}
} |
Partager