PDA

View Full Version : Jframe problem



neo_philiac
04-16-2010, 04:34 AM
Hello all:

I have a little problem. In the code below

1. How do I get the layout right? Right now its equally divided in the frame.

2. Now the Jpanels open and close but how do I get them to close if I open another panel? So, if i have 3 pen and I clicked 1, 3 would close and 1 would open.

A little help please!

Thanks




package desktopapplication2;

import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.image.BufferedImage;
import javax.swing.*;


public class Test {
public static void main(String[] args) {

JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container contentPane = f.getContentPane();
//contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.PAGE_AXIS));
contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.Y_AXIS));


CollapsablePanel cp1 = new CollapsablePanel("test1", buildPanel());
contentPane.add(cp1);

CollapsablePanel cp2 = new CollapsablePanel("test2", buildPanel());
contentPane.add(cp2);

CollapsablePanel cp3 = new CollapsablePanel("test3", buildPanel());
contentPane.add(cp3);

f.setSize(360, 500);
f.setLocation(200, 100);
f.setVisible(true);



}

public static JPanel buildPanel() {
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2, 1, 2, 1);
gbc.weightx = 1.0;
gbc.weighty = 1.0;

JPanel p1 = new JPanel(new GridBagLayout());
gbc.gridwidth = gbc.RELATIVE;
p1.add(new JButton("button 1"), gbc);
gbc.gridwidth = gbc.REMAINDER;
p1.add(new JButton("button 2"), gbc);
gbc.gridwidth = gbc.RELATIVE;
p1.add(new JButton("button 3"), gbc);
gbc.gridwidth = gbc.REMAINDER;
p1.add(new JButton("button 4"), gbc);

return p1;
}
}

class CollapsablePanel extends JPanel {

private boolean selected;
JPanel contentPanel_;
HeaderPanel headerPanel_;

private class HeaderPanel extends JPanel implements MouseListener {
String text_;
Font font;
BufferedImage open, closed;
final int OFFSET = 30, PAD = 5;

public HeaderPanel(String text) {
addMouseListener(this);
text_ = text;
font = new Font("sans-serif", Font.PLAIN, 12);
// setRequestFocusEnabled(true);
setPreferredSize(new Dimension(200, 20));
int w = getWidth();
int h = getHeight();

/*try {
open = ImageIO.read(new File("images/arrow_down_mini.png"));
closed = ImageIO.read(new File("images/arrow_right_mini.png"));
} catch (IOException e) {
e.printStackTrace();
}*/

}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int h = getHeight();
/*if (selected)
g2.drawImage(open, PAD, 0, h, h, this);
else
g2.drawImage(closed, PAD, 0, h, h, this);
*/ // Uncomment once you have your own images
g2.setFont(font);
FontRenderContext frc = g2.getFontRenderContext();
LineMetrics lm = font.getLineMetrics(text_, frc);
float height = lm.getAscent() + lm.getDescent();
float x = OFFSET;
float y = (h + height) / 2 - lm.getDescent();
g2.drawString(text_, x, y);
}

public void mouseClicked(MouseEvent e) {
toggleSelection();
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}

}

public CollapsablePanel(String text, JPanel panel) {
super(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(1, 3, 0, 3);
gbc.weightx = 1.0;
gbc.fill = gbc.HORIZONTAL;
gbc.gridwidth = gbc.REMAINDER;

selected = false;
headerPanel_ = new HeaderPanel(text);

setBackground(new Color(200, 200, 220));
contentPanel_ = panel;

add(headerPanel_, gbc);
add(contentPanel_, gbc);
contentPanel_.setVisible(false);

JLabel padding = new JLabel();
gbc.weighty = 1.0;
add(padding, gbc);

}

public void toggleSelection() {
selected = !selected;

if (contentPanel_.isShowing())
contentPanel_.setVisible(false);
else
contentPanel_.setVisible(true);

validate();

headerPanel_.repaint();
}

}