The code below is a simplified excerpt which compiles & runs, just to show the problem I encountered.
I tried to use BoxLayout in the ColorPanel method, but the JLabel "swatch" was not visible in the frame at all. I found that I can get the effect that I wanted by using BorderLayout instead, but I'd still like to know why the "swatch" disappeared in BoxLayout. Any ideas?
(BoxLayout works fine in the ShowColors method. It's only a problem in ColorPanel.)
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import javax.swing.*;
import javax.swing.border.*;
class ViewFrame {
public final Dimension OUTPUT_SZ = new Dimension(800, 600);
public final Dimension PATCH_SZ = new Dimension(100, 100);
public final Dimension WINDOW_SZ = new Dimension(110, 120);
public final Dimension DIALOG_SZ = new Dimension(400, 200);
public final Dimension INPUT_SZ = new Dimension(36, 20);
private void showColors() {
JPanel outputPanel, buttonPanel;
colorDisplayPanel = new JPanel();
outputPanel = new JPanel();
buttonPanel = new JPanel();
JButton exitButton = new JButton("Exit Viewer");
colorDisplayPanel.setLayout(new BoxLayout(colorDisplayPanel,
BoxLayout.Y_AXIS));
outputPanel.setLayout(new FlowLayout());
JPanel colorPanel = ColorPanel( 255, 255,100 );
outputPanel.add(colorPanel);
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
});
buttonPanel.add(exitButton);
colorDisplayPanel.add(outputPanel);
colorDisplayPanel.add(buttonPanel);
colorDisplayPanel.setVisible(true);
frame.add(colorDisplayPanel);
frame.setSize(OUTPUT_SZ);
}
private void createAndShowGUI() {
frame = new JFrame("ViewFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(OUTPUT_SZ);
showColors();
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JPanel ColorPanel( int chan0, int chan1, int chan2 ) {
JPanel window = new JPanel();
// window.setLayout(new BoxLayout(window, BoxLayout.Y_AXIS));
window.setLayout(new BorderLayout());
window.setPreferredSize(WINDOW_SZ);
TitledBorder title;
JLabel swatch = new JLabel();
swatch.setPreferredSize(PATCH_SZ);
swatch.setOpaque(true);
swatch.setBackground( new Color( chan0, chan1, chan2 ));
title = BorderFactory.createTitledBorder( chan0 + ","
+ chan1 + "," + chan2 );
JLabel testlabel = new JLabel("123456789");
Font f = testlabel.getFont();
testlabel.setFont(f.deriveFont(Font.PLAIN, 10.0f));
testlabel.setForeground(Color.white);
testlabel.setBackground(Color.black);
testlabel.setHorizontalAlignment(SwingConstants.CENTER);
window.setBorder(title);
// window.add(testlabel); // comment-out for BorderLayout
// window.add( swatch); // comment-out for BorderLayout
window.add(testlabel, BorderLayout.PAGE_START);
window.add( swatch, BorderLayout.CENTER );
window.setBackground(Color.black);
title.setTitleColor(Color.white);
return window;
}
public static void main(String[] args) {
ViewFrame vf = new ViewFrame();
vf.createAndShowGUI();
}
// member variables
private JPanel colorDisplayPanel;
private JFrame frame;
}
Right now, the code is set up for BorderLayout so you can see how it should look. For BoxLayout, comment out lines 63, 85 and 86 and uncomment lines 62, 83 and 84.