Question

well I've had a few tips on using the splitpane to split my frame into two areas, but I can't manage to get it to show something useful. The code looks as follows:

public class Whiteboard extends JPanel {

int width = 600;
int sidePanelWidth = 200;
int lineHeight = 120;
int numberOfLines = 5;
JFrame frame = null;
Glyph glyph = null;
//java.awt.Rectangle bounds = new java.awt.Rectangle();
Bounds bounds = null;
JSplitPane splitPane = null;
JPanel tools = null;


public Whiteboard() {
    frame = new JFrame();
    frame.setSize(width + sidePanelWidth, getFullHeight());
    FlowLayout simpleLayout = new FlowLayout();
    frame.setLayout(simpleLayout);

    tools = new JPanel();
    tools.setSize(new Dimension(sidePanelWidth, getFullHeight()));
    this.setSize(width, getFullHeight());

    splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, this, tools);
    splitPane.setPreferredSize(new Dimension(width + sidePanelWidth, getFullHeight()));
    splitPane.setOneTouchExpandable(false);
    splitPane.setDividerLocation(150);

    frame.add(splitPane);
    this.setBackground(Color.white);
    java.awt.Rectangle rectBounds = this.getBounds();
    bounds = new Bounds((int)rectBounds.getX(), (int)rectBounds.getY(), (int)(rectBounds.getX() + rectBounds.getWidth()), (int)(rectBounds.getY() + rectBounds.getHeight()));

}

public int getFullHeight() {
    return lineHeight * numberOfLines;
}

I changed now the code like this:

    public static void main(String[] args) {
    int sidePanelWidth = 200;

    JFrame frame = new JFrame();        
    Whiteboard whiteboard = new Whiteboard();         

    JPanel sidePanel = new JPanel();
    sidePanel.setPreferredSize(new Dimension(sidePanelWidth, whiteboard.getFullHeight()));


    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    splitPane.add(whiteboard, JSplitPane.LEFT);
    splitPane.add(sidePanel, JSplitPane.RIGHT);

    frame.setLayout(new FlowLayout());
    frame.getContentPane().add(splitPane);


    frame.pack();
    frame.setVisible(true);        
    whiteboard.repaint();
}

And the constructor to this:

public Whiteboard() {
    this.setPreferredSize(new Dimension(width, getFullHeight()));
    this.setBackground(Color.red);   

}

Now I don't know where the problem is, maybe it's because it doesn't call the paintComponent method. I tried forcing it by calling repaint() it doesn't help, it just doesn't call this componenent

Edit: Well now it seems it is calling the paintComponent method after all, but still I get the screen like this: enter image description here

As you can see, it's not looking so good. Well the code of my current main Method:

public static void main(String[] args) {
    int sidePanelWidth = 200;

    JFrame frame = new JFrame();        
    Whiteboard whiteboard = new Whiteboard();         

    JPanel sidePanel = new JPanel();
    sidePanel.setPreferredSize(new Dimension(sidePanelWidth, whiteboard.getFullHeight()));


    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    splitPane.add(whiteboard, JSplitPane.LEFT);
    splitPane.add(sidePanel, JSplitPane.RIGHT);

    frame.setLayout(new FlowLayout());
    frame.getContentPane().add(splitPane);

    frame.pack();
    frame.setVisible(true);        
    whiteboard.repaint();
}

Any idea how to change it to fix the problem? Do I need to post other methods?

Was it helpful?

Solution

Creating a JFrame from within the constructor of a JPanel should really not be done.

Here is an example I created:

Screen shot

import java.awt.Container;
import java.awt.Dimension;
import javax.swing.*;

public class JavaApplication100 {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new JavaApplication100().createAndShowUI();
            }
        });
    }

    private void createAndShowUI() {

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        initComponents(frame.getContentPane());

        frame.pack();
        frame.setVisible(true);
    }

    private void initComponents(Container contentPane) {
        JPanel mainPanel = new JPanel();

        //create our 2 seperate panels (could be custom ones)
        JPanel leftPanel = new JPanel();
        JPanel rightPanel = new JPanel();

        //add labels for viewing
        leftPanel.add(new JLabel("LEFT"));
        rightPanel.add(new JLabel("RIGHT"));

        //just so you can see em or they would be small
        leftPanel.setPreferredSize(new Dimension(300, 300));
        rightPanel.setPreferredSize(new Dimension(300, 300));

        JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

        //add panels to split pane
        jsp.add(rightPanel, JSplitPane.RIGHT);
        jsp.add(leftPanel, JSplitPane.LEFT);

        mainPanel.add(jsp);//add splitpane to mainpanel
        contentPane.add(mainPanel);

    }
}

EDIT/UPDATE:

as per your comment if you want to colour the background override paintComponent(Graphics g) in your WhiteBoard which extendsJPanel like so:

public class WhiteBoard extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red);
g2.fillRect(0, 0, getWidth(), getHeight());
}
}

OTHER TIPS

You could use JSplitPane.setDividerLocation(int) instead...

public class TestSplitPane extends JFrame {

    public TestSplitPane() throws HeadlessException {
        setSize(600, 600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        setLayout(new BorderLayout());

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

        splitPane.setLeftComponent(new JLabel("I'm on the left"));
        splitPane.setRightComponent(new JLabel("I'm on the right"));

        add(splitPane);

        splitPane.setDividerLocation(200);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
//                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                        if ("Nimbus".equals(info.getName())) {
                            javax.swing.UIManager.setLookAndFeel(info.getClassName());
                            break;
                        }
                    }
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                new TestSplitPane().setVisible(true);
            }
        });
    }
}

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top