Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-16-2007, 01:24 PM
Member
 
Join Date: Nov 2007
Posts: 10
iimasd is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-16-2007, 03:52 PM
Member
 
Join Date: Nov 2007
Posts: 10
iimasd is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-16-2007, 09:06 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-20-2007, 09:46 AM
Member
 
Join Date: Nov 2007
Posts: 10
iimasd is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-20-2007, 10:03 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
create new lines for the text inside the JTextArea
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-21-2007, 10:31 AM
Member
 
Join Date: Nov 2007
Posts: 10
iimasd is on a distinguished road
Ya, I completed this code, but with some calculations as shown in the BOLD text down.I am sending my code downwards.thanks.
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)); 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); 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); } }
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-21-2007, 12:01 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
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); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JPopupMenu falls behind window undertow AWT / Swing 4 01-18-2008 12:20 AM
Error: non-static variable height cannot be referenced from a static context at line fernando AWT / Swing 1 08-01-2007 11:25 PM


All times are GMT +3. The time now is 03:08 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org