Results 1 to 7 of 7
  1. #1
    iimasd is offline Member
    Join Date
    Nov 2007
    Posts
    10
    Rep Power
    0

    Default dynamising the height of a JPopupMenu

    how can we dynamise the height of JPopupMenu in order to contain the full text-message from a JLabel, ie, a JLabel containing some long text messages is shown inside a JPopupmenu of a fixed width.so, i want that the text should automatically fit to its width go to another line downwards to the dynamically generated height.
    Need ur valuable help.Thanks.

  2. #2
    iimasd is offline Member
    Join Date
    Nov 2007
    Posts
    10
    Rep Power
    0

    Default

    lemme clarify my problem:-
    a JLabel (containing some texts) is passed into a JPopupMenu(of a fixed size).But the whole texts are not shown due to the short fixed width of the JPopupMenu. I want that : the height of the JPopupMenu should automatically increase to display the full text.
    TIA.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Some ideas in the absence of more detail:
    Once you set the text in the JLabel you can get its preferredSize and use this to help determine the size needed for the popupMenu. Dealing with line breaks may take some extra effort: you have the option to use html in the label string or you can use LineBreakMeasurer (api has example code) for this.
    Another option is to use a JTextArea that will handle line-wrapping and can give an accurate preferredSize. You can set it non-focusable, remove the border, etc.
    JOptionPane is set up to do this automatic sizing.

  4. #4
    iimasd is offline Member
    Join Date
    Nov 2007
    Posts
    10
    Rep Power
    0

    Default

    thanks hardwired, let me try these 3 ways.
    I tried with the JTextArea method , but struggling to create new lines for the text inside the JTextArea of the JpopupMENU.
    Any help would be grateful to me.Thanks.

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    create new lines for the text inside the JTextArea
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);

  6. #6
    iimasd is offline Member
    Join Date
    Nov 2007
    Posts
    10
    Rep Power
    0

    Default

    Ya, I completed this code, but with some calculations as shown in the BOLD text down.I am sending my code downwards.thanks.
    Java Code:
    import javax.swing.*;
    import javax.swing.JPopupMenu;
    import java.awt.event.*;
    import java.awt.*;
    import java.awt.Component;
    import javax.swing.border.*;
    
    class PopUpHeightClass extends  JFrame{
        public JFrame BaseFrame=null;
        public JPanel Title_Panel,Content_Panel=null;
        public JLabel title_Label=null;
        public JTextArea content_TextArea = null;
        public Container menuContainer=getContentPane();
        public int popheight=100;//any height for initializing.
        public int popwidth=400;
        public int n_chrctrs_per_line = 48;//for a popwidth of '400'.
        public String characters_in_line = "";
        
        PopUpHeightClass(String mytitle,String mycontents) {
            BaseFrame = new JFrame("Base Frame");
            BaseFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            BaseFrame.setBounds(150,150,1000,350);
            JPanel BasePanel = new JPanel();
            BasePanel.setBorder(new LineBorder(Color.BLACK));
            BasePanel.setSize(100,20);
            JLabel baseTesting_Label = new JLabel("Testing Button :-");
            baseTesting_Label.setBorder(new LineBorder(Color.RED));
            JButton click_Button= new JButton("??");
            BasePanel.add(baseTesting_Label);
            BasePanel.add(click_Button);
            BaseFrame.getContentPane().add(BasePanel);
            BaseFrame.setVisible(true);
            title_Label=new JLabel(mytitle);
            content_TextArea=new JTextArea("");
            content_TextArea.setBorder(new LineBorder(Color.BLUE));
            
            [B]int n_lines = (mycontents.length()/n_chrctrs_per_line)+1;
            try {
                for (int i = 0; i < mycontents.length(); i++) {
                    if ((mycontents.length()-i)>n_chrctrs_per_line) {
                        characters_in_line = mycontents.substring(i,i+n_chrctrs_per_line);
                    } else {
                        characters_in_line = mycontents.substring(i,mycontents.length());
                    }
                    i=i+n_chrctrs_per_line-1;
                    content_TextArea.append("\n");
                    content_TextArea.append(characters_in_line.trim());
                }
            } catch (Exception e) {
            }
            content_TextArea.setEditable(false);
            if (n_lines<4) {
                popheight = 120;
            }else popheight = (18*n_lines)+60;
            System.out.println("n_lines== "+n_lines+" popheight== "+popheight); [/B]       
            content_TextArea.setPreferredSize(new Dimension(popwidth-10,popheight-10));
            
            Title_Panel=new JPanel();
            Title_Panel.setLayout(new FlowLayout(FlowLayout.CENTER));
            Title_Panel.add(title_Label);
            Title_Panel.setBorder(new LineBorder(Color.BLUE));
            Content_Panel=new JPanel();
            Content_Panel.setLayout(new FlowLayout(FlowLayout.LEFT));
            Content_Panel.add(content_TextArea);
            Content_Panel.setBorder(new LineBorder(Color.RED));
            menuContainer.add(Title_Panel,BorderLayout.NORTH);
            menuContainer.add(Content_Panel,BorderLayout.CENTER);
            
            click_Button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent ae){
                    JPopupMenu popup = new JPopupMenu();
                    popup.setBorder(new LineBorder(Color.BLACK));
                    popup.add(menuContainer);
                    popup.setPopupSize(popwidth,popheight);
                    popup.show(BaseFrame,50,80);
                 }
            });
        }
    }
    public class PopupMenuClass {
        public static void main(String[] args) {
            String contents = "KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKAAAAAAAAAAAAAAAKKKKK KKKKKKKKKKKKKKKK HHHHHHHHHHHHHHH   FOR";
            PopUpHeightClass ob = new PopUpHeightClass("My Title",contents);
            
        }                                             
    }

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import javax.swing.*;
    import javax.swing.JPopupMenu;
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.text.*;
    import javax.swing.border.*;
    
    class PHC extends JFrame{
        public JPanel Title_Panel,Content_Panel=null;
        public JLabel title_Label=null;
        public JTextArea content_TextArea = null;
    //    public Container menuContainer=getContentPane();
        public int popheight=100;            //any height for initializing.
        public int popwidth=400;
        public int n_chrctrs_per_line = 48;  //for a popwidth of '400'.
        public String characters_in_line = "";
         JPopupMenu popup;
        
        PHC(String mytitle, String mycontents) {
            super("Base Frame");
            final PHC frame = this;
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setBounds(150,150,600,350);
            initPopup(mytitle, mycontents);
            JPanel BasePanel = new JPanel();
            BasePanel.setBorder(new LineBorder(Color.BLACK));
    //        BasePanel.setSize(100,20);
            JLabel baseTesting_Label = new JLabel("Testing Button :-");
            baseTesting_Label.setBorder(new LineBorder(Color.RED));
            JButton click_Button= new JButton("show popup");
            BasePanel.add(baseTesting_Label);
            BasePanel.add(click_Button);
            getContentPane().add(BasePanel, BorderLayout.PAGE_START);
            setVisible(true);
    
            click_Button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent ae){
                    popup.show(frame, 50,80);
                    popup.pack();
                 }
            });
        }
    
        private void initPopup(String mytitle, String mycontents) {
            title_Label=new JLabel(mytitle);
            content_TextArea=new JTextArea("");
            content_TextArea.setBorder(new LineBorder(Color.BLUE));
            content_TextArea.setEditable(false);
    /*
            int n_lines = (mycontents.length()/n_chrctrs_per_line)+1;
            try {
                for (int i = 0; i < mycontents.length(); i++) {
                    if ((mycontents.length()-i)>n_chrctrs_per_line) {
                        characters_in_line = mycontents.substring(i,i+n_chrctrs_per_line);
                    } else {
                        characters_in_line = mycontents.substring(i,mycontents.length());
                    }
                    i=i+n_chrctrs_per_line-1;
                    content_TextArea.append("\n");
                    content_TextArea.append(characters_in_line.trim());
                }
            } catch (Exception e) {
            }
            if (n_lines<4) {
                popheight = 120;
            }else popheight = (18*n_lines)+60;
            System.out.println("n_lines== "+n_lines+" popheight== "+popheight);
    */
            popwidth = 300;
            popheight = 120;  // 400, 120
            content_TextArea.setPreferredSize(new Dimension(popwidth-10,popheight-10));
            content_TextArea.setSize(popwidth, popheight);
    
            content_TextArea.setLineWrap(true);
            content_TextArea.setText(mycontents);
    
            int pos = content_TextArea.getDocument().getLength();
            Rectangle r = null;
            try {
                // Rectangle[x=221,y=33,width=1,height=16]
                r = content_TextArea.modelToView(pos);
                System.out.printf("r = [%d, %d, %d, %d]%n",
                                   r.x, r.y, r.width, r.height);
            } catch(BadLocationException e) {
                System.out.println("Bad location: " + e.getMessage());
            }
            content_TextArea.setCursor(Cursor.getDefaultCursor());
            content_TextArea.addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseMoved(MouseEvent e) {
                    System.out.println("x = " + e.getX() + "  y = " + e.getY());
                }
            });
    
            Dimension d = content_TextArea.getPreferredSize();
            d.height = r.y + r.height;
            content_TextArea.setPreferredSize(d);
    
            Title_Panel=new JPanel();
            Title_Panel.setLayout(new FlowLayout(FlowLayout.CENTER));
            Title_Panel.add(title_Label);
            Title_Panel.setBorder(new LineBorder(Color.BLUE));
            Content_Panel=new JPanel(new BorderLayout());
    //        Content_Panel.setLayout(new FlowLayout(FlowLayout.LEFT));
            Content_Panel.add(content_TextArea);
            Content_Panel.setBorder(new LineBorder(Color.RED));
            JPanel menuContainer = new JPanel(new BorderLayout());
            menuContainer.add(Title_Panel,BorderLayout.NORTH);
            menuContainer.add(Content_Panel,BorderLayout.CENTER);
    
            popup = new JPopupMenu();
            popup.setBorder(new LineBorder(Color.BLACK));
            popup.setLayout(new BorderLayout());
            popup.add(menuContainer);
    //        popup.setPopupSize(popwidth,popheight);
        }
    
        public static void main(String[] args) {
            String contents = "KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKAAAAA" +
                              "AAAAAAAAAAKKKKK KKKKKKKKKKKKKKKK HHHHHHHHHHHHHHH   FOR";
            new PHC("My Title",contents);
        }
    }

Similar Threads

  1. JPopupMenu falls behind window
    By undertow in forum AWT / Swing
    Replies: 4
    Last Post: 01-17-2008, 10:20 PM
  2. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

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