Results 1 to 8 of 8

Thread: Shifting components in a JToolBar?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Resolved Shifting components in a JToolBar?

    I created a JToolBar that i eventually want to use in an application. When i hook the JToolBar into one of my apps the Buttons dont seem to line up right. I want to shift them over but nothing seems to work. Any ides on how i can do this?
    Code:
      import java.io.*;  
      import java.awt.*; 
      import javax.swing.*; 
      import java.awt.event.*; 
    
      public class ToolBar extends JToolBar{
        JButton redo;  
        JButton refresh; 
        JButton help;
        
        public ToolBar(){
        setFloatable(false);
         
         redo = new JButton(); 
         redo.setIcon(new ImageIcon("C:" +
            File.separator + "JavaLFGraphicsRepository" + File.separator + "toolbarButtonGraphics"
             + File.separator + "general" + File.separator + "Redo24.gif"));
                redo.setToolTipText("Update Database"); 
         
         refresh  = new JButton();
         refresh.setIcon(new ImageIcon("C:" +
            File.separator + "JavaLFGraphicsRepository" + File.separator + "toolbarButtonGraphics"
             + File.separator + "general" + File.separator + "Refresh.24.gif"));
                refresh.setToolTipText("Refresh Table");
     
         help = new JButton(); 
         help.setIcon(new ImageIcon("C:" +
            File.separator + "JavaLFGraphicsRepository" + File.separator + "toolbarButtonGraphics"
             + File.separator + "general" + File.separator + "Help24.gif"));
                help.setToolTipText("Help");    
      
         //add(Box.createVerticalStrut(1)); // Pushes buttons all the way over to the right.
         //addSeparator(new Dimension(0,100)); // does nothing 
         add(help); 
         add(redo);
         add(refresh); 
         
        }   
       }

  2. #2
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    dunno if this helps but got no prob with this code
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class sample extends JFrame{
        public sample(){
            setTitle("Sample");
            setSize(400,300);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            getContentPane().add(new ToolBar(),BorderLayout.NORTH);
        }
    
        // ToolBar
        class ToolBar extends JToolBar{
            JButton redo=new JButton("Redo",new ImageIcon("left.gif")),
                refresh=new JButton("Refresh",new ImageIcon("middle.gif")),
                help=new JButton("Help",new ImageIcon("right.gif"));
            public ToolBar(){
                setFloatable(false);
    
                redo.setVerticalTextPosition(AbstractButton.BOTTOM);
                redo.setHorizontalTextPosition(AbstractButton.CENTER);
                redo.setToolTipText("Update Database");
    
                refresh.setVerticalTextPosition(AbstractButton.BOTTOM);
                refresh.setHorizontalTextPosition(AbstractButton.CENTER);
                refresh.setToolTipText("Refresh Table");
    
                help.setVerticalTextPosition(AbstractButton.BOTTOM);
                help.setHorizontalTextPosition(AbstractButton.CENTER);
                help.setToolTipText("Help");
    
                add(redo);
                add(refresh);
                add(help);
            }
        }
        public static void main(String[] args){
            new sample().show();
        }
    }

  3. #3

  4. #4
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    Pardon my ignorance but what is Shift?

    edit: you mean, invert the order?
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class sample extends JFrame{
        public sample(){
            setTitle("Sample");
            setSize(400,300);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            getContentPane().add(new ToolBar(),BorderLayout.NORTH);
        }
    
        // ToolBar
        class ToolBar extends JToolBar{
            JButton redo=new JButton("Redo",new ImageIcon("left.gif")),
                refresh=new JButton("Refresh",new ImageIcon("middle.gif")),
                help=new JButton("Help",new ImageIcon("right.gif"));
            public ToolBar(){
                setFloatable(false);
    
                redo.setVerticalTextPosition(AbstractButton.BOTTOM);
                redo.setHorizontalTextPosition(AbstractButton.CENTER);
                redo.setToolTipText("Update Database");
    
                refresh.setVerticalTextPosition(AbstractButton.BOTTOM);
                refresh.setHorizontalTextPosition(AbstractButton.CENTER);
                refresh.setToolTipText("Refresh Table");
    
                help.setVerticalTextPosition(AbstractButton.BOTTOM);
                help.setHorizontalTextPosition(AbstractButton.CENTER);
                help.setToolTipText("Help");
    
                //add(redo);
                //add(refresh);
                //add(help);
    
                add(help);
                add(refresh);
                add(redo);
            }
        }
        public static void main(String[] args){
            new sample().show();
        }
    }
    Works fine but dunno if this is what you mean?
    Last edited by brown monkey; Sep 29th, 2004 at 11:29 PM.

  5. #5

  6. #6

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Notice the two commented lines of code in my previous post. Either one dosen't do the trick. I used the value of one because all other values pushed the buttons all the way over to the right of the JToolBar.
    The other line didnt work either. The height i dont need so 0 should be fine. For the width now matter what value i pass it does nothing to move the buttons.
    Code:
    add(Box.createVerticalStrut(1)); // Pushes buttons all the way over to the right.
    addSeparator(new Dimension(0,100)); // does nothing

  7. #7
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    Dumb me. Got this code and the screenshot.
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class sample extends JFrame{
        public sample(){
            setTitle("Sample");
            setSize(400,300);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            JMenuBar mbar=new JMenuBar();
            mbar.add(new JMenu("File"));
            setJMenuBar(mbar);
            getContentPane().add(new ToolBar(),BorderLayout.NORTH);
        }
    
        // ToolBar
        class ToolBar extends JToolBar{
            JButton redo=new JButton("Redo",new ImageIcon("left.gif")),
                refresh=new JButton("Refresh",new ImageIcon("middle.gif")),
                help=new JButton("Help",new ImageIcon("right.gif"));
            public ToolBar(){
                setFloatable(false);
    
                redo.setVerticalTextPosition(AbstractButton.BOTTOM);
                redo.setHorizontalTextPosition(AbstractButton.CENTER);
                redo.setToolTipText("Update Database");
    
                refresh.setVerticalTextPosition(AbstractButton.BOTTOM);
                refresh.setHorizontalTextPosition(AbstractButton.CENTER);
                refresh.setToolTipText("Refresh Table");
    
                help.setVerticalTextPosition(AbstractButton.BOTTOM);
                help.setHorizontalTextPosition(AbstractButton.CENTER);
                help.setToolTipText("Help");
    
                add(Box.createVerticalStrut(1));
                addSeparator(new Dimension(0,100));
    
                add(help);
                add(refresh);
                add(redo);
            }
        }
        public static void main(String[] args){
            new sample().show();
        }
    }
    Is this the one? Or am missing something.

    Attached Images Attached Images  

  8. #8

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Got it! I should have used add(Box.createHorizontalStrut(100)) not add(Box.createVerticalStrut(100)); Also addSeparator(new Dimension(200,0)); works fine. I just had the args mixed up before so the width was 0 and the height was 200. Thanks for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width