Search code examples
javaswingjsplitpane

Drawing on JSplitPane not showing


Just trying to draw some lines to the screen.

  • I've checked to ensure all the relevant code's being run
  • I've tried calling repaint (and ensuring that's being run)
  • Since this is a JSplitPane, the layout must be the JSplitPane layout
  • I'm setting the color to ensure it isn't drawing using the background color.
  • I've checked the height and width to ensure its size isn't 0 or something
  • I've tried drawing text as well; same result
  • I've changed the coordinates all over the place, tried both arbitrary and proportional values

Or at least I think. Swing is unintuitively quirky. I'd use AWT, but I need the specificity Swing offers. Anyway, the code. It's just a split pane, which is actually displaying - resizable and all - but the contents of the top pane (the only one I've attempted to put anything in) don't show.

package derange;

import java.awt.Dimension;


import java.awt.GridLayout;

import javax.swing.*;        

public class Derange {

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Derange");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Display the window.
        frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        frame.pack();
        frame.setVisible(true);


        //Create a split pane with the two scroll panes in it.
        PanelScore scorePane = new PanelScore();
        JScrollPane instrumentPane = new JScrollPane();
        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                                   scorePane, instrumentPane);
        splitPane.setOneTouchExpandable(true);
        splitPane.setDividerLocation((frame.getHeight() / 4) * 3 );// Three-quarters of the way down
        splitPane.setDividerSize(20);

        //Provide minimum sizes for the two components in the split pane
        Dimension minimumSize = new Dimension(frame.getWidth(), frame.getHeight()/ 2);//width, height
        scorePane.setMinimumSize(minimumSize); //Score takes up at least half the screen
        instrumentPane.setMinimumSize(new Dimension(0,0));//no minimum size on the instrument panel; collapsible

        frame.getContentPane().add(splitPane);
    }


    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

.

package derange;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.JScrollPane;

@SuppressWarnings("serial")//wtf is this needed for?

public class PanelScore extends JScrollPane{

    public int strings = 6;

    public void drawStaffTablature(Graphics g){
        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(Color.black);

        int xStart = 30;//insets.left;
        int xEnd = getParent().getWidth() - 30;
        int yCoord = this.getHeight() / 2;
        System.out.println(this.isShowing());

        //Space between tablature lines
        int lineSpacing = 15;
        //Space between staffs.
        int staffSpacing = 60;`enter code here`
        for(int x = 0; x < strings; x++){
            g2d.drawLine(xStart, yCoord + (lineSpacing * x), xEnd, yCoord + (lineSpacing * x));
            //System.out.println("String: " + (x + 1));
             g.drawString("Test", xStart, yCoord); //change the co-odrinates
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        drawStaffTablature(g);
    }
}

Solution

  • Short answer, don't extend from JScrollPane, a JScrollPane contains single component known as a JViewport, which covers the most of the scroll pane (the rest is taken up by the JScrollBars

    enter image description here

    Instead, try extending from something like JPanel.

    I'd also advise you against using anything like int xEnd = getParent().getWidth() - 30; within your paint code, the Graphics context is translated to the components location, making the top/left corner 0x0 and clipped to the components current width and height