JTree in JScrollPane incorrect scrolling

Hi,
I have a JTree in a JScrollPane, when I scroll (programatically) in the JTree using the setAnchorSelectionPath method of the JTree some scrolling occurs but not enough to bring the leaf node of the JTree into the visible viewport.
Any suggestions,
Mark

Cheers for the pointer, I am not attempting drag and drop though ....
I have a selected leaf node in the tree and then I change the type of sort I use on the data in the tree, I find the new leaf node, expand the path down to it and then attempt to scroll.
Thus no pointer information to use with <i>Autoscroll</i>
Mark

Similar Messages

  • JTree and JScrollPane problems

    Hello all.
    bear with me a second, so I can explain what is happenning with my code. I have got a java application that is accessing an SQL database (either Oracle or postgreSQL) and inputing the data into a tree.
    More specifically it takes the "enrollment" data and draws into the scrollpane the courses and students enrolled. Everything seems to be working perfectly with the building of the tree.
    but I have a drop down list that can select which years to display (1-4th year). I want the tree in the scrollPane to update when u change the year in the drop down list.
    I have added an ActionListener to the drop down list and it picks up the right selection, and I create a new tree object with the new year details and nothing is changed in the scrollpane.
    This is what the GUI looks like:
    This is my tree class which creates teh tree and adds it to the scrollPane object:
    package Interface;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    import java.sql.*;
    import Database.Database;
    public class Tree{
         JTree tree;
         JScrollPane scrollPane;
         public Tree(GridBagConstraints c, Container contentPane, GridBagLayout gridBag, int year){
              //Set up the default table.
              TreeNode root = buildTree("Enrollments", year);
              tree = new JTree(root);
              //Set up a scrollPane for the tree.
              scrollPane = new JScrollPane(tree);
              //Set constraints for the ScrollPane
              c.fill = GridBagConstraints.BOTH;
              c.weightx = 1;
              c.weighty = 1;
              c.gridwidth = 2;
              c.gridx = 0;
              c.gridy = 1;
              c.anchor = GridBagConstraints.CENTER;
              gridBag.setConstraints(scrollPane, c);
              contentPane.add(scrollPane);     
              //Set defaults for teh Tree object.
              tree.expandRow(0);
              tree.setRootVisible(false);
              tree.setEditable(true);     
         public DefaultMutableTreeNode buildTree(String s, int year){
              //Set root of tree.
              DefaultMutableTreeNode node = new DefaultMutableTreeNode(s);
              //Create a database object.
              Database db = new Database();
              //change this bit to get the name's of the courses and add into array.
              ResultSet className = db.query("SELECT name FROM courses WHERE year = "+year);
              DefaultMutableTreeNode course = null;
              DefaultMutableTreeNode name = null;
              try {
                        while(className.next()){
                             String classStr = className.getString("name");
                             course = new DefaultMutableTreeNode(classStr);
                             node.add(course);
                             ResultSet firstName = db.query("SELECT students.name FROM students, enrollments, courses WHERE students.id = enrollments.studentID AND enrollments.courseid = courses.id AND courses.name = '"+classStr+"'");
                             while(firstName.next()){
                                  String nameStr = firstName.getString("name");
                                  name = new DefaultMutableTreeNode(nameStr);
                                  course.add(name);
                   catch(Exception e){
                        System.err.println("" + e.getMessage());
                        e.printStackTrace(System.err);
              return node;
    }This is the GUI class that calls on the tree class and draws the GUI. and the actionlistener for the drop down list.
    package Interface;
         import java.awt.*;
         import javax.swing.*;
         import java.awt.event.*;
    import java.sql.*;
         public class GUI extends JFrame implements ActionListener{
              private static final long serialVersionUID = 1L;     
              DropDownMenu yearDrop;
              ScrollPane scrollPane;
              JMenuItem menuItemSave;
              JMenuItem menuItemQuit;
              JMenuBar menuBar;
              JMenu menu;
              JButton save;
              JButton quit;
              Tree tree;
              String year[] = { "First Year", "Second Year", "Third Year", "Fourth Year" };
              Container contentPane = getContentPane();
              GridBagLayout gridBag = new GridBagLayout();
              GridBagConstraints c = new GridBagConstraints();
              public GUI(){
                   setSize(500,500);
                   setTitle("UTAS Enrollments");
                   contentPane.setLayout(gridBag);          
                   save = new JButton("Save");
                   quit = new JButton("Quit");
                   menuItemSave = new JMenuItem("Save");
                   menuItemQuit = new JMenuItem("Quit");
                   menuItemSave.setMnemonic('s');
                   menuItemQuit.setMnemonic('q');
                   yearDrop = new DropDownMenu(year, c, contentPane, gridBag);
                   tree = new Tree(c, contentPane, gridBag, 1);
                   menuBar = new JMenuBar();
                   menu = new JMenu("File");
                   menu.setMnemonic('f');
                   menuBar.add(menu);
                   menu.add(menuItemSave);
                   menu.add(menuItemQuit);
                   setJMenuBar(menuBar);
                   //Set constraints for the Save button
                   c.fill = GridBagConstraints.NONE;
                   c.weightx = 1;
                   c.weighty = 0;
                   c.gridwidth = 1;
                   c.gridx = 0;
                   c.gridy = 2;
                   c.anchor = GridBagConstraints.WEST;
                   save.setMnemonic('s');
                   gridBag.setConstraints(save, c);
                   contentPane.add(save);
                   //Set constraints for the Quit button
                   c.fill = GridBagConstraints.NONE;
                   c.weightx = 1;
                   c.weighty = 0;
                   c.gridwidth = 1;
                   c.gridx = 1;
                   c.gridy = 2;
                   c.anchor = GridBagConstraints.EAST;
                   quit.setMnemonic('q');
                   gridBag.setConstraints(quit, c);
                   contentPane.add(quit);
                   //Add ActionListener to the interactive buttons on the GUI.
                   //QUIT MENU
                   menuItemQuit.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                             quitAction();
                   //SAVE MENU
                   menuItemSave.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e){
                             saveAction();
                   //QUIT BUTTON
                   quit.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e){
                             quitAction();
                   //SAVE BUTTON
                   save.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e){
                             saveAction();
                   //DROPDOWN LIST
                   yearDrop.dropDown.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e){
                             listAction();
              public void listAction(){
                   System.out.println("getSelected: "+ year[yearDrop.getSelected()]);
                   tree = new Tree(c, contentPane, gridBag, (yearDrop.getSelected()+1));
                   repaint();
         I have taken out the quitAction and saveAction methods to save space.
    I would have thought creating a new Tree object over the original tree would have updated the scrollPane.
    Any ideas would be awesome.
    Thanks in advance.

    and I create a new tree object with the new year details and nothing is changed in the scrollpane.Creating a new Tree doesn't add the tree to the scroll pane. It just changes the reference of the tree variable to point to the new Tree object. To update the scroll pane you would need to do:
    scrollPane.setViewportView( tree );
    Or another option is to just create a new TreeModel and then change the model that your existing tree is using:
    TreeModel model = new TreeModel(...);
    tree.setModel( model );

  • Problem in refreshing JTree inside Jscrollpane on Button click

    hi sir,
    i have problem in refreshing JTree on Button click inside JscrollPane
    Actually I am removing scrollPane from panel and then again creating tree inside scrollpane and adding it to Jpanel but the tree is not shown inside scrollpane. here is the dummy code.
    please help me.
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.tree.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.WindowListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    public class Test
    public static void main(String[] args)
         JFrame jf=new TestTreeRefresh();
         jf.addWindowListener( new
         WindowAdapter()
         public void windowClosing(WindowEvent e )
         System.exit(0);
         jf.setVisible(true);
    class TestTreeRefresh extends JFrame
         DefaultMutableTreeNode top;
    JTree tree;
         JScrollPane treeView;
         JPanel jp=new JPanel();
         JButton jb= new JButton("Refresh");
    TestTreeRefresh()
    setTitle("TestTree");
    setSize(500,500);
    getContentPane().setLayout(null);
    jp.setBounds(new Rectangle(1,1,490,490));
    jp.setLayout(null);
    top =new DefaultMutableTreeNode("The Java Series");
    createNodes(top);
    tree = new JTree(top);
    treeView = new JScrollPane(tree);
    treeView.setBounds(new Rectangle(50,50,200,200));
    jb.setBounds(new Rectangle(50,300,100,50));
    jb.addActionListener(new ActionListener()
    public void actionPerformed(ActionEvent ae)
              jp.remove(treeView);
              top =new DefaultMutableTreeNode("The Java ");
    createNodes(top);
              tree = new JTree(top);
                   treeView = new JScrollPane(tree);
                   treeView.setBounds(new Rectangle(50,50,200,200));
                   jp.add(treeView);     
                   jp.repaint();     
    jp.add(jb);     
    jp.add(treeView);
    getContentPane().add(jp);
    private void createNodes(DefaultMutableTreeNode top) {
    DefaultMutableTreeNode category = null;
    DefaultMutableTreeNode book = null;
    category = new DefaultMutableTreeNode("Books for Java Programmers");
    top.add(category);
    book = new DefaultMutableTreeNode("The Java Tutorial: A Short Course on the Basics");
    category.add(book);
    book = new DefaultMutableTreeNode("The Java Tutorial Continued: The Rest of the JDK");
    category.add(book);
    book = new DefaultMutableTreeNode("The JFC Swing Tutorial: A Guide to Constructing GUIs");
    category.add(book);
    book = new DefaultMutableTreeNode("Effective Java Programming Language Guide");
    category.add(book);
    book = new DefaultMutableTreeNode("The Java Programming Language");
    category.add(book);
    book = new DefaultMutableTreeNode("The Java Developers Almanac");
    category.add(book);
    category = new DefaultMutableTreeNode("Books for Java Implementers");
    top.add(category);
    book = new DefaultMutableTreeNode("The Java Virtual Machine Specification");
    category.add(book);
    book = new DefaultMutableTreeNode("The Java Language Specification");
    category.add(book);
    }

    hi sir ,
    thaks for u'r suggession.Its working fine but the
    properties of the previous tree were not working
    after setModel() property .like action at leaf node
    is not working,I'm sorry but I don't understand. I think you are saying that the problem is solved but I can read it to mean that you still have a problem.
    If you still have a problem then please post some code (using the [code] tags of course).

  • JScrollPane auto scrolling.

    Hi Friends,
    Basically I want text in the JTextArea added to the JScrollPane to scroll automatically.
    I have a JFrame to which I have added a horizontal JSplitPane. The Left side of the SplitPane consists of a JPanel with different components and the Right side consists of a JPanel with a JScrollPane containing the JTextArea which displays some Help/News etc.
    I want this text to keep on scrolling continuously, while the user does his work on the Left side, till me application exits.
    Please help me out on this.
    Regards,
    Ravi

    try this:
    JScrollPane scrollPane = new JScrollPane(textArea);
    textArea.setPreferredSize(new Dimension(100,300)):
    scrollpane.setPreferredSize(new Dimension(82,250));
    I am not sure whether it will work or not in your case.but it does work in my application.Hope it works!

  • Need debugging help: Why is JScrollPane auto-scrolling?

    I'm trying to figure out why when I double-click on a cell within a JTable that's wrapped in a JScrollPane in order to open up another window, the JScrollPane automatically scrolls to the top.
    Even when I update the double click operation to simply call setSelected(false), the JScrollPane still automatically scrolls to the top.
    I added myself as a change listener to the scroll pane's row header view port, and captured the following stack trace.
    I'm having trouble debugging exactly what is causing the following chain of events to fire. If I try to put a debug breakpoint on a method like EventQueue.invokeLater() that's probably initiating the following chain of events, I can't even get back to my application window from my IDE without having to dismiss all of the breakpoints that are reached.
    If I try to follow through from the double-click to the actual JScrollPane auto-scroll, I get lost in a maze of Swing events that I can't navigate out of.
    Any suggestions on how to appropriately debug this would be greatly appreciated.
    Thanks,
    Mike
    rowHeaderViewPort stateChanged fired
    chgEvent == javax.swing.event.ChangeEvent[source=javax.swing.JViewport[,1,21,0x329,invalid,layout=javax.swing.ViewportLayout,alignmentX=null,alignmentY=null,border=,flags=8,maximumSize=,minimumSize=,preferredSize=java.awt.Dimension[width=0,height=0],isViewSizeSet=false,lastPaintPosition=,scrollUnderway=false]]
    chgEvent source == javax.swing.JViewport[,1,21,0x329,invalid,layout=javax.swing.ViewportLayout,alignmentX=null,alignmentY=null,border=,flags=8,maximumSize=,minimumSize=,preferredSize=java.awt.Dimension[width=0,height=0],isViewSizeSet=false,lastPaintPosition=,scrollUnderway=false]
    java.lang.Exception: stack trace
         at MyForm$2.stateChanged(MyForm.java:192)
         at javax.swing.JViewport.fireStateChanged(JViewport.java:1341)
         at javax.swing.JViewport.setViewPosition(JViewport.java:1096)
         at javax.swing.ViewportLayout.layoutContainer(ViewportLayout.java:179)
         at java.awt.Container.layout(Container.java:1020)
         at java.awt.Container.doLayout(Container.java:1010)
         at java.awt.Container.validateTree(Container.java:1092)
         at java.awt.Container.validateTree(Container.java:1099)
         at java.awt.Container.validateTree(Container.java:1099)
         at java.awt.Container.validateTree(Container.java:1099)
         at java.awt.Container.validateTree(Container.java:1099)
         at java.awt.Container.validateTree(Container.java:1099)
         at java.awt.Container.validateTree(Container.java:1099)
         at java.awt.Container.validateTree(Container.java:1099)
         at java.awt.Container.validate(Container.java:1067)
         at javax.swing.RepaintManager.validateInvalidComponents(RepaintManager.java:353)
         at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:116)
         at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)
         at java.awt.EventQueue.dispatchEvent(EventQueue.java:454)
         at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
         at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
         at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

    Thanks for your response. I examined the code and nowhere that I can see is it calling either of those two methods.
    At some point, it looks like something within my code or in the UI frameworks that I'm using is invalidating my JScrollPane. I'm having a very difficult time, though, tracking down exactly which component is causing that invalidation.
    Short of writing my own JScrollPane so that I can capture the stack trace of calls to invalidate(), does anyone have any suggestions for debugging this further? I tried to use a conditional breakpoint in Eclipse 2.1.3 (Component.invalidate() where the Component is an instance of JScrollPane), but my breakpoint is never being executed.
    Thanks,
    Mike

  • My JScrollpane wont scroll horizontally?

    hmmmm...
    As the title says, my JSCrollPane wont scroll horizontally. I've passed a customer "Drawer" object to it.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    public class gui extends JFrame{
         Container c;
         JPanel p;
         Drawer d;
         JComboBox box;
         JLabel lab;
         JScrollPane sp=new JScrollPane(p);
         String options[]={"Terminal","Non-Terminal","Repeat","Or"};
         ArrayList list=new ArrayList();
         String lastTerminal="";
         String lastNonTerminal="";
         int x=0;
         int y=350;
         boolean stupid=true;
         int pixelsize=0;
         String orString;
         ArrayList orStringList=new ArrayList();
         ArrayList orList=new ArrayList();
         boolean done=false;
         int count=0;
         int lastx=0;
         int lasty=0;
         public gui(){
              super("Something");
              setSize(900,700);
              c=getContentPane();
              c.setLayout(new BorderLayout(5,5));
              c.setBackground(Color.white);          
              p=new JPanel();
             d=new Drawer();
             box=new JComboBox(options);
             lab=new JLabel("Add:");
            box.addActionListener(new BoxListener());
            box.setOpaque(false);
             p.add(lab);
              p.add(box);
              sp=new JScrollPane(d);
              c.add(sp,BorderLayout.CENTER);
              c.add(p,BorderLayout.SOUTH);
              show();
         public class BoxListener implements ActionListener{
          public void actionPerformed(ActionEvent evt) {
               //if(!stupid){
                if (box.getSelectedIndex()==0)
                     lastTerminal=JOptionPane.showInputDialog("Enter your Terminal name");
                     list.add(new Terminal(lastTerminal,x,y,true));
                     Terminal tt=(Terminal)list.get(list.size()-1);
                     x=x+tt.getStringWidth()+45;
                     repaint();
                if (box.getSelectedIndex()==1)
                       lastNonTerminal=JOptionPane.showInputDialog("Enter your Non-Terminal name");
                     list.add(new Terminal(lastNonTerminal,x,y,false));
                     Terminal tt=(Terminal)list.get(list.size()-1);
                     x=x+tt.getStringWidth()+45;
                     repaint();
             if (box.getSelectedIndex()==3)
                       orStringList.clear();
                       orString=JOptionPane.showInputDialog("Enter your Or, seperate each Or with a |")+"|";
                       StringBuffer sb=new StringBuffer(orString);
                       while(sb.indexOf("|")!=-1)
                            orStringList.add(sb.substring(0,sb.indexOf("|")));
                            sb.delete(0,sb.indexOf("|")+1);
                       int eveny=y;
                        int oddy=y;
                        for(int i=1;i<orStringList.size()+1;i++)
                                 if((i%2)==0)
                                           eveny=eveny+25;
                                           orList.add(new OrTerm((String)orStringList.get(i-1),x+35,eveny,false,x,y,x+130,y));
                                  else
                                            oddy=oddy-25;
                                           orList.add(new OrTerm((String)orStringList.get(i-1),x+35,oddy,false,x,y,x+130,y));
                       OrTerm tt=(OrTerm)orList.get(orList.size()-1);
                     x=x+tt.getStringWidth()+100;
                       repaint();
         public static void main (String args[]) {
              gui app = new gui();
              app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         public class Drawer extends JPanel
              public Drawer()
                   this.setBackground(Color.ORANGE);
         public void paint(Graphics g2)
             super.paint(g2);
             Graphics2D g=(Graphics2D)g2;
              g.setFont(new Font(null,Font.BOLD,15));
              Terminal temp=null;
              OrTerm temp2=null;
              for(int i=0;i<orList.size();i++)
                        temp2=(OrTerm)orList.get(i);
                        pixelsize=SwingUtilities.computeStringWidth(g.getFontMetrics(),temp2.getName());
                        g.drawString(temp2.getName(),temp2.getx(),temp2.gety());
                        g.drawRect(temp2.getx()-4,temp2.gety()-16,pixelsize+7,25);
                        g.drawLine(temp2.getx()-2,temp2.gety()-2,temp2.getx2()-5,temp2.gety2()-2);
                        g.drawLine(temp2.getx()+pixelsize+7,temp2.gety()-2,temp2.getx3(),temp2.gety3());
              for(int i=0;i<list.size();i++)
                   temp=(Terminal)list.get(i);
                   g.drawString(temp.getName(),temp.getx(),temp.gety());
                   pixelsize=SwingUtilities.computeStringWidth(g.getFontMetrics(),temp.getName());
                   if(temp.isTerminal()==true)
                     g.drawOval(temp.getx()-4,temp.gety()-16,pixelsize+7,25);
                   else
                     g.drawRect(temp.getx()-4,temp.gety()-16,pixelsize+7,25);
                   lastx=temp.getx()+pixelsize+7+30;
                   lasty=y-2;
                   //drawing line
                   g.drawLine(temp.getx()+pixelsize+7,y-2,temp.getx()+pixelsize+7+30,y-2); 
                 //drawing little array >
                 g.drawLine(temp.getx()+pixelsize+7+30,y-2,temp.getx()+pixelsize+7+25,y-7);
                  g.drawLine(temp.getx()+pixelsize+7+30,y-2,temp.getx()+pixelsize+7+25,y+5);
    class Terminal {
         private String name;
         private int x2;
         private int y2;
         private boolean terminal;
         public Terminal(String name, int x, int y,boolean boo)
              this.name=name;
              this.x2=x;
              this.y2=y;
              this.terminal=boo;
         public String getName(){return this.name;}
         public int getx(){return this.x2;}
         public int gety(){return this.y2;}
         public int getStringWidth()
              return SwingUtilities.computeStringWidth(Toolkit.getDefaultToolkit().getFontMetrics(new Font(null,Font.BOLD,15)),this.name);
         public boolean isTerminal(){return this.terminal;}
    class OrTerm {
         private String name;
         private int x;
         private int y;
         private int x2;
         private int y2;
         private int x3;
         private int y3;
         private boolean terminal;
         public OrTerm(String name, int x, int y,boolean boo,int x2,int y2,int x3,int y3)
              this.name=name;
              this.x=x;
              this.y=y;
              this.terminal=boo;
              this.x2=x2;
              this.y2=y2;
              this.x3=x3;
              this.y3=y3;          
         public String getName(){return this.name;}
         public int getx(){return this.x;}
         public int gety(){return this.y;}
         public int getx2(){return this.x2;}
         public int gety2(){return this.y2;}
         public int getx3(){return this.x3;}
         public int gety3(){return this.y3;}
         public int getStringWidth()
              return SwingUtilities.computeStringWidth(Toolkit.getDefaultToolkit().getFontMetrics(new Font(null,Font.BOLD,15)),this.name);
         public boolean isTerminal(){return this.terminal;}
         import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    public class gui extends JFrame{
         Container c;
         JPanel p;
         Drawer d;
         JComboBox box;
         JLabel lab;
         JScrollPane sp=new JScrollPane(p);
         String options[]={"Terminal","Non-Terminal","Repeat","Or"};
         ArrayList list=new ArrayList();
         String lastTerminal="";
         String lastNonTerminal="";
         int x=0;
         int y=350;
         boolean stupid=true;
         int pixelsize=0;
         String orString;
         ArrayList orStringList=new ArrayList();
         ArrayList orList=new ArrayList();
         boolean done=false;
         int count=0;
         int lastx=0;
         int lasty=0;
         public gui(){
              super("Something");
              setSize(900,700);
              c=getContentPane();
              c.setLayout(new BorderLayout(5,5));
              c.setBackground(Color.white);          
              p=new JPanel();
         d=new Drawer();
         box=new JComboBox(options);
         lab=new JLabel("Add:");
    box.addActionListener(new BoxListener());
    box.setOpaque(false);
         p.add(lab);
              p.add(box);
              sp=new JScrollPane(d);
              c.add(sp,BorderLayout.CENTER);
              c.add(p,BorderLayout.SOUTH);
              show();
         public class BoxListener implements ActionListener{
         public void actionPerformed(ActionEvent evt) {
              //if(!stupid){
              if (box.getSelectedIndex()==0)
         lastTerminal=JOptionPane.showInputDialog("Enter your Terminal name");
         list.add(new Terminal(lastTerminal,x,y,true));
         Terminal tt=(Terminal)list.get(list.size()-1);
         x=x+tt.getStringWidth()+45;
         repaint();
              if (box.getSelectedIndex()==1)
         lastNonTerminal=JOptionPane.showInputDialog("Enter your Non-Terminal name");
         list.add(new Terminal(lastNonTerminal,x,y,false));
         Terminal tt=(Terminal)list.get(list.size()-1);
         x=x+tt.getStringWidth()+45;
         repaint();
    if (box.getSelectedIndex()==3)
              orStringList.clear();
              orString=JOptionPane.showInputDialog("Enter your Or, seperate each Or with a |")+"|";
              StringBuffer sb=new StringBuffer(orString);
              while(sb.indexOf("|")!=-1)
                   orStringList.add(sb.substring(0,sb.indexOf("|")));
                   sb.delete(0,sb.indexOf("|")+1);
              int eveny=y;
                        int oddy=y;
                        for(int i=1;i<orStringList.size()+1;i++)
                        if((i%2)==0)
                                  eveny=eveny+25;
                                  orList.add(new OrTerm((String)orStringList.get(i-1),x+35,eveny,false,x,y,x+130,y));
                                  else
                                            oddy=oddy-25;
                                  orList.add(new OrTerm((String)orStringList.get(i-1),x+35,oddy,false,x,y,x+130,y));
              OrTerm tt=(OrTerm)orList.get(orList.size()-1);
         x=x+tt.getStringWidth()+100;
              repaint();
         public static void main (String args[]) {
              gui app = new gui();
              app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         public class Drawer extends JPanel
              public Drawer()
                   this.setBackground(Color.ORANGE);
    public void paint(Graphics g2)
         super.paint(g2);
         Graphics2D g=(Graphics2D)g2;
              g.setFont(new Font(null,Font.BOLD,15));
         Terminal temp=null;
         OrTerm temp2=null;
              for(int i=0;i<orList.size();i++)
                   temp2=(OrTerm)orList.get(i);
                   pixelsize=SwingUtilities.computeStringWidth(g.getFontMetrics(),temp2.getName());
                   g.drawString(temp2.getName(),temp2.getx(),temp2.gety());
                   g.drawRect(temp2.getx()-4,temp2.gety()-16,pixelsize+7,25);
                   g.drawLine(temp2.getx()-2,temp2.gety()-2,temp2.getx2()-5,temp2.gety2()-2);
                   g.drawLine(temp2.getx()+pixelsize+7,temp2.gety()-2,temp2.getx3(),temp2.gety3());
         for(int i=0;i<list.size();i++)
              temp=(Terminal)list.get(i);
              g.drawString(temp.getName(),temp.getx(),temp.gety());
              pixelsize=SwingUtilities.computeStringWidth(g.getFontMetrics(),temp.getName());
              if(temp.isTerminal()==true)
              g.drawOval(temp.getx()-4,temp.gety()-16,pixelsize+7,25);
              else
              g.drawRect(temp.getx()-4,temp.gety()-16,pixelsize+7,25);
              lastx=temp.getx()+pixelsize+7+30;
              lasty=y-2;
              //drawing line
              g.drawLine(temp.getx()+pixelsize+7,y-2,temp.getx()+pixelsize+7+30,y-2);
         //drawing little array >
         g.drawLine(temp.getx()+pixelsize+7+30,y-2,temp.getx()+pixelsize+7+25,y-7);
         g.drawLine(temp.getx()+pixelsize+7+30,y-2,temp.getx()+pixelsize+7+25,y+5);
    class Terminal {
         private String name;
         private int x2;
         private int y2;
         private boolean terminal;
         public Terminal(String name, int x, int y,boolean boo)
              this.name=name;
              this.x2=x;
              this.y2=y;
              this.terminal=boo;
         public String getName(){return this.name;}
         public int getx(){return this.x2;}
         public int gety(){return this.y2;}
         public int getStringWidth()
              return SwingUtilities.computeStringWidth(Toolkit.getDefaultToolkit().getFontMetrics(new Font(null,Font.BOLD,15)),this.name);
         public boolean isTerminal(){return this.terminal;}
    class OrTerm {
         private String name;
         private int x;
         private int y;
         private int x2;
         private int y2;
         private int x3;
         private int y3;
         private boolean terminal;
         public OrTerm(String name, int x, int y,boolean boo,int x2,int y2,int x3,int y3)
              this.name=name;
              this.x=x;
              this.y=y;
              this.terminal=boo;
              this.x2=x2;
              this.y2=y2;
              this.x3=x3;
              this.y3=y3;          
         public String getName(){return this.name;}
         public int getx(){return this.x;}
         public int gety(){return this.y;}
         public int getx2(){return this.x2;}
         public int gety2(){return this.y2;}
         public int getx3(){return this.x3;}
         public int gety3(){return this.y3;}
         public int getStringWidth()
              return SwingUtilities.computeStringWidth(Toolkit.getDefaultToolkit().getFontMetrics(new Font(null,Font.BOLD,15)),this.name);
         public boolean isTerminal(){return this.terminal;}
         

    Scrollbars will appear when the preferred size of the component is greater than the size of the scroll pane. If you are adding a custom drawn component to the scrollpane then you must set the preferredSize.

  • Why the JScrollPane not scroll?

    I create a dialog,then I put a JScrollPane on the dialog,after this I put a JPanel on the JScrollPane,
    finally I put many checkbox on the JPanel.Due to having many checkboxes, I hope that JScrollPane can scroll,but it cannot scroll .how can I sove this problem?
    the following is my code:
    this.setSize(new Dimension(543, 523));
    this.getContentPane().setLayout(null);
    jButton3.setText("ok");
    jButton3.setBounds(new Rectangle(235, 455, 95, 30));
    jButton4.setText("cancel");
    jButton4.setBounds(new Rectangle(395, 455, 100, 30));
    // jScrollPane1.setBounds(new Rectangle(0, 20, 515, 1195));
    // jScrollPane1.setAutoscrolls(true);
    jPanel1.setBackground(Color.cyan);
    jPanel1.setLayout(null);
    jPanel1.setAutoscrolls(true);
    jPanel1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    jCheckBox1.setText("attrib_manager");
    jCheckBox1.setBounds(new Rectangle(10, 10, 110, 25));
    ������������
    jCheckBox66.setBounds(new Rectangle(205, 1120, 97, 26));
    jCheckBox66.setEnabled(false);
    ������������
    jPanel1.add(jCheckBox66, null);
    jPanel1.add(jCheckBox1, null);
    JScrollPane jScrollPane1 = new JScrollPane(jPanel1);
    jScrollPane1.setBounds(new Rectangle(0, 20, 515, 425));
    jScrollPane1.setAutoscrolls(true);
    jScrollPane1.getViewport().add(jPanel1, null);
    this.getContentPane().add(jScrollPane1, null);
    this.getContentPane().add(jButton4, null);
    this.getContentPane().add(jButton3, null);

    You have to set size of your panel.
    override method getPrefferedSize() of panel. This methd should return correct size of content.
    best regadrs
    Stas

  • JTree in JScrollPane doesn't scroll

    I have a JTree in a JScrollPane that doesn't scroll.
    Background: it used to!
    History: I moved the creation of the JTree to inside an Object that returns it with a ".getTree()" method. Since then, it hasn't been scrolling. I even set the JScrollPane to always have scrollbars and they just sit there inactive.
    Does anyone know what this would happen and/or how to fix it?
    thanks.

    That actaully worked, but it was way to big. I'm not sure what I did, but I changed the values to Studio ONE's defaults and now it works ok.

  • JTree in JScrollpane, find out

    I have following constellation:
    A JTree in a JScrollpane with a vertical scrollbar.
    Most nodes are hidden "behind the scrollpane" and only view are seen.
    I search for a property which signals if a node is currently seen on screen and which are "scrolled" (that means those nodes which aren't visible) (the jcomponent method isVisible() doesn't work)
    I think about such a method which uses the information of the scrollbarposition and the dimension of the jviewport but i can't get the information which node is seen if some nodes are expanded and some are collapsed.
    Does anybody know such a property or method?
    Or is there a SWING-THING which solves the problem?
    Thank you for your answers

    isVisible() is a "Component-Method" which means i have to take the rendered component finding out the visible nodes.
    my application is following:
    i write a method which expand all nodes until the leafs are visible but because of performance (i have more than 1000 nodes directly under the root) i want to expand only the nodes which are showing and expand the other later.

  • Refresing JTree with JScrollPane

    I have made a JTree where the nodes can be dynamically added and removed, this tree is added to a scroll pane:
    JTree tree = new JTree();
    JScrollPane scrollpane = new JScrollPane(tree);
    The object that instantiates the tree extends from JPanel and the scrollpane is added to the panel.
    Then the tree is added to a JSplitPane. When I resize the window the scrollbars will appear, although when the nodes are added to the tree they will keep extending without having the scrollbar appear.
    Does anyone know a good way to make the scroll bars appear when the tree gets larger than the frame?

    I have made a JTree where the nodes can be dynamically added and removed, this tree is added to a scroll pane:
    JTree tree = new JTree();
    JScrollPane scrollpane = new JScrollPane(tree);
    The object that instantiates the tree extends from JPanel and the scrollpane is added to the panel.
    Then the tree is added to a JSplitPane. When I resize the window the scrollbars will appear, although when the nodes are added to the tree they will keep extending without having the scrollbar appear.
    Does anyone know a good way to make the scroll bars appear when the tree gets larger than the frame?

  • JScrollPane not scrolling to the end of JTable

    Hello,
    I put a table in a scroll pane, and add the scroll pane to a panel. The problem is the scroll pane does not scroll to the end of the table, it always lets me view only the first 37 rows of the table, regardless of how many rows the table actually has. Basically, the scroll pane thinks the table has exactly the same height, no matter how many lines it has. Why is this happening?
    The code is something like
    model = new SearchResultTableModel(false, null, null);
    table = new ResultTable(model);
    scrollPane = new JScrollPane(table);
    mainBox.add(scrollPane);
    If this is important, the table is a bit more complicated, I subclass JTable, use a custom TableModel, several types of cell renderers / editors, so maybe this is why it behaves so strange. How could I tell the scroll pane the table is actually larger?
    Thank you very much, any ideas are highly appreciated,
    Regards from Romania,
    Adi

    Hi,
    fireTableDataChanged() is the easiest way to do it, but also that one, that forces JTable to rerender all visible cells, regardless the fact, that they are already displayed and would not need rerendering - if you add a row - for example row 7 - do fireTableRowsInserted(7,7); instead - so JTable will only render this single row if it is visible in the viewport. If you add a couple of rows, for example row 7 to 227, do fireTableRowsInserted(7,227); - always fire only the correct notifications accordingly to the changes you make, in order to keep the performance of JTable high.
    greetings Marsian

  • JScrollPane wont scroll .. pls help

    hi to all,
    I have a scrollpane that i am displaying a graph in, and they will be large graphs, 10000 nodes with about cardinality 10 for each node.
    I can generate the graph output no problems but my scrollbars dont allow me to scroll the pane, and i cant figure out why.
    I'm assuming that i should be doing something with the viewport once i add the graph, but i dont know what.
    can anyone pls help me,
    also can anyone suggest how i go about zooming in and out. im thinking its just resizng the viewport, but i need to work out why i cant scroll before i tackle that.
    package graphappz.ui;
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import graphappz.graph.Graph;
    import java.util.Iterator;
    import graphappz.graph.Vertex;
    import graphappz.graph.Edge;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
    public class GraphWindow
        extends JPanel {
      JEditorPane graphWindow;
      JScrollPane graphView;
      Dimension d = new Dimension();
      Point2D p = null;
      GraphViewer viewer;
      public GraphWindow() {
        graphWindow = new JEditorPane();
        graphWindow.setText("");
        viewer = new GraphViewer();
        graphView = new JScrollPane(viewer);
        graphView.setHorizontalScrollBarPolicy(JScrollPane.
                                               HORIZONTAL_SCROLLBAR_ALWAYS);
        graphView.setVerticalScrollBarPolicy(JScrollPane.
                                             VERTICAL_SCROLLBAR_ALWAYS);
        graphView.getViewport().setBackground(Color.white);
        graphView.setBorder(BorderFactory.createEtchedBorder());
        graphView.setToolTipText("Graph Output Display Window");
        repaint();
      public JScrollPane getGraphView() {
        return this.graphView;
      public Dimension getSize() {
        graphView.setPreferredSize(new Dimension(520, 440));
        return graphView.getPreferredSize();
      class GraphViewer
          extends JPanel {
        public void paintComponent(Graphics g) {
          super.paintComponent(g);
          int nodeWidth = 20;
          int nodeHeight = 20;
          if (graphappz.Main.graph != null) {
            Graph graph = graphappz.Main.graph;
            Iterator iter = graph.edgeIter();
            while (iter.hasNext()) {
              Edge e = (Edge) iter.next();
              Vertex v_0 = e.getv0();
              Vertex v_1 = e.getv1();
              if ( ( (v_0.getLayoutPosition() != null) ||
                    (v_1.getLayoutPosition() != null))) {
                Point2D.Double pos_0 = v_0.getLayoutPosition();
                Point2D.Double pos_1 = v_1.getLayoutPosition();
                // draw the edge
                g.setColor(Color.red);
                g.drawLine( (int) pos_0.x + (nodeWidth / 2),
                           (int) pos_0.y + (nodeHeight / 2),
                           (int) pos_1.x + (nodeWidth / 2),
                           (int) pos_1.y + (nodeHeight / 2));
                // draw the first node
                g.setColor(Color.blue);
                g.drawOval( (int) pos_0.x, (int) pos_0.y, nodeWidth, nodeHeight);
                g.fillOval( (int) pos_0.x, (int) pos_0.y, nodeWidth, nodeHeight);
                // draw the second node
                g.setColor(Color.blue);
                g.drawOval( (int) pos_1.x, (int) pos_1.y, nodeWidth, nodeHeight);
                g.fillOval( (int) pos_1.x, (int) pos_1.y, nodeWidth, nodeHeight);
                // draw the edge
                g.setColor(Color.black);
                g.drawString(v_0.getReference() + "", (int) pos_0.x + 5,
                             (int) pos_0.y + 14);
          else {
            graphappz.util.Debug.debugText.append("\n graph is null, can't draw it");
            //TODO: show new Alert_Dialog
        public GraphViewer() {
          this.setBackground(Color.white);
          this.addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent e) {}
            public void mousePressed(MouseEvent e) {}
            public void mouseReleased(MouseEvent e) {}
            public void mouseEntered(MouseEvent e) {}
            public void mouseExited(MouseEvent e) {}
    }

    Scrollbars will appear when the preferred size of the panel is greater than the preferred size of the scrollpane.
    When you add components (JButton, JTextField...) to a panel then the preferred size can be calculated by the layout manager.
    When you draw on a panel the layout manager has no idea what the size of you panel is so you need to set the preferrred size yourself:
    panel.setPreferredSize(...);

  • JScrollPane's scroll bar alignment

    I tried all the suggested methods, and yet I still get the vertical scroll bar aligned somewhere in the middle when the JFrame opens, when top alignment would be preferable... Any suggestion on this is more than welcome. My code is structured with nested components like this:
    JScrollPane scroll
    ---JPanel allJPanels
    ------JPanel a
    ---------JPanel a_1
    ---------JLabel a_2
    ---------JEditorPane a_3
    ------JPanel b
    ---------JPanel b_1
    ---------JLabel b_2
    ---------JEditorPane b_3
    scroll.setViewportView( allJPanels );
    Methods attempted:
    scroll.getVerticalScrollBar().setValue( scroll.getVerticalScrollBar().getMinimum() );
    OR
    scroll.getViewport().setViewPosition( new java.awt.Point( 0, 0 ) );
    OR
    scroll.getVerticalScrollBar().getModel().setValue( 0 );     
    OR
    allJPanels.scrollRectToVisible( new java.awt.Rectangle( 1, 1, allJPanels.getWidth(), allJPanels.getHeight() ) );
    Thanks for any suggestion

    Could you please explain your exact requirement. If you just want whether you have to show the scrollpane or not then there is a policy in JScrollpane in which you can set the vertical scrollbar policy as required.

  • JScrollPane's scroll

    i have a jpalel onto JScrollPane, how can I know from program this jcrollpane's vertical scroll is shown or not ( this panel needs scroll or not ).
    Has a JScrollpane object or variable , which messages me this .
    I dont wont solve this problem with size of panel. I wont enything usable and symple.
    enyone know how can I do?

    Could you please explain your exact requirement. If you just want whether you have to show the scrollpane or not then there is a policy in JScrollpane in which you can set the vertical scrollbar policy as required.

  • JScrollPane Must Scroll to the END ........

    My problem is :
    I have a JTextArea , and the text inside doesn't fit
    in it. When this happens , the two ScrollBar should
    scroll to the END of the text....
    Like if a have a JTextArea with 10 rows with a JScrollPane, and my text is 20 rows long, then i
    need to see the end of the text(No manual Scrolling)
    Are there any method to control the Scrollbar position ?
    Help....!!!!!!!

    Hi,
    When you move the caret position of your JTextArea, then the scollbars follow. Try this (assuming your JTextArea is called textArea):
    textArea.setCaretPosition(textArea.getLineEndOffset(textArea.getLineCount()-1));This scrolls to the end of the JTextArea.
    Hope this helps,
    Kurt.

Maybe you are looking for

  • Prodagio wants to use BAPI_PO_GETDETAIL, but here's the problem !!!!!

    For some PO line items, there can be multiple entries for the same line item number if there are freight charges, etc. You can see the extra entries in ME23, among other places - there will be more than one entry for the same PO line item. BAPI_PO_GE

  • How to set password for every application

    Hi, As a administrator how to set password for every weblication in sharepoint 2013. Is there any way to set? Thanks,

  • Handling Back Button UIX ADF Application

    Hi, I have a similar situation, in which the user goes through 6 steps (UIX pages) on a registration process. If the user is in middle of the registration process and hits the back button, It shows the previous page with entered values, If he hits th

  • Macbookpro noise issue on right side with heat

    Hi, my 2014 macbook pro ratina display,13 inch, just finished a years warranty period, and suddenly from right side of the laptop, a noise starts (like a modem connecting or something burning kind a noise) with upper right corner more heated then the

  • Unicode vs non-unicode

    Hi All, Can anyone please tell me what is the differenece between unicode and non-unicode SAP systems. I heard that all the new versions are unicode. What is the difference, purpose and significance of these? Thanks Cyrus