/** 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 } } }); }
/** * Affiche les cases de la grille grâce à des JTextField pour les cases à remplir et des JLabel * pour les indices de somme dans le JPanel pan * * @param pan * @throws NumberFormatException */ public void affichCase(JPanel pan) throws NumberFormatException { if (this.n != 0) { bloc.setValue(null); try { MaskFormatter format = new MaskFormatter("#"); bloc = new JFormattedTextField(format); } catch (ParseException e2) { } pan.add(bloc); bloc.setHorizontalAlignment(SwingConstants.CENTER); bloc.addKeyListener( new KeyAdapter() { public void keyReleased(KeyEvent e) { try { if (Integer.valueOf(bloc.getText()) == n) { Utilitaire.score -= juste; juste = 1; Utilitaire.score += juste; } else { Utilitaire.score -= juste; juste = 0; Utilitaire.score += juste; } } catch (NumberFormatException e1) { } } }); } else { Utilitaire.score -= juste; juste = 1; Utilitaire.score += juste; if (sX != 0) if (sY != 0) block.setText(String.format("%2d\\%2d", sX, sY)); else block.setText(String.format("%2d\\ ", sX)); else if (sY != 0) block.setText(String.format(" \\%2d", sY)); block.setHorizontalAlignment(SwingConstants.CENTER); block.setBorder(null); block.setOpaque(true); block.setBackground(Color.BLACK); block.setForeground(Color.WHITE); pan.add(block); } }
/** * Constructor con dos parámetros para definir los límites que queremos definirle al número a * ingresar. * * @param min (Float) Permite un NULL como valor lo que no definirá un valor para el límite * inferior. * @param max (Float) Permite un NULL como valor lo que no definirá un valor para el límite * superior. */ public FloatEditor(Float min, Float max) { super(new JFormattedTextField(new DecimalFormat("####.##"))); ftf = (JFormattedTextField) getComponent(); this.minimum = min; this.maximum = max; // Set up the editor for the float cells. floatFormat = new DecimalFormat("####.##"); NumberFormatter floatFormatter = new NumberFormatter(floatFormat); floatFormatter.setFormat(floatFormat); floatFormatter.setMinimum(minimum); floatFormatter.setMaximum(maximum); ftf.setFormatterFactory(new DefaultFormatterFactory(floatFormatter)); ftf.setValue(minimum); ftf.setHorizontalAlignment(JTextField.TRAILING); ftf.setFocusLostBehavior(JFormattedTextField.PERSIST); // React when the user presses Enter while the editor is // active. (Tab is handled as specified by // JFormattedTextField's focusLostBehavior property.) ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check"); ftf.getActionMap() .put( "check", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { if (!ftf.isEditValid()) { // The text is invalid. if (userSaysRevert()) { // reverted ftf.postActionEvent(); // inform the editor } } else try { // The text is valid, ftf.commitEdit(); // so use it. ftf.postActionEvent(); // stop editing } catch (java.text.ParseException exc) { } } }); }
private void createZoomTextField() { mZoomTextField = new JFormattedTextField(new DecimalFormat("##0.00%")); mZoomTextField.setFocusLostBehavior(JFormattedTextField.REVERT); mZoomTextField.setHorizontalAlignment(SwingConstants.CENTER); mZoomTextField.setColumns(6); mZoomTextField.setMinimumSize(mZoomTextField.getPreferredSize()); mZoomTextField.addActionListener( (x) -> mImagePresentationModel.setZoom(((Number) mZoomTextField.getValue()).doubleValue())); mImagePresentationModel.addListener( new ImagePresentationModel.Listener() { @Override public void onVisibleImageContentUpdate() { mZoomTextField.setValue(mImagePresentationModel.getZoom()); } @Override public void onImageChange() { onVisibleImageContentUpdate(); } }); }
public GUI() { // Frame frame = new JFrame("HardwareSwap Notifier"); // Panels panel = new JPanel(); group1 = new JPanel(); group2 = new JPanel(); group3 = new JPanel(); group4 = new JPanel(); group5 = new JPanel(); group6 = new JPanel(); group7 = new JPanel(); group8 = new JPanel(); // Menu Bar menus = new JMenuBar(); fileMenu = new JMenu("File"); clearCurrent = new JMenuItem("Clear"); quitItem = new JMenuItem("Quit"); load = new JMenuItem("Load"); saveCurrent = new JMenuItem("Save All"); clearSaved = new JMenuItem("Clear Saved"); removeItem = new JMenuItem("Remove Item"); removePhone = new JMenuItem("Remove Phone"); saveCurrent = new JMenuItem("Save Current"); helpMenu = new JMenu("Help"); help = new JMenuItem("How To Use"); about = new JMenuItem("About"); // Buttons add1 = new JButton("Add"); add2 = new JButton("Add"); start = new JButton("Start"); stop = new JButton("Stop"); save1 = new JButton("Add/Save"); save2 = new JButton("Add/Save"); show = new JButton("Display Data"); add1.setFocusPainted(false); add2.setFocusPainted(false); start.setFocusPainted(false); stop.setFocusPainted(false); save1.setFocusPainted(false); save2.setFocusPainted(false); show.setFocusPainted(false); stop.setEnabled(false); // CheckBox remove = new JCheckBox("Remove items when found"); remove.setFocusable(false); // Listener ButtonListener listener = new ButtonListener(); add1.addActionListener(listener); add2.addActionListener(listener); start.addActionListener(listener); stop.addActionListener(listener); load.addActionListener(listener); save1.addActionListener(listener); save2.addActionListener(listener); saveCurrent.addActionListener(listener); show.addActionListener(listener); quitItem.addActionListener(listener); clearCurrent.addActionListener(listener); clearSaved.addActionListener(listener); saveCurrent.addActionListener(listener); help.addActionListener(listener); about.addActionListener(listener); removePhone.addActionListener(listener); removeItem.addActionListener(listener); remove.addActionListener(listener); // Carrier Selection options = new String[10]; options[0] = "AT&T"; options[1] = "Boost Mobile"; options[2] = "Cellular One"; options[3] = "Nextel"; options[4] = "T-Mobile"; options[5] = "Tracfone"; options[6] = "US Cellular"; options[7] = "Sprint"; options[8] = "Verizon"; options[9] = "Virgin Mobile"; carriers = new JComboBox<String>(options); // Text Fields searchName = new JTextField(15); item = new JTextField(15); phone = new JTextField(15); interval2 = new JTextField(15); results = new JTextArea(10, 20); JScrollPane scrollPane = new JScrollPane(results); results.setEditable(false); // Interval intOptions = new SpinnerNumberModel(5, 1, 60, 1); interval = new JSpinner(intOptions); JFormattedTextField tf = ((JSpinner.DefaultEditor) interval.getEditor()).getTextField(); tf.setHorizontalAlignment(JFormattedTextField.LEFT); // Background panelBackground = new Color(237, 237, 237); panel.setBackground(panelBackground); searchName.setBackground(panelBackground); item.setBackground(panelBackground); phone.setBackground(panelBackground); interval.setBackground(panelBackground); // Panel Layouts panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS)); group1.setLayout(new BoxLayout(group1, BoxLayout.PAGE_AXIS)); group2.setLayout(new BoxLayout(group2, BoxLayout.X_AXIS)); group3.setLayout(new BoxLayout(group3, BoxLayout.PAGE_AXIS)); group4.setLayout(new BoxLayout(group4, BoxLayout.X_AXIS)); group5.setLayout(new BoxLayout(group5, BoxLayout.X_AXIS)); group6.setLayout(new BoxLayout(group6, BoxLayout.X_AXIS)); group7.setLayout(new BoxLayout(group7, BoxLayout.X_AXIS)); group8.setLayout(new BoxLayout(group8, BoxLayout.X_AXIS)); // Borders searchName.setBorder( BorderFactory.createTitledBorder( null, "Search Name", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_JUSTIFICATION, null, Color.DARK_GRAY)); item.setBorder( BorderFactory.createTitledBorder( null, "Item", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_JUSTIFICATION, null, Color.DARK_GRAY)); phone.setBorder( BorderFactory.createTitledBorder( null, "Cell Phone", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_JUSTIFICATION, null, Color.DARK_GRAY)); group5.setBorder( BorderFactory.createTitledBorder( null, "Check Interval (mins)", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_JUSTIFICATION, null, Color.DARK_GRAY)); // Sizes panel.setPreferredSize(new Dimension(200, 0)); searchName.setMaximumSize(new Dimension(190, 50)); item.setMaximumSize(new Dimension(190, 50)); phone.setMaximumSize(new Dimension(185, 50)); carriers.setMaximumSize(new Dimension(175, 20)); group5.setPreferredSize(new Dimension(190, 47)); group5.setMaximumSize(new Dimension(190, 47)); add1.setMaximumSize(new Dimension(90, 20)); save1.setMaximumSize(new Dimension(90, 20)); add2.setMaximumSize(new Dimension(90, 20)); save2.setMaximumSize(new Dimension(90, 20)); start.setMaximumSize(new Dimension(90, 20)); stop.setMaximumSize(new Dimension(90, 20)); show.setMaximumSize(new Dimension(120, 20)); // Add file menu items fileMenu.add(clearCurrent); fileMenu.add(clearSaved); fileMenu.add(load); fileMenu.add(removeItem); fileMenu.add(removePhone); fileMenu.add(saveCurrent); fileMenu.add(quitItem); // Add help menu items helpMenu.add(help); helpMenu.add(about); // Add to menu bar menus.add(fileMenu); menus.add(helpMenu); // Add items to panel group1.add(searchName); group1.add(item); group2.add(add1); group2.add(Box.createHorizontalStrut(10)); group2.add(save1); group6.add(remove); group3.add(phone); group3.add(Box.createVerticalStrut(10)); group3.add(carriers); group4.add(add2); group4.add(Box.createHorizontalStrut(10)); group4.add(save2); group5.add(interval); group7.add(show); group8.add(start); group8.add(Box.createHorizontalStrut(10)); group8.add(stop); panel.add(Box.createVerticalStrut(10)); panel.add(group1); panel.add(Box.createVerticalStrut(10)); panel.add(group2); panel.add(Box.createVerticalStrut(40)); panel.add(group3); panel.add(Box.createVerticalStrut(10)); panel.add(group4); panel.add(Box.createVerticalStrut(40)); panel.add(group5); panel.add(Box.createVerticalStrut(30)); panel.add(group6); panel.add(Box.createVerticalStrut(40)); panel.add(group7); panel.add(Box.createVerticalStrut(10)); panel.add(group8); panel.add(Box.createVerticalStrut(10)); // Setup frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setJMenuBar(menus); frame.add(scrollPane); frame.add(BorderLayout.EAST, panel); frame.pack(); frame.setSize(new Dimension(670, 620)); frame.setVisible(true); }