Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

adding a scrollbar to a jframe in java

Options
  • 04-01-2004 1:45am
    #1
    Closed Accounts Posts: 1,525 ✭✭✭


    I though this would be pretty trivial but its getting quite annoying. what i did was set the jframes content pane equal to a scrollpane and then added the panel i had been using as the content pane to that scroll pane. i then set horizontal scroll bars to never and vertical to always. The problem is that this messes up horizontal resizing. I'm using a combination of gridbaglayouts and flowlayouts. Normally these resize properly when the jframe is resized. with the scrollbar however the frame seems to have set itself a minimum horizontal size under which the contents of the frame don't get resized. I'm wondering if anyone has any better ideas about adding a scroll bar to a jframe.


Comments

  • Registered Users Posts: 19,396 ✭✭✭✭Karoma


    paste your code.

    try redefining
    getScrollableTracksViewportWidth()
    {
      return false;
    }
    
    or something close to that. been a while since i played with SWING. look through API ...


  • Closed Accounts Posts: 1,525 ✭✭✭vorbis


    thanks, i'll try doing that tonight.
    Just another question. Does anyone know if theres an easy to get selected text from a jtextfield IF the user uses the copy menu item. Do i have to manually look for the component in focus each time and call its getselectedtext method? Thanks


  • Registered Users Posts: 841 ✭✭✭Dr Pepper


    Don't quite understand what you're asking ("copy menu item" - is that like right-click on the textfield and select copy?) A test to see if the source of an action is a JTextField (or any component type) is:
    public void actionPerformed(ActionEvent e)
    {
    	// Is the source of the action an instance of JTextField
    	if( e.getSource().getClass().isInstance(new JTextField()) )
    	{
    		// Cast to a JTextField and use the getSelectedText() method
    		String selectedText = ((JTextField)e.getSource()).getSelectedText();
    	}
    }
    

    This example does not require that you know what object performed the action. If it was a JTextField, it gets the selected text..


  • Closed Accounts Posts: 1,525 ✭✭✭vorbis


    thanks for the reply dr pepper.
    I already do somehting similar with actioncommands.
    What I meant was the edit menu at the top of the program. When I click on copy in that menu I want it to get the text selected in a textbox. Its just in case my user is used to copying and pasting that way.
    I'm going to test using
    KeyboardFocusManager.getFocusOwner (returns the component in focus)
    to see if it can do what i want.


  • Closed Accounts Posts: 302 ✭✭Auburn


    I think this is what you're looking for:
    menuItem = new JMenuItem(new DefaultEditorKit.CopyAction());
    menuItem.setText("Copy");
    

    DefaultEditorKit, the CopyAction() method in the API. Think it also has cut and paste there too.


  • Advertisement
  • Closed Accounts Posts: 1,525 ✭✭✭vorbis


    Karoma thanks implmenting scollable works. All I need to do now is add some code so that the component will resize vertically when enlarged and scroll when reduced.
    Thanks once again


Advertisement