I have two text area on the screen, I can make bold the different words between two text areas and I want to have for up and down to go the different words helping this button on the text areas. how can I do that?

Dani AI

Generated

Thread summary and a practical solution that fills gaps in the replies: confirmed the goal is a pair of controls (buttons and/or keyboard arrows) that step the caret/selection to the next/previous word already marked as different between the two text areas. was right to suggest listeners; asked the needed clarification.

Recommended approach (concise):

  • Keep an ordered list of difference ranges (start,end offsets) for each area. Maintain a current index into that list.
  • On Up/Down (buttons or keys) decrement/increment the index, clamp or wrap as desired, then select that range in the target text component and scroll it into view.
  • If styled marking (bold) is required, use JTextPane + StyledDocument. JTextArea is plain text and cannot hold mixed styles.
  • Prefer Swing Key Bindings over KeyListener for arrow-key navigation (more reliable inside Swing focus system). See the Oracle Key Bindings guide for details.

Minimal navigation example (illustrative):

List<int[]> diffs = ...; // each int[] = {start, end}
int current = -1;

void show(int idx, JTextComponent tc) {
  if (idx < 0 || idx >= diffs.size()) return;
  int[] r = diffs.get(idx);
  tc.requestFocusInWindow();
  tc.select(r[0], r[1]);
  try {
    Rectangle rect = tc.modelToView(r[0]);
    if (rect != null) tc.scrollRectToVisible(rect);
  } catch (BadLocationException ex) { /* ignore */ }
}

Action prev = new AbstractAction(){ public void actionPerformed(ActionEvent e){
  if (current > 0) show(--current, target); }};

Action next = new AbstractAction(){ public void actionPerformed(ActionEvent e){
  if (current < diffs.size()-1) show(++current, target); }};

target.getInputMap().put(KeyStroke.getKeyStroke("UP"), "prev");
target.getActionMap().put("prev", prev);
target.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "next");
target.getActionMap().put("next", next);

upButton.addActionListener(prev);
downButton.addActionListener(next);

Practical tips and cautions:

  • If the diff list changes after edits, recompute it and adjust current so it remains valid. Disable or gray navigation controls when no diffs exist.
  • For visible styled marks use StyledDocument.setCharacterAttributes (SimpleAttributeSet + StyleConstants.setBold). For scrolling use modelToView (or modelToView2D) inside EDT.
  • Reference: How to Use Key Bindings and How to Use Text Components.

Recommended Answers

All 6 Replies

what do you mean by:

and I want to have a button up and down to go the different words helping this button on the text areas.

Are you asking how to have one button that when pressed will select the next word towards the top of the list of words and another button that will select the next word towards the bottom of the list?

Are you asking how to have one button that when pressed will select the next word towards the top of the list of words and another button that will select the next word towards the bottom of the list?

yes you are right?

You will need to add action listeners to the buttons. The listener code will need the logic to move the selection to the next word in the list as determined by which button was pressed.

Can I use keyEvent for this when I use up or down key, its gonna show the next different word?

Are you using JButtons on your GUI to control the action
or are you going to use the keyboard up & down arrows?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.