Re: mehrfach unterstreichen möglich?



Mein Beispiel etwas erweitert:

import javax.swing.*;
import javax.swing.text.Caret;
import javax.swing.text.BadLocationException;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.CaretEvent;
import javax.swing.border.Border;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * FlowDOMView
 *
 * @author FRMEYER
 * @version 25.10.2005 18:09:31
 */
public class FlowDOMView extends JPanel {
  private DefaultTreeModel model = null;
  private FieldListener fieldListener = null;
  private HashMap borderMap = null;

  public FlowDOMView(DefaultTreeModel model) {
    this.model = model;
    init();
  }

  private void init() {
    setLayout(new LineLayout());
    fieldListener = new FieldListener(this);
    borderMap = new HashMap();
    fill((DefaultMutableTreeNode)model.getRoot(), 0);
  }

  private void fill(DefaultMutableTreeNode node, int depth) {
    int count = node.getChildCount();
    if (count==0) {
      JTextField field = new JTextField();
      field.setText(node.getUserObject().toString());
      field.getDocument().addDocumentListener(fieldListener);
      field.addCaretListener(fieldListener);

UnderLineBorder border = (UnderLineBorder) borderMap.get(new Integer(depth));
if (border==null) {
border = new UnderLineBorder(null, 1, 2);
borderMap.put(new Integer(depth), border);
for (int i=0; i<depth; i++) {
int c = 255*i/depth;
border.addColor(new Color(c,c,c));
}
}
field.setBorder(border);
add(field);
} else {
for (int i=0; i<count; i++) {
DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(i);
fill(child, depth+1);
}
}
}


public static void main(String[] args) {
DefaultMutableTreeNode nRoot = new DefaultMutableTreeNode("root");
String[] sample = new String[] {"para", "line", "text", "asdfasdf", "232", "difkflsdl", "assd324"};
for (int i=0; i<50; i++) {
DefaultMutableTreeNode n0 = new DefaultMutableTreeNode("aadf");
nRoot.add(n0);
int depth = (int)(Math.random()*(double)sample.length)+1;
for (int d=0; d<depth; d++) {
DefaultMutableTreeNode n1 = new DefaultMutableTreeNode(sample[d]);
n0.add(n1);
n0 = n1;
}
}
DefaultTreeModel model = new DefaultTreeModel(nRoot);


    FlowDOMView view = new FlowDOMView(model);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new JScrollPane(view));

    frame.setSize(220,200);
    frame.setVisible(true);
  }

private static class FieldListener implements DocumentListener, CaretListener {
FlowDOMView view = null;


    public FieldListener(FlowDOMView view) {
      this.view = view;
    }

    private void updateLayout() {
      view.doLayout();
      Container parent = view.getParent();
      if (parent instanceof JViewport) {
        parent.doLayout();
      }
    }

    public void caretUpdate(CaretEvent e) {
      JTextField field = (JTextField) e.getSource();
      int pos = field.getCaretPosition();
      try {
        Rectangle rect = field.getUI().modelToView(field, pos);
        if (rect==null) {
          rect = new Rectangle();
        }
        rect.x += field.getX();
        rect.y += field.getY();
        rect.width += 10;
        view.scrollRectToVisible(rect);
      } catch (Exception e1) {
        e1.printStackTrace();
      }
    }

    public void changedUpdate(DocumentEvent e) {
    }

    public void insertUpdate(DocumentEvent e) {
      updateLayout();
    }

    public void removeUpdate(DocumentEvent e) {
      updateLayout();
    }
  }

  private static class UnderLineBorder implements Border {
    private ArrayList colors = new ArrayList();
    private int thickness = 1;
    private int vGap = 0;

    public UnderLineBorder() {
    }

    public UnderLineBorder(Color[] cs, int thickness, int vgap) {
      setColors(cs);
      this.thickness = thickness;
      this.vGap = vgap;
    }

    public int getThickness() {
      return thickness;
    }

    public void setThickness(int v) {
      thickness = v;
    }

    public int getVGap() {
      return vGap;
    }

    public void setVGap(int v) {
      vGap = v;
    }

    public Color[] getColors() {
      return (Color[]) colors.toArray(new Color[colors.size()]);
    }

    public void setColors(Color[] cs) {
      colors.clear();
      for (int i = 0; cs!=null && i < cs.length; i++) {
        Color c = cs[i];
        colors.add(c);
      }
    }

    public void addColor(Color c) {
      colors.add(c);
    }

    public void removeColor(Color c) {
      colors.remove(c);
    }

    public void clearColors() {
      colors.clear();
    }

    public boolean isBorderOpaque() {
      return false;
    }

public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Color[] colors = getColors();
Insets insets = getBorderInsets(c);
int base = y + height - 1 - insets.bottom;
for (int i = 0; i < colors.length; i++) {
Color color = colors[i];
g.setColor(color);
for (int t = 0; t < thickness; t++) {
g.drawLine(x, base, x+width, base);
base++;
}
base += vGap;
}
g.setColor(Color.red);
g.drawLine(x, y+height, x+width, y+height);
}


    public Insets getBorderInsets(Component c) {
      Insets insets = new Insets(0,0,0,0);

      int size = colors.size();
      for (int i=0; i<size; i++) {
        insets.bottom += thickness;
        if (i>0) {
          insets.bottom += vGap;
        }
      }

      return insets;
    }
  }

  private static class LineLayout implements LayoutManager {

    private int iAlignmentH = SwingConstants.LEFT;

    private int hGap = 0;
    private int vGap = 0;

    public LineLayout() {
      this(5,5);
    }

    public LineLayout(int hGap, int vGap) {
      this.hGap = hGap;
      this.vGap = vGap;
    }

    public LineLayout(int hGap, int vGap, int iAlignmentH) {
      this.hGap = hGap;
      this.vGap = vGap;
      this.iAlignmentH = iAlignmentH;
    }

    public int getAlignmentH() {
      return iAlignmentH;
    }

    public void setAlignmentH(int AlignmentH) {
      this.iAlignmentH = AlignmentH;
    }

    public int getHGap() {
      return hGap;
    }

    public void setHGap(int hGap) {
      this.hGap = hGap;
    }

    public int getVGap() {
      return vGap;
    }

    public void setVGap(int vGap) {
      this.vGap = vGap;
    }

    /**
     * wird hier nicht gebraucht
     * @param name the component name
     * @param comp the component to be added
     */
    public void addLayoutComponent(String name, Component comp) {
    }

    /**
     * wird hier nicht gebraucht
     * @param comp the component to be removed
     */
    public void removeLayoutComponent(Component comp) {
    }

    /**
     * Calculates the preferred size dimensions for the specified
     * panel given the components in the specified parent container.
     * @param parent the component to be laid out
     *
     * @see #minimumLayoutSize
     */
    public Dimension preferredLayoutSize(Container parent) {
      return layoutImpl(parent, false, hGap, vGap, iAlignmentH);
    }

    /**
     * Calculates the minimum size dimensions for the specified
     * panel given the components in the specified parent container.
     * @param parent the component to be laid out
     * @see #preferredLayoutSize
     */
    public Dimension minimumLayoutSize(Container parent) {
      return layoutImpl(parent, false, hGap, vGap, iAlignmentH);
    }

    /**
     * Lays out the container in the specified panel.
     * @param parent the component which needs to be laid out
     */
    public void layoutContainer(Container parent) {
      layoutImpl(parent, true, hGap, vGap, iAlignmentH);

    }

/**
* delegate-methode
* @param cont
* @param placeComps
* @return
*/
private static Dimension layoutImpl(Container cont, boolean placeComps, int hGap, int vGap, int iAlignmentH) {
synchronized (cont.getTreeLock()) {
Dimension dim = new Dimension();
if (cont==null) {
return dim;
}
Component[] comps = cont.getComponents();
if (comps==null || comps.length==0) {
return dim;
}


        Dimension dimMax = findVisibleDimension(cont);
        if (dimMax==null) {
          dimMax = new Dimension(Integer.MAX_VALUE,Integer.MAX_VALUE);
        }

        Insets insets = cont.getInsets();
        int inLeft = insets.left;
        int inRight = insets.right;
        int inTop = insets.top;
        int inBottom = insets.bottom;

        int xLeft = inLeft + hGap;
        int xRight = dimMax.width - inRight - hGap;
        if (xRight<0) {
          xRight = 0;
        }
        if (xRight<xLeft) {
          xRight = xLeft;
        }

        int y = inTop + vGap;
        int x = xLeft;

        int rowH = 0;
        int maxX = 0;
        int maxY = 0;

        ArrayList vLineComps = new ArrayList();
        for (int i = 0; i < comps.length; i++) {

          Component comp = comps[i];
          Dimension dimComp = comp.getPreferredSize();
          if (dimComp==null) {
            dimComp = comp.getMinimumSize();
          }
          if (dimComp==null) {
            dimComp = new Dimension();
          }
          int compW = dimComp.width;
          int compH = dimComp.height;

          int nextRight = x+compW;
          if (dimComp.height>rowH) {
            rowH = dimComp.height;
          }

          boolean newLine = false;
          if (nextRight>xRight) {
            newLine = true;
          } else if (compW>=xRight-xLeft) {
            newLine = true;
          }

          if (i!=0 && newLine) {
            if (placeComps) {
              placeLine(vLineComps,dimMax,iAlignmentH);
            }
            vLineComps.clear();
            x = xLeft;
            y += rowH;
            y += vGap;
            if (placeComps) {
              comp.setBounds(x,y,compW,compH);
            }
            rowH = 0;
          } else {
            if (placeComps) {
              comp.setBounds(x,y,compW,compH);
            }
          }

          vLineComps.add(comp);

          x += compW;
          x += hGap;

          if (x>maxX) {
            maxX = x;
          }
          if (y>maxY) {
            maxY = y;
          }
        }

        if (placeComps) {
          placeLine(vLineComps,dimMax,iAlignmentH);
        }

        int compX0 = 0;
        int compX1 = 0;
        int compY0 = 0;
        int compY1 = 0;
        for (int i = 0; i < comps.length; i++) {
          Component comp = comps[i];
          Rectangle rect = comp.getBounds();
          if (i==0 || rect.x<compX0) {
            compX0 = rect.x;
          }
          if (i==0 || rect.x+rect.width>compX1) {
            compX1 = rect.x+rect.width;
          }
          if (i==0 || rect.y<compY0) {
            compY0 = rect.y;
          }
          if (i==0 || rect.y+rect.height>compY1) {
            compY1 = rect.y+rect.height;
          }
        }

        dim.width = compX1 + inRight;
        dim.height = compY1 + inBottom;

        return dim;
      }
    }

private static void placeLine(ArrayList vLineComps, Dimension dimMax, int iAlignmentH) {
if (iAlignmentH!=SwingUtilities.RIGHT && iAlignmentH!=SwingUtilities.CENTER) {
return;
}
int count = vLineComps.size();
if (count==0) {
return;
}
Component[] comps = new Component[count];
System.arraycopy(vLineComps.toArray(),0,comps,0,count);
int widthMax = dimMax.width;
Component compLeft = comps[0];
Component compRight = comps[count-1];
int x0 = compLeft.getBounds().x;
int x1 = compRight.getBounds().x + compRight.getBounds().width;
int w = x1-x0;
int addX = 0;
if (iAlignmentH==SwingUtilities.RIGHT) {
addX = widthMax-w;
} else {
// CENTER
addX = widthMax-w;
addX /= 2;
}


      for (int i = 0; i < comps.length; i++) {
        Component comp = comps[i];
        Rectangle rect = comp.getBounds();
        rect.x += addX;
        comp.setBounds(rect);
      }

    }

    private static Dimension findVisibleDimension(Container cont) {
      while (true) {
        if (!(cont instanceof JComponent)) {
          return null;
        }
        JComponent comp = (JComponent)cont;
        Dimension size = comp.getVisibleRect().getSize();
        if (size.width>0) {
          return size;
        }
        cont = cont.getParent();
        if (cont==null) {
          return null;
        }
      }
    }
  }
}
.



Relevant Pages

  • Re: Sortable Java Tree Table
    ... I have never actually done this, but if I wanted to sort a JTree, ... private Listener listener = new Listener; ... public Object getChild(Object parent, int index) { ... public void valueForPathChanged{ ...
    (comp.lang.java.gui)
  • poker odds
    ... public static final int HIGH_CARD = 0; ... private static Logger logger; ... public Poker(int numCard, Card deck) ... public void maxSimulation ...
    (comp.lang.java.programmer)
  • Re: NotSerializable object issue...
    ... private static final NoopOutputStream DUMMY_OUTPUT_STREAM = new ... public SerializableCheckerthrows IOException { ... public void writeObjectthrows IOException { ... private int counter; ...
    (comp.lang.java.programmer)
  • Re: Im needing help with compile errors for GUI enviroment GameOfWar
    ... public static int[] randomize ... private Rank ... public void paintComponent ... class WinningsPanel extends JPanel implements ItemListener ...
    (comp.lang.java.gui)
  • Re: Im needing help with compile errors GameOfWar new compile errors under 2judith
    ... public static int[] randomize ... private Rank ... public void paintComponent ... class WinningsPanel extends JPanel implements ItemListener ...
    (comp.lang.java.gui)

Loading