4

I have written a small program, while reading a book about swing, that creates a JSplitPane between two labels. The problem is that the JSplitPane can barely be seen (at least in my operating system - MAC OS Lion) and setting some properties on it (like foreground color) does not seem to work.

Here is the code :

//Demonstrate a simple JSplitPane


package swingexample4_6;

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

public class SplitPaneDemo {

    //constructor
    public SplitPaneDemo()
    {
        //Create a new JFrame container.
        //Use the default border layout
        JFrame jfrm = new JFrame("Split Pane Demo");

        //Give the frame an initial size
        jfrm.setSize(380, 150);

        //Terminate the program when the user closes the application
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //--Make two labels to show the split pane
        JLabel jlab = new JLabel(" Left side: ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        JLabel jlab2 = new JLabel(" Right side: ABCDEFGHIJKLMNOPQRSTUVWXYZ");

        //Set the minimum size for each label
        //This step is not technically needed to use a split pane,
        //but it enables the split pane resizing features to be
        //used to their maximum extent
        jlab.setMinimumSize(new Dimension(90, 30));
        jlab2.setMinimumSize(new Dimension(90, 30));

        //--Create a split pane
        JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, jlab, jlab2);

        //Code to get a list of component names in the console
        Component[] listComponents = jsp.getComponents();

        String theList;
        for (Component myComponent: listComponents)
        {
            theList = myComponent.toString();
            System.out.println(theList);
        }


        //Add the split pane to the content pane
        jfrm.getContentPane().add(jsp);

        //Display the frame
        jfrm.setVisible(true);
    }

    public static void main(String[] args) {
        //Create the frame on the event dispatching thread
        SwingUtilities.invokeLater(new Runnable(){

            @Override
            public void run() {
                new SplitPaneDemo();
            }

        });
    }
}

Is there any way I can change its color , so that it can really stand out? Thank you.

skiabox
  • 3,201
  • 12
  • 59
  • 92

2 Answers2

7

You can use the SplitPane.background property, as shown below.

SplitPane background

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

/** @see http://stackoverflow.com/a/10110232/230513 */
public class SplitPaneDemo {

    //constructor
    public SplitPaneDemo() {
        JFrame jf = new JFrame("Split Pane Demo");
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //--Make two labels to show the split pane
        JPanel left = content("Left side: ");
        JPanel right = content("Right side: ");

        //--Create a split pane
        JSplitPane jsp = new JSplitPane(
            JSplitPane.HORIZONTAL_SPLIT, true, left, right);
        jsp.setDividerLocation(0.5f);

        //Add the split pane to the frame's content pane
        jf.add(jsp);
        jf.pack();

        //Display the frame
        jf.setLocationRelativeTo(null);
        jf.setVisible(true);

        //Code to get a list of component names in the console
        for (Component myComponent : jsp.getComponents()) {
            System.out.println(myComponent);
        }
    }

    private JPanel content(String s) {
        final JLabel label = new JLabel(s + "Some text.", JLabel.CENTER);
        JPanel panel = new JPanel(new GridLayout()) {

            @Override
            public Dimension getPreferredSize() {
                Dimension d = label.getPreferredSize();
                return new Dimension(d.width * 2, d.height * 3);
            }
        };
        panel.setOpaque(true);
        panel.setBackground(new Color(0xffffffc0));
        panel.add(label);
        return panel;
    }

    public static void main(String[] args) {
        UIManager.put("SplitPane.background", new Color(0xff8080ff));
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new SplitPaneDemo();
            }
        });
    }
}
trashgod
  • 196,350
  • 25
  • 213
  • 918
  • 1
    See also [*Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?*](http://stackoverflow.com/q/7229226/230513). – trashgod Apr 11 '12 at 16:42
  • Nice solution this one too.I have one question though: – skiabox Apr 11 '12 at 20:54
  • Why `UIManager.put("SplitPane.foreground", new Color(0xff8080ff));` is not working alone (without changing any background of label or splitpane)? – skiabox Apr 11 '12 at 20:55
  • When I comment out `panel.setBackground()`, the `SplitPane.background` color still shows. Note that `JPanel` is opaque by default, unlike `JLabel`. – trashgod Apr 11 '12 at 21:02
  • 1
    In fact your answer is complementary to the previous answer but I can't accept two answers! :( – skiabox Apr 11 '12 at 21:04
5

JLabel is by default NON_Opaque, simple is transparent, you can

  • change JLabels to the JComponent or JPanel could be better

  • change opacity by JLabel#setOpaque(true)

mKorbel
  • 108,320
  • 17
  • 126
  • 296
  • I'd go with `JLabel.setOpaque(true)` in this case, if only for the fact it is less code line changes from current source to working. Of course, you *also* need to call `JLabel.setBackground(Color)`, which doubles the line count. ;) – Andrew Thompson Apr 11 '12 at 16:37
  • +1 Alternatively, set the container's background color and/or component property, as shown [here](http://stackoverflow.com/a/10110232/230513). – trashgod Apr 11 '12 at 16:47
  • I didn't know that default property setting of JLabel. – skiabox Apr 11 '12 at 20:48