Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could place a Listener on each of the text fields and change the other text fields according to which one was changed by the user.</p> <p>I'm afraid I'm not entirely sure which listener you have to add to receive an event AFTER the text field gets changed (InputMethodListener didn't seem to fire anything and KeyListener fires before the text changes) so in this example I'll use ActionListener, which means the user will have to press "enter" after typing in the number.</p> <pre><code>final JTextField hex = new JTextField("hex"); final JTextField dec = new JTextField("dec"); ActionListener act = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(e.getSource() == hex) { dec.setText(Integer.toString(Integer.parseInt(hex.getText(), 16))); } else if(e.getSource() == dec) { hex.setText(Integer.toHexString(Integer.parseInt(dec.getText()))); } } }; hex.addActionListener(act); dec.addActionListener(act); </code></pre> <p>EDIT: This means you won't need the "Convert!" button though, if you <strong>really</strong> want that then you'll need to keep track of which text field was edited the last time. Which you could do by doing:</p> <pre><code>JTextField lastEdited; private void init() { //other init stuff final JTextField hex = new JTextField("hex"); final JTextField dec = new JTextField("dec"); KeyListener keyListener = new KeyAdapter() { @Override public void keyTyped(KeyEvent e) { lastEdited = (JTextField) e.getSource(); } }; hex.addKeyListener(keyListener); dec.addKeyListener(keyListener); JButton convert = new JButton("Convert!"); convert.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(lastEdited == hex) { dec.setText(Integer.toString(Integer.parseInt(hex.getText(), 16))); } else if(lastEdited == dec) { hex.setText(Integer.toHexString(Integer.parseInt(dec.getText()))); } } }); //other init stuff } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload