Results 1 to 7 of 7
- 11-16-2007, 11:24 AM #1
Member
- Join Date
- Nov 2007
- Posts
- 10
- Rep Power
- 0
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.
- 11-16-2007, 01:52 PM #2
Member
- Join Date
- Nov 2007
- Posts
- 10
- Rep Power
- 0
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.
- 11-16-2007, 07:06 PM #3
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.
- 11-20-2007, 07:46 AM #4
Member
- Join Date
- Nov 2007
- Posts
- 10
- Rep Power
- 0
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.
- 11-20-2007, 08:03 AM #5
create new lines for the text inside the JTextArea
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
- 11-21-2007, 08:31 AM #6
Member
- Join Date
- Nov 2007
- Posts
- 10
- Rep Power
- 0
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); } }
- 11-21-2007, 10:01 AM #7
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
-
JPopupMenu falls behind window
By undertow in forum AWT / SwingReplies: 4Last Post: 01-17-2008, 10:20 PM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks