0

This is my first time using java Swing, and I don't know how to paint in the specific JPanel in JSplitPane, and I try to create a new class to implement the paintComponent method, but it can't be Override. Can someone help me?

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

public class SplitPane extends JPanel{

    private JPanel mainPanel;
    private JPanel leftPanel;
    private JPanel rightPanel;


    public SplitPane() {

    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SplitPane().createAndShowUI();
            }
        });
    }

    private void createAndShowUI() {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        initComponents(frame.getContentPane());
        frame.setVisible(true);
    }

    private void initComponents(Container contentPane) {
        mainPanel = new JPanel();
        leftPanel = new JPanel();
        rightPanel = new JPanel();
        leftPanel.add(new JLabel("left"));
        rightPanel.add(new JLabel("right"));
        leftPanel.setPreferredSize(new Dimension(200, 40));
        rightPanel.setPreferredSize(new Dimension(280, 400));
        leftPanel.setBackground(Color.WHITE);
        rightPanel.setBackground(Color.WHITE);

        JSplitPane mainJsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        mainJsp.add(leftPanel, JSplitPane.TOP);
        mainJsp.add(rightPanel, JSplitPane.BOTTOM);
        mainJsp.setOneTouchExpandable(true);
        mainJsp.setDividerLocation(150);
        mainPanel.add(mainJsp);
        contentPane.add(mainPanel);

        leftPanel = new PaintPanel();


    }

    public class PaintPanel extends JPanel {
        public PaintPanel() {
            System.out.println("PaintPanel");
            this.setLayout(new BorderLayout());
            this.setPreferredSize(new Dimension(300, 300));
        }

        @Override
        public void paintComponent(Graphics g) {

            System.out.println("12345678");
            super.paintComponent(g);
            //g.setColor(Color.black);
            g.drawRect(3, 3, 20, 20);
        }
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
mcgG
  • 127
  • 1
  • 8
  • `public void paintComponents(Graphics g) {` should be `public void paintComponent(Graphics g) {` (No **S**). Further `public void paintComponent(Graphics g) { ..` should be `public void paintComponent(Graphics g) { super.paintComponent(g); ..` – Andrew Thompson Oct 14 '15 at 21:00
  • 1
    See also [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Oct 14 '15 at 21:01
  • Thanks! it still doesn't work, I use system.out.print in the paintComponent function, but there is no print out in the console, so I think this function is not be override. – mcgG Oct 14 '15 at 21:08
  • You never add your panel to any container. There is not line such as `add(middleRightPanel)` – Guillaume Polet Oct 14 '15 at 21:17
  • @GuillaumePolet I have done that, I add middleRightPanel to bottomPanel then add bottomPanel to mainJsp, and mainJsp to the Frame, It has to ben done, because I put a label "right" in the middleRightPanel, and it can be shown on the panel – mcgG Oct 14 '15 at 21:26
  • 1
    For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Note that an MCVE should include `import` statements and a `main(String[])` to run it. – Andrew Thompson Oct 14 '15 at 21:30
  • @AndrewThompson, I have put the example code in the first answer, Thanks for check – mcgG Oct 14 '15 at 22:15
  • *"I have put the example code in the first answer,"* 1) It's not an answer so don't put it as one! Instead edit the question by selecting the `edit` link below the tags. 2) Use code formatting on code samples. To do that, select the code and click the `{}` button above the message posting/editing form. – Andrew Thompson Oct 14 '15 at 22:30
  • @AndrewThompson Thanks for the tips, I have done that – mcgG Oct 14 '15 at 22:42
  • `PaintPanel` is never added to anything, your reassign the `leftPanel`, but you never actually add it to anything – MadProgrammer Oct 15 '15 at 00:20
  • @MadProgrammer Thanks a lot , it works now! – mcgG Oct 15 '15 at 01:11

1 Answers1

0

You never add PaintPanel to anything, for example...

private void initComponents(Container contentPane) {
    mainPanel = new JPanel();
    leftPanel = new JPanel();
    rightPanel = new JPanel();
    leftPanel.add(new JLabel("left"));
    rightPanel.add(new JLabel("right"));
    leftPanel.setPreferredSize(new Dimension(200, 40));
    rightPanel.setPreferredSize(new Dimension(280, 400));
    leftPanel.setBackground(Color.WHITE);
    rightPanel.setBackground(Color.WHITE);

    JSplitPane mainJsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    mainJsp.add(leftPanel, JSplitPane.TOP);
    mainJsp.add(rightPanel, JSplitPane.BOTTOM);
    mainJsp.setOneTouchExpandable(true);
    mainJsp.setDividerLocation(150);
    mainPanel.add(mainJsp);
    contentPane.add(mainPanel);

    leftPanel = new PaintPanel();
    // Just left hanging here, never added to anything...?

}

So, if I change it to something like...

private void initComponents(Container contentPane) {
    mainPanel = new JPanel();
    leftPanel = new PaintPanel();
    rightPanel = new JPanel();
    leftPanel.add(new JLabel("left"));
    rightPanel.add(new JLabel("right"));
    leftPanel.setPreferredSize(new Dimension(200, 40));
    rightPanel.setPreferredSize(new Dimension(280, 400));
    leftPanel.setBackground(Color.WHITE);
    rightPanel.setBackground(Color.WHITE);

    JSplitPane mainJsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    mainJsp.add(leftPanel, JSplitPane.TOP);
    mainJsp.add(rightPanel, JSplitPane.BOTTOM);
    mainJsp.setOneTouchExpandable(true);
    mainJsp.setDividerLocation(150);
    mainPanel.add(mainJsp);
    contentPane.add(mainPanel);

    //leftPanel = new PaintPanel();

}

It now displays...

enter image description here

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366