@Override public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column) { JFormattedTextField ftf = (JFormattedTextField) getComponent(); ftf.setFormatterFactory(null); ftf.setValue(value); ftf.setFormatterFactory(formatterFactory); return super.getTableCellEditorComponent(table, value, isSelected, row, column); }
@Override public boolean stopCellEditing() { JFormattedTextField ftf = (JFormattedTextField) getComponent(); if (ftf.isEditValid()) { try { ftf.commitEdit(); } catch (java.text.ParseException exc) { // nothing to do } } else { if (!askEditOrRevert(ftf, null)) { return false; } else { ftf.setValue(ftf.getValue()); } } return super.stopCellEditing(); }
/** Constructs cell editor. */ public CellEditor() { super(new JFormattedTextField()); final JFormattedTextField ftf = (JFormattedTextField) getComponent(); // Set GUI behaviour of text field ftf.setValue(null); ftf.setHorizontalAlignment(JTextField.LEADING); ftf.setFocusLostBehavior(JFormattedTextField.PERSIST); // Set that one click on cell is enough for editing setClickCountToStart(1); // Special handling code for ENTER ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check"); ftf.getActionMap() .put( "check", new AbstractAction() { public void actionPerformed(ActionEvent e) { if (!ftf.isEditValid()) { if (askEditOrRevert(ftf, null)) { ftf.setValue(ftf.getValue()); ftf.postActionEvent(); } } else try { ftf.commitEdit(); ftf.postActionEvent(); } catch (java.text.ParseException exc) { // nothing to do } } }); }
public void actionPerformed(ActionEvent e) { if (e.getSource() == submit) { if (rb1.isSelected()) { JFrame frame = Application.getFrame(); if (typeCodes.isEmpty()) { JOptionPane.showMessageDialog( frame, "Please select at least one toponym type.", "Error", JOptionPane.ERROR_MESSAGE); } else { Application.getMainCitiesPanel(this, typeCodes, 0, 0.0); } } else if (rb2.isSelected()) { JFrame frame = Application.getFrame(); try { nCities = new Integer(nCitiesField.getText()); if (nCities <= 0) throw new NumberFormatException(); typeCodes = new ArrayList<String>(); Application.getMainCitiesPanel(this, typeCodes, nCities, 0.0); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog( frame, "Please enter a positive integer number.", "Error", JOptionPane.ERROR_MESSAGE); } } else { JFrame frame = Application.getFrame(); try { Double dist = new Double(distField.getText()); if (dist <= 0) throw new NumberFormatException(); typeCodes = new ArrayList<String>(); Application.getMainCitiesPanel(this, typeCodes, 0, dist); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog( frame, "Please enter a positive number.", "Error", JOptionPane.ERROR_MESSAGE); } } } else { Application.getOptionPanel(this, countryName); } }
public void init() { // 添加按钮 JPanel buttonPanel = new JPanel(); buttonPanel.add(okButton); mainPanel.setLayout(new GridLayout(0, 3)); mainWin.add(mainPanel, BorderLayout.CENTER); JFormattedTextField intField0 = new JFormattedTextField( new InternationalFormatter(NumberFormat.getIntegerInstance()) { protected DocumentFilter getDocumentFilter() { return new NumberFilter(); } }); intField0.setValue(100); addRow("只接受数字的文本框", intField0); JFormattedTextField intField1 = new JFormattedTextField(NumberFormat.getIntegerInstance()); intField1.setValue(new Integer(100)); // 添加输入校验器 intField1.setInputVerifier(new FormattedTextFieldVerifier()); addRow("带输入校验器的文本框", intField1); // 创建自定义格式器对象 IPAddressFormatter ipFormatter = new IPAddressFormatter(); ipFormatter.setOverwriteMode(false); // 以自定义格式器对象创建格式化文本框 JFormattedTextField ipField = new JFormattedTextField(ipFormatter); ipField.setValue(new byte[] {(byte) 192, (byte) 168, 4, 1}); addRow("IP地址格式", ipField); mainWin.add(buttonPanel, BorderLayout.SOUTH); mainWin.pack(); mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainWin.setVisible(true); }
public MakeReservation() { new BorderLayout(); standardRoom.setMnemonic(KeyEvent.VK_K); standardRoom.setActionCommand("Standard Room"); standardRoom.setSelected(true); familyRoom.setMnemonic(KeyEvent.VK_F); familyRoom.setActionCommand("Family Room"); suiteRoom.setMnemonic(KeyEvent.VK_S); suiteRoom.setActionCommand("Suite"); // Add Booking Button ImageIcon bookRoomIcon = createImageIcon("images/book.png"); bookRoom = new JButton("Book Room", bookRoomIcon); bookRoom.setVerticalTextPosition(AbstractButton.BOTTOM); bookRoom.setHorizontalTextPosition(AbstractButton.CENTER); bookRoom.setMnemonic(KeyEvent.VK_M); bookRoom.addActionListener(this); bookRoom.setActionCommand("book"); // Group the radio buttons. group.add(standardRoom); group.add(familyRoom); group.add(suiteRoom); // Create the labels. nameLabel = new JLabel("Name: "); amountroomsLabel = new JLabel("How many rooms? "); checkoutdateLabel = new JLabel("Check-Out Date: "); checkindateLabel = new JLabel("Check-In Date: "); // Create the text fields and set them up. nameField = new JFormattedTextField(); nameField.setColumns(10); amountroomsField = new JFormattedTextField(new Integer(1)); amountroomsField.setValue(new Integer(1)); amountroomsField.setColumns(10); // java.util.Date dt_checkin = new java.util.Date(); LocalDate today = LocalDate.now(); // java.text.SimpleDateFormat sdf_checkin = new java.text.SimpleDateFormat("MM/dd/yyyy"); currentDate_checkin = today.toString(); checkindateField = new JFormattedTextField(currentDate_checkin); checkindateField.setColumns(10); // java.util.Date dt_checkout = new java.util.Date(); LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS); // java.text.SimpleDateFormat sdf_checkout = new java.text.SimpleDateFormat("MM/dd/yyyy"); currentDate_checkout = tomorrow.toString(); checkoutdateField = new JFormattedTextField(currentDate_checkout); checkoutdateField.setColumns(10); // Tell accessibility tools about label/textfield pairs. nameLabel.setLabelFor(nameField); amountroomsLabel.setLabelFor(amountroomsField); checkoutdateLabel.setLabelFor(checkoutdateField); checkindateLabel.setLabelFor(checkindateField); // Lay out the labels in a panel. JPanel labelPane1 = new JPanel(new GridLayout(0, 1)); labelPane1.add(amountroomsLabel); JPanel labelPane3 = new JPanel(new GridLayout(0, 1)); labelPane3.add(checkindateLabel); JPanel labelPane2 = new JPanel(new GridLayout(0, 1)); labelPane2.add(checkoutdateLabel); JPanel labelPane4 = new JPanel(new GridLayout(0, 1)); labelPane4.add(nameLabel); // Layout the text fields in a panel. JPanel fieldPane1 = new JPanel(new GridLayout(0, 1)); fieldPane1.add(amountroomsField); JPanel fieldPane3 = new JPanel(new GridLayout(0, 1)); fieldPane3.add(checkindateField); JPanel fieldPane2 = new JPanel(new GridLayout(0, 1)); fieldPane2.add(checkoutdateField); JPanel fieldPane4 = new JPanel(new GridLayout(0, 1)); fieldPane4.add(nameField); // Put the radio buttons in a column in a panel. JPanel radioPanel = new JPanel(new GridLayout(0, 1)); radioPanel.add(standardRoom); radioPanel.add(familyRoom); radioPanel.add(suiteRoom); // Put the panels in this panel, labels on left, // text fields on right. setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); add(labelPane1, BorderLayout.LINE_START); add(fieldPane1, BorderLayout.LINE_END); add(labelPane3, BorderLayout.LINE_START); add(fieldPane3, BorderLayout.LINE_END); add(labelPane2, BorderLayout.LINE_START); add(fieldPane2, BorderLayout.LINE_END); add(labelPane4, BorderLayout.LINE_START); add(fieldPane4, BorderLayout.LINE_END); add(radioPanel, BorderLayout.LINE_END); add(bookRoom); }
public void bookRoom() { try { int code = 0; Connection con = DriverManager.getConnection( "jdbc:mysql://67.20.111.85:3306/jeehtove_caliking?relaxAutoCommit=false", "jeehtove_ck", "Z_^PBBZT+kcy"); Statement stmt_insert = con.createStatement(); Statement stmt_tableRange = con.createStatement(); Statement stmt_checkrooms = con.createStatement(); Statement stmt_checkrooms_2 = con.createStatement(); String insert = null; int flag = 0; String room_type; if (standardRoom.isSelected()) { room_type = "Standard"; code = 1; } else if (familyRoom.isSelected()) { room_type = "Family"; code = 2; } else { room_type = "Suite"; code = 3; } // Converting string from checkin and checkout dates so that they can access the correct table String currentDate_checkin_getText = checkindateField.getText(); String currentDate_checkin_rest = currentDate_checkin_getText.substring(5); String currentDate_checkin_replace = currentDate_checkin_rest.replace("-", "_"); String currentDate_checkin_year = currentDate_checkin.substring(0, 4); String currentDate_checkin_final = currentDate_checkin_year + "_" + currentDate_checkin_replace; String currentDate_checkout_getText = checkoutdateField.getText(); String currentDate_checkout_rest = currentDate_checkout_getText.substring(5); String currentDate_checkout_replace = currentDate_checkout_rest.replace("-", "_"); String currentDate_checkout_year = currentDate_checkout.substring(0, 4); String currentDate_checkout_final = currentDate_checkout_year + "_" + currentDate_checkout_replace; // MySQL Statement to select tables from schema that are in between checkin and checkout range String tableRange = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='jeehtove_caliking' AND TABLE_NAME BETWEEN 'rooms_" + currentDate_checkin_final + "' AND 'rooms_" + currentDate_checkout_final + "';"; ResultSet tb_Range = stmt_tableRange.executeQuery(tableRange); System.out.println(tableRange); List tb_range_list = new ArrayList(); while (tb_Range.next()) { String tb_range_date = tb_Range.getString("table_name"); tb_range_list.add(tb_range_date); // String current_table = tb_Range.getString("table_name"); // System.out.println(tb_range_list.add(tb_Range.getString("table_name"))); } stmt_tableRange.close(); for (int x = 0; x < tb_range_list.size(); x++) { String query_checkrooms = "SELECT * FROM " + tb_range_list.get(x) + " WHERE type = '" + code + "' AND booked = '0' LIMIT 1;"; // System.out.println(query_checkrooms); ResultSet checkrooms = stmt_checkrooms.executeQuery(query_checkrooms); while (checkrooms.next()) { String getroomnumber = checkrooms.getString("room"); insert = "INSERT INTO `reservations` VALUES ('" + getroomnumber + "', '" + checkindateField.getText() + "', '" + checkoutdateField.getText() + "', '" + room_type + "', '" + nameField.getText() + "');"; String bookroom = "UPDATE `" + tb_range_list.get(x) + "` SET booked = '1' WHERE room = '" + getroomnumber + "';"; // System.out.println(insert); int cr = stmt_checkrooms_2.executeUpdate(bookroom); flag = 1; System.out.println("Room Booked!"); } } if (flag == 1) { int rs = stmt_insert.executeUpdate(insert); } else { System.out.println("All " + room_type + " rooms are booked for the time requested."); } /*String query_checkrooms = "SELECT * FROM rooms_" + currentDate_checkin_final + " WHERE type = '" + code + "' AND booked = '0';"; System.out.println(query_checkrooms); Statement stmt_checkrooms = con.createStatement(); ResultSet checkrooms = stmt_checkrooms.executeQuery(query_checkrooms);*/ /*if (checkrooms.next()){ String getroomnumber = checkrooms.getString("room"); String insert = "INSERT INTO `reservations` VALUES ('" + getroomnumber + "', '" + checkindateField.getText() + "', '" + checkoutdateField.getText() + "', '" + room_type + "', '" + nameField.getText() + "');"; String bookroom = "UPDATE `rooms_" + currentDate_checkin_final + "` SET booked = '1' WHERE room = '" + getroomnumber + "';"; //System.out.println(insert); int rs=stmt_insert.executeUpdate(insert); int cr=stmt_checkrooms.executeUpdate(bookroom); con.commit(); System.out.println("Room Booked!"); }*/ // else System.out.println("All " + room_type + " rooms are booked for the time requested."); System.out.println("Complete."); } catch (Exception e) { System.out.println(e); } }
public void actionPerformed(ActionEvent a) { int code = 0; String roomtypeCompare_arr = null; String roomtypeCompare_dep = null; if ("book".equals(a.getActionCommand())) { try { Class.forName("com.mysql.jdbc.Driver"); // Database connection information. Please note that when porting the code, you must have // the jdbc Driver installed where the code is running. Connection con = DriverManager.getConnection( "jdbc:mysql://67.20.111.85:3306/jeehtove_caliking?relaxAutoCommit=true", "jeehtove_ck", "Z_^PBBZT+kcy"); // The SELECT query String query_arr = "SELECT * from reservations WHERE checkin BETWEEN '" + checkindateField.getText() + "' AND '" + checkoutdateField.getText() + "'"; String query_dep = "SELECT * from reservations WHERE checkout BETWEEN '" + checkindateField.getText() + "' AND '" + checkoutdateField.getText() + "'"; Statement stmt_arr = con.createStatement(); Statement stmt_dep = con.createStatement(); Statement stmt_insert = con.createStatement(); ResultSet result_arr = stmt_arr.executeQuery(query_arr); ResultSet result_dep = stmt_dep.executeQuery(query_dep); // Get room type from radio buttons String room_type; if (standardRoom.isSelected()) { room_type = "Standard"; code = 1; } else if (familyRoom.isSelected()) { room_type = "Family"; code = 2; } else { room_type = "Suite"; code = 3; } bookRoom(); // ResultSet rs=stmt.executeQuery("select * from emp"); /*if (!result_arr.next() && !result_dep.next()){ bookRoom(); //Query Prep for checking for open rooms /*String query_checkrooms = "SELECT * FROM makereservation_rooms WHERE type = '" + code + "' AND booked = '0';"; Statement stmt_checkrooms = con.createStatement(); ResultSet checkrooms = stmt_checkrooms.executeQuery(query_checkrooms); if (checkrooms.next()){ String getroomnumber = checkrooms.getString("room"); String insert = "INSERT INTO `reservations` VALUES ('" + getroomnumber + "', '" + checkindateField.getText() + "', '" + checkoutdateField.getText() + "', '" + room_type + "', '" + nameField.getText() + "');"; String bookroom = "UPDATE `makereservation_rooms` SET booked = '1' WHERE room = '" + getroomnumber + "';"; //System.out.println(insert); int rs=stmt_insert.executeUpdate(insert); int cr=stmt_checkrooms.executeUpdate(bookroom); con.commit(); System.out.println("Room Booked!"); } else System.out.println("All " + room_type + " rooms are booked for the time requested."); } else { if (result_arr.next()){ roomtypeCompare_arr = result_arr.getString("type"); System.out.println("(arrival) Does " + roomtypeCompare_arr + " equal " + room_type + "?"); if (roomtypeCompare_arr.equals(room_type)){ String checkindateDB_arr = result_arr.getString("checkin"); String checkoutdateDB_arr = result_arr.getString("checkout"); System.out.println("There is a room booked on " + checkindateDB_arr + " that checks out on " + checkoutdateDB_arr + ". Please select another date."); } else { System.out.println("Room Booked!"); } } } //System.out.println(query_dep); if (result_dep.next()){ roomtypeCompare_dep = result_dep.getString("type"); System.out.println("(departure) Does " + roomtypeCompare_dep + " equal " + room_type + "?"); if (roomtypeCompare_dep.equals(room_type)){ String checkindateDB_dep = result_dep.getString("checkin"); String checkoutdateDB_dep = result_dep.getString("checkout"); System.out.println("There is a room booked on " + checkindateDB_dep + " that checks out on " + checkoutdateDB_dep + ". Please select another date."); } else { System.out.println("Room Booked!"); } }*/ con.close(); } catch (Exception e) { System.out.println(e); } } }
public FormatTestFrame() { JPanel buttonPanel = new JPanel(); okButton = new JButton("Ok"); buttonPanel.add(okButton); add(buttonPanel, BorderLayout.SOUTH); mainPanel = new JPanel(); mainPanel.setLayout(new GridLayout(0, 3)); add(mainPanel, BorderLayout.CENTER); JFormattedTextField intField = new JFormattedTextField(NumberFormat.getIntegerInstance()); intField.setValue(new Integer(100)); addRow("Number:", intField); JFormattedTextField intField2 = new JFormattedTextField(NumberFormat.getIntegerInstance()); intField2.setValue(new Integer(100)); intField2.setFocusLostBehavior(JFormattedTextField.COMMIT); addRow("Number (Commit behavior):", intField2); JFormattedTextField intField3 = new JFormattedTextField( new InternationalFormatter(NumberFormat.getIntegerInstance()) { protected DocumentFilter getDocumentFilter() { return filter; } }); intField3.setValue(new Integer(100)); addRow("Filtered Number", intField3); JFormattedTextField intField4 = new JFormattedTextField(NumberFormat.getIntegerInstance()); intField4.setValue(new Integer(100)); intField4.setInputVerifier( new InputVerifier() { public boolean verify(JComponent component) { JFormattedTextField field = (JFormattedTextField) component; return field.isEditValid(); } }); addRow("Verified Number:", intField4); JFormattedTextField currencyField = new JFormattedTextField(NumberFormat.getCurrencyInstance()); currencyField.setValue(new Double(10)); addRow("Currency:", currencyField); JFormattedTextField dateField = new JFormattedTextField(DateFormat.getDateInstance()); dateField.setValue(new Date()); addRow("Date (default):", dateField); DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT); format.setLenient(false); JFormattedTextField dateField2 = new JFormattedTextField(format); dateField2.setValue(new Date()); addRow("Date (short, not lenient):", dateField2); try { DefaultFormatter formatter = new DefaultFormatter(); formatter.setOverwriteMode(false); JFormattedTextField urlField = new JFormattedTextField(formatter); urlField.setValue(new URL("http://java.sun.com")); addRow("URL:", urlField); } catch (MalformedURLException ex) { ex.printStackTrace(); } try { MaskFormatter formatter = new MaskFormatter("###-##-####"); formatter.setPlaceholderCharacter('0'); JFormattedTextField ssnField = new JFormattedTextField(formatter); ssnField.setValue("078-05-1120"); addRow("SSN Mask:", ssnField); } catch (ParseException ex) { ex.printStackTrace(); } JFormattedTextField ipField = new JFormattedTextField(new IPAddressFormatter()); ipField.setValue(new byte[] {(byte) 130, 65, 86, 66}); addRow("IP Address:", ipField); pack(); }
@Override public Object getCellEditorValue() { JFormattedTextField ftf = (JFormattedTextField) getComponent(); return ftf.getValue(); }
// 当输入组件失去焦点时,该方法被触发 public boolean verify(JComponent component) { JFormattedTextField field = (JFormattedTextField) component; // 返回用户输入是否有效 return field.isEditValid(); }
public MainCitiesCriteriaPanel() { super(new BorderLayout()); CountryController countryc = Application.getCountryController(); CitiesController citiesc = Application.getCitiesController(); countryName = Application.getCountryName(); label = new JLabel(); labelPanel = new JPanel(); labelPanel.add(label); label.setText("Criteria to select main cities for " + countryName.replaceAll("_", " ")); listModel = new DefaultListModel(); Iterator<HashMap<String, String>> iter = citiesc.getToponymTypesIterator(); while (iter.hasNext()) { String topTypeName = iter.next().get("code"); listModel.addElement(topTypeName); } list = new JList(listModel); list.addListSelectionListener(this); listPanel = new JScrollPane(list); NumberFormat nCitiesFormat = NumberFormat.getInstance(); nCitiesFormat.setMaximumFractionDigits(0); nCitiesFormat.setMaximumIntegerDigits(4); NumberFormat distFormat = NumberFormat.getInstance(); distFormat.setMaximumFractionDigits(0); distFormat.setMaximumIntegerDigits(3); nCitiesField = new JFormattedTextField(nCitiesFormat); distField = new JFormattedTextField(distFormat); nCitiesField.setMaximumSize(new Dimension(50, 1)); distField.setMaximumSize(new Dimension(50, 1)); rb1 = new JRadioButton("Filter by type", true); rb2 = new JRadioButton("Filter by number", false); rb3 = new JRadioButton("Filter by distance to the borders (km)", false); ButtonGroup bgroup = new ButtonGroup(); bgroup.add(rb1); bgroup.add(rb2); bgroup.add(rb3); JPanel radioPanel = new JPanel(); radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS)); radioPanel.add(rb1); radioPanel.add(listPanel); radioPanel.add(rb2); radioPanel.add(nCitiesField); radioPanel.add(rb3); radioPanel.add(distField); submit = new JButton(); back = new JButton(); submit.setText("OK"); back.setText("GO BACK"); submit.addActionListener(this); back.addActionListener(this); buttonPanel = new JPanel(); buttonPanel.add(back); buttonPanel.add(submit); add(labelPanel, BorderLayout.NORTH); add(radioPanel, BorderLayout.CENTER); add(buttonPanel, BorderLayout.SOUTH); }