JSplitPane and Canvas
As part of an application I'm writing I need to mix the old (heavyweight) Canvas with swing components - specifically nesting them inside a JSplitPane. However, when I do this the divider refuses to resize anywhere as though neither canvas will accept a reduction in size. The code demonstrating the issue is thus:
JFrame frame = new JFrame();
JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new Canvas(), new Canvas());
pane.setResizeWeight(0.5);
frame.add(pane);
frame.pack();
frame.setVisible(true);
I initially assumed this was a simple thing to solve, however after doing a fa开发者_如何学Pythonir bit of research the options seem to present themselves as:
- Use a JPanel instead (not always an option as in my case where I'm using vlcj to render directly onto a canvas)
- Insert x dodgy hack that might work in some cases
Is there a better way of doing this? Or is it really just a case of resorting to hacks? I've debated other options such as if an alternative SplitPane implementation might be available that works, but there's no heavyweight implementation and I'd be surprised if a lightweight one avoided the problem.
A JSplitPane uses the "minimum size" of the component to determine if the component can shrink when using the divider.
I've never used a Canvas before, but itt appears that the minimum size always defaults to the preferred size.
Override the getMinimumSize(...) size method of the Canvas to return a reasonable minimum.
For a quick text you can use:
Canvas canvas = new Canvas();
canvas.setMinimumSize( new Dimension(50, 50) );
JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, canvas, new Canvas());
and you will be able to move the divider left, but never back to the right.
精彩评论