In my application, I have a JFrame displaying a JSplitPane, with the split being VERTICAL_SPLIT. The top is displaying a JLabel, and the bottom is displaying a JInternalFrame. Two problems are occuring.
The JLabel is displaying, but the JInternalFrame is not.
2. I have to resize the application to have the JSplitPane display at all
I believe this is both linked to an incorrect use of JSplitPane. However, I have been unable to work out what. May I please have some help with this issue?
p.s. I have run tests to make sure GUIWindow.getInsideFrame() is not returning null. The instanceof checks at the end are saying that both components in the pane exist and are of that type. Thank you very much for all your help:
protected static void newWindow(GUIFrame window) {
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
JInternalFrame intFrame = window.getInsideFrame();
pane.setRightComponent(intFrame);
pane.setLeftComponent(new JLabel(window.getDescription()));
synchronized(lock){
frame.remove(currentPane);
frame.add(pane);
}
synchronized(lock){
frame.revalidate();
pane.setVisible(true);
frame.repaint();
if(window instanceof ColourFrameShower) return;
currentWindow = window;
currentPane = pane;
currentFrame = intFrame;
}
if(pane.getLeftComponent() instanceof JLabel) System.out.println("JLabel exists!");
else System.out.println("JLabel does not exist!");
if(pane.getRightComponent() instanceof JInternalFrame) System.out.println("JInternalFrame exists!");
else System.out.println("JInternalFrame does not exist!");
}
});
}
EDIT: I fixed problem 2 with a call to frame.revalidate() at the start of the second synchronised(lock) block. This has been included in the code.
As I told you in comment, you simply need to use setVisible(true) on your JInternalFrame, else it will not be considered by your JSplitPane.
This a really common mistake on java swing !
I'm glad it helped you ;)
Try to set the resizeWeight: pane.setResizeWeight(0.5);
Related
I want to dispose() my current jFrame and move to the next jFrame(StudentProfilepage()).But it shows error at this.dispose().
How can i do that.Here i used MouseListner a jLabel l1
My code as follows
l1.setCursor(Cursor.getDefaultCursor());
l1.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent e) {
//added check for MouseEvent.BUTTON1 which is left click
if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON1) {
this.dispose(); //error here(i want to close my current frame and move to StudentProfile() page
new StudentProfilePage().setVisible(true);
}
}
});
this in
this.dispose();
refers to the MouseAdapter, hence the compilation errors you are seeing.
You need to invoke dispose on the JFrame
JFrameClassName.this.dispose();
Also consider using a JDialog rather than a JFrame as the second window. Read The Use of Multiple JFrames, Good/Bad Practice?
You should write
YourClassName.this.dispose();
which points to your jframe.
I want to paint the contents of a JFrame onto another frame. Currently, I only get it to work if the JFrame is visible.Is there a way to paint a hidden JFrame?
Additional info:In my project I need to be able to rotate and scale windows. I do not want to write my own window-api, so I thought I might be able to just paint JFrames or similar container classes in a rotated way (which the Graphics2D-API supports perfectly well). It would be awesome to be able to use standard JFrames for that, but a custom frame extending a JFrame would also be OK..
public class JFTest extends JFrame {
private JFrame frameToPaint = null;
public static void main (String[] args) {
new JFTest ();
}
public JFTest () {
// some basic initialization
super ("Container");
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setExtendedState (JFrame.MAXIMIZED_BOTH);
add (new JPanel () {
#Override public void paintComponent (Graphics g) {
super.paintComponent (g);
// painting the child frame's contents onto this frame
if (frameToPaint != null) frameToPaint.getRootPane().paintAll (g);
}
});
setVisible (true);
// initializing some test-frame that will get painted onto the container
frameToPaint = new JFrame ("child");
frameToPaint.setSize (200, 100);
frameToPaint.add (new JLabel ("test"));
frameToPaint.addComponentListener (new ComponentAdapter() {
#Override public void componentResized (ComponentEvent e) { repaint (); }
#Override public void componentHidden (ComponentEvent e) { repaint (); }
});
// magic line. an invisible frame will not get painted! why??
frameToPaint.setVisible (true);
}
}
Hint 1: JFrame's setDefaultLookAndFeelDecorated(false)/setUndecorated(true) might be of use for a window without caption and borders;
Hint 2: as setGlassPane/setLayeredPane/setOpaque(false) might be of use for a second "layer".
I want to get the graphical contents of a frame without having to make the frame visible for the user
The Screen Image class should help. Although I think it will only work for the "content pane" of the frame and not the entire frame (with the title bar and borders) unless you use a decorated frame.
1) you have to use proper LayoutManager, not setSize() or setBounds()
2) if is there null LayoutManager used then Container returns any size after setVisible(true);
3) if is there used proper LayoutManager, then Container return its Size after call pack();, in other hands this container couldn't be visible on the screen ( meaning setVisible(true); )
4) JComponents must to returns PrefferedSize for example
I want to have a frame that it has 9 planes with red and blue and green color and I set the frame as a borderlayout manager but it doesn't show anything.please help me.thanks
(the LightsNPlanesApp is correct and can be run correctly but the MainFrame is not correct because it doesn't show anything)
The MainFrame:(just the main method)
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
MainFrame frame = new MainFrame();
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
private void addComponentsToPane(Container pane) {
pane.add(new LightsNPlanesApp(), BorderLayout.PAGE_START);
pane.add(new LightsNPlanesApp(), BorderLayout.CENTER);
pane.add(new LightsNPlanesApp(), BorderLayout.PAGE_END);
}
});
}
add("Center", canvas3D);
... is obsolete / wrong and should be replaced with:
add(canvas3D, BorderLayout.CENTER);
How does the code you posted compile? Did you bother to listen to my suggestion about starting with simple code? Is the problem with your custom JPanel or all JPanel'?
Why don't you try adding 3 JPanels each with a different background color and see if that works first. Of course if won't work, but once you figure out that problem, then maybe you can use the same solution on your other class.
Of course because you haven't posted a proper SSCCE, I'm just guessing which is why I'm not giving your what I think the solution is outright. If you are going to make use guess what the code thats causing the problem is like, then you will need to guess the solution as well given the hints provided. And again a SSCCE does not mean you include the full code from your custom panel, it means you post simple code that simulates the problem.
I am trying something very basic:
I have a list of 5 buttons. They are in a FlowLayout and the general idea should be that once I click one it should disappear and the others should reorder themselves accordingly.
Now, if I call setVisible(false) the button becomes invisible, but it still occupies it's space in the Layoutmanager.
Is there any way to keep the Button in the JPanel while hiding it so it doesn't get picked up by Layout?
Update:: Thanks for all the answers, the problem with removing the buttons is that the order is important. The problem I was trying to solve was a find as you type szenario where a very long list of buttons gets filtered down to only the ones matching the characters entered so users can easily click them. Since users can delete characters from the search field ordering is important and buttons have to pop back in once they match again.
Works fine for me.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FlowLayoutInvisible extends JFrame
implements ActionListener
{
JPanel north;
int i;
public FlowLayoutInvisible()
{
north = new JPanel();
for (int i = 0; i < 5; i++)
{
JButton button = new JButton("North - " + i);
button.addActionListener(this);
north.add(button);
}
getContentPane().add(north, BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e)
{
Component c = (Component)e.getSource();
c.setVisible(false);
((JPanel)c.getParent()).revalidate();
}
public static void main(String[] args)
{
FlowLayoutInvisible frame = new FlowLayoutInvisible();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
If you need more help post your SSCCE.
Update: I don't know if the revalidate() is required. I seemed to have a problem once but now I can't duplicate the problem.
Just remove it:
panel.remove( button );
What's wrong with this option?
Layout managers are thought precisely to avoid having the "user" to make tricks in order to have each component it the right place ( although it seems to provoke the opposite effect )
Removing the button from the panel will have the effect of laying out again all the remaining components. That's why it's name is "Layout manager" it manages to layout the components for you.
I see two possibilities:
Write your own layout manager that listens for changes to its children's visible property - shouldn't be too hard, you can probably subclass FlowLayout to do it.
actually remove the clicked-button from the panel and, if necessary, re-add it later.
You could override each button's getPreferredSize() methods (and possibly getMinimumSize() as well to return 0,0 when the component is invisible; and you need to call, I think, invalidate() (or revalidate or validate, I can never keep them straight) on the container.
Do you know any way to remove the border from a JComboBox in Java? I try the following code
public class ComboFrame extends JFrame {
public ComboFrame() {
JPanel container = new JPanel();
JComboBox cmb = new JComboBox(new String[] { "one", "two" });
cmb.setBorder(BorderFactory.createEmptyBorder());
container.add(cmb);
getContentPane().add(container);
pack();
}
}
and
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
EventQueue.invokeLater(new Runnable() {
public void run() {
new ComboFrame().setVisible(true);
}
});
}
Don't ask why would someone want to remove the border from a combobx... I guess it does not make too much sense, but this is how it's wanted, and I got really curious if it can be done. I tried several tricks, but none of them worked.
The most effective was changing the UI with
cmb.setUI(new BasicComboBoxUI());
This makes the border go away, but alters the L&F, and I need to keep the Windows L&F if possible.
Thanks.
I did a bit of research and found this bug
I tried it for myself and it does seem to affect the border. You might want to try one or both of the following code blocks for yourself.
for (int i = 0; i < combo.getComponentCount(); i++)
{
if (combo.getComponent(i) instanceof JComponent) {
((JComponent) combo.getComponent(i)).setBorder(new EmptyBorder(0, 0,0,0));
}
if (combo.getComponent(i) instanceof AbstractButton) {
((AbstractButton) combo.getComponent(i)).setBorderPainted(false);
}
}
It is important to note that at the bottom of the bug entry, you can read the following:
The JButton maintains it's own border so JComponent paintBorder() and paintComponent() has no awareness of the JComboBox border.
Good luck,
Jeach!
If you want to use the windows L&F, you can do cmd.setUI(new WindowsComboBoxUI());
If you, however, want to be able to use any L&F, you might be better off using the solution proposed by Jeach.