I want to dispaly a label which is of Boxlayout, in a frame
of Gridlayout of 6X6 sothat, I can display 36 cells in the frame.
But According to the code below only one cell is getting displayed.
kindly Fix the code. Here is the code.
//package check;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
public class CheckPanel1 extends JFrame
{
public CheckPanel1()
{
super("CheckPanel1");
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
/* create and show the GUI */
new CheckPanel1().createAndShowGui();
}
});
}
private void createAndShowGui()
{
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
CheckPanel1 frame = this;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*
JLabel image = new JLabel(new ImageIcon(frame.getClass().getResource("check1.jpg")), SwingConstants.CENTER);
*/
JLabel image = new JLabel(createImageIcon("/check1.jpg", "Check Image"));
image.setBorder(BorderFactory.createLineBorder(Color.BLACK));
image.setAlignmentX(Component.CENTER_ALIGNMENT);
// Add address lines
JLabel addressLine1 = new JLabel(" Faircloth");
JLabel addressLine2 = new JLabel(" 163 Tamworth Drive");
JLabel addressLine3 = new JLabel(" Denton, NC 27239-7014");
JPanel addressPanel = new JPanel();
addressPanel.setLayout(new GridLayout(3,1,0,0));
addressPanel.setBorder(BorderFactory.createLineBorder(Color.WHITE));
addressPanel.setBackground(Color.WHITE);
addressPanel.add(addressLine1);
addressPanel.add(addressLine2);
addressPanel.add(addressLine3);
addressPanel.setMaximumSize(new Dimension(302, 65));
addressPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
Box mainPanel = Box.createVerticalBox();
mainPanel.add(image);
mainPanel.add(Box.createRigidArea(new Dimension(0, 1)));
mainPanel.add(addressPanel);
// contentPane.add(mainPanel, BorderLayout.CENTER);
// Align the components and display window
/*
// Adjust address panel
Rectangle bounds = addressPanel.getBounds();
addressPanel.setBounds((int) (bounds.getX() + bounds.getWidth()/2 - 50),(int) bounds.getY(), 100,40) ;
*/
Container contentPane = frame.getContentPane();
//Container contentPane = new JPanel();
contentPane.setLayout(new GridLayout(6,6,1,1));
// Ceation Of 36 JPanels
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
JPanel panel6 = new JPanel();
JPanel panel7 = new JPanel();
JPanel panel8 = new JPanel();
JPanel panel9 = new JPanel();
JPanel panel10 = new JPanel();
JPanel panel11 = new JPanel();
JPanel panel12 = new JPanel();
JPanel panel13 = new JPanel();
JPanel panel14 = new JPanel();
JPanel panel15 = new JPanel();
JPanel panel16 = new JPanel();
JPanel panel17 = new JPanel();
JPanel panel18 = new JPanel();
JPanel panel19 = new JPanel();
JPanel panel20 = new JPanel();
JPanel panel21 = new JPanel();
JPanel panel22 = new JPanel();
JPanel panel23 = new JPanel();
JPanel panel24 = new JPanel();
JPanel panel25 = new JPanel();
JPanel panel26 = new JPanel();
JPanel panel27 = new JPanel();
JPanel panel28 = new JPanel();
JPanel panel29 = new JPanel();
JPanel panel30 = new JPanel();
JPanel panel31 = new JPanel();
JPanel panel32 = new JPanel();
JPanel panel33 = new JPanel();
JPanel panel34 = new JPanel();
JPanel panel35 = new JPanel();
JPanel panel36 = new JPanel();
// Adding mainPanel(box) to 36 Panels
panel1.add(mainPanel);
panel2.add(mainPanel);
panel3.add(mainPanel);
panel4.add(mainPanel);
panel5.add(mainPanel);
panel6.add(mainPanel);
panel7.add(mainPanel);
panel8.add(mainPanel);
panel9.add(mainPanel);
panel10.add(mainPanel);
panel11.add(mainPanel);
panel12.add(mainPanel);
panel13.add(mainPanel);
panel14.add(mainPanel);
panel15.add(mainPanel);
panel16.add(mainPanel);
panel17.add(mainPanel);
panel18.add(mainPanel);
panel19.add(mainPanel);
panel20.add(mainPanel);
panel21.add(mainPanel);
panel22.add(mainPanel);
panel23.add(mainPanel);
panel24.add(mainPanel);
panel25.add(mainPanel);
panel26.add(mainPanel);
panel27.add(mainPanel);
panel28.add(mainPanel);
panel29.add(mainPanel);
panel30.add(mainPanel);
panel31.add(mainPanel);
panel32.add(mainPanel);
panel33.add(mainPanel);
panel34.add(mainPanel);
panel35.add(mainPanel);
panel36.add(mainPanel);
JPanel panellayer1 = new JPanel(new GridLayout(1,6,1,1));
JPanel panellayer2 = new JPanel(new GridLayout(1,6,1,1));
JPanel panellayer3 = new JPanel(new GridLayout(1,6,1,1));
JPanel panellayer4 = new JPanel(new GridLayout(1,6,1,1));
JPanel panellayer5 = new JPanel(new GridLayout(1,6,1,1));
JPanel panellayer6 = new JPanel(new GridLayout(1,6,1,1));
panellayer1.add(panel1);
panellayer1.add(panel2);
panellayer1.add(panel3);
panellayer1.add(panel4);
panellayer1.add(panel5);
panellayer1.add(panel6);
panellayer2.add(panel7);
panellayer2.add(panel8);
panellayer2.add(panel9);
panellayer2.add(panel10);
panellayer2.add(panel11);
panellayer2.add(panel12);
panellayer3.add(panel13);
panellayer3.add(panel14);
panellayer3.add(panel15);
panellayer3.add(panel16);
panellayer3.add(panel17);
panellayer3.add(panel18);
panellayer4.add(panel19);
panellayer4.add(panel20);
panellayer4.add(panel21);
panellayer4.add(panel22);
panellayer4.add(panel23);
panellayer4.add(panel24);
panellayer5.add(panel25);
panellayer5.add(panel26);
panellayer5.add(panel27);
panellayer5.add(panel28);
panellayer5.add(panel29);
panellayer5.add(panel30);
panellayer6.add(panel31);
panellayer6.add(panel32);
panellayer6.add(panel33);
panellayer6.add(panel34);
panellayer6.add(panel35);
panellayer6.add(panel36);
contentPane.add(panellayer1);
contentPane.add(panellayer2);
contentPane.add(panellayer3);
contentPane.add(panellayer4);
contentPane.add(panellayer5);
contentPane.add(panellayer6);
frame.setSize(600,200);
frame.setVisible(true);
}
/**
* Returns an ImageIcon, or null if the path was invalid.
*/
protected static ImageIcon createImageIcon(String path,
String description)
{
java.net.URL imgURL = CheckPanel1.class.getResource(path);
if (imgURL != null)
{
return new ImageIcon(imgURL, description);
}
else
{
System.err.println("Couldn't find file: " + path);
return null;
}
}
}
Thanks In advance..
Comments
[code]
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
public class CheckPanel1 extends JFrame {
JLabel image;
Icon icon;
public CheckPanel1() {
super("CheckPanel1");
createAndShowGui();
}
public static void main(String[] args) {
CheckPanel1 frame = new CheckPanel1();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(840,460);
frame.setVisible(true);
}
protected static ImageIcon createImageIcon(String path, String description) {
java.net.URL imgURL = CheckPanel1.class.getResource(path);
if (imgURL != null)
return new ImageIcon(imgURL, description);
else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
private Box createMainPanel() {
JPanel addressPanel = new JPanel();
addressPanel.setLayout(new GridLayout(3,1,0,0));
addressPanel.setBorder(BorderFactory.createLineBorder(Color.WHITE));
addressPanel.setBackground(Color.WHITE);
addressPanel.add(new JLabel(" Faircloth"));
addressPanel.add(new JLabel(" 163 Tamworth Drive"));
addressPanel.add(new JLabel(" Denton, NC 27239-7014"));
addressPanel.setMaximumSize(new Dimension(302, 65));
addressPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
image = new JLabel(icon);
image.setBorder(BorderFactory.createLineBorder(Color.BLACK));
image.setAlignmentX(Component.CENTER_ALIGNMENT);
Box mainPanel = Box.createVerticalBox();
mainPanel.add(image);
mainPanel.add(Box.createRigidArea(new Dimension(0, 1)));
mainPanel.add(addressPanel);
return mainPanel;
}
private void createAndShowGui() {
JFrame.setDefaultLookAndFeelDecorated(true);
icon = createImageIcon("/check1.jpg", "Check Image");
Container c = getContentPane();
c.setLayout(new GridLayout(6,6,1,1));
JPanel p;
for(int i=0; i<36; i++) {
c.add((p = new JPanel()));
p.add(createMainPanel());
}
}
}
[/code]
: [code]
: import javax.swing.*;
: import javax.swing.border.Border;
: import java.awt.*;
:
: public class CheckPanel1 extends JFrame {
: JLabel image;
: Icon icon;
: public CheckPanel1() {
: super("CheckPanel1");
: createAndShowGui();
: }
: public static void main(String[] args) {
: CheckPanel1 frame = new CheckPanel1();
: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
: frame.setSize(840,460);
: frame.setVisible(true);
: }
: protected static ImageIcon createImageIcon(String path, String description) {
: java.net.URL imgURL = CheckPanel1.class.getResource(path);
: if (imgURL != null)
: return new ImageIcon(imgURL, description);
: else {
: System.err.println("Couldn't find file: " + path);
: return null;
: }
: }
: private Box createMainPanel() {
: JPanel addressPanel = new JPanel();
: addressPanel.setLayout(new GridLayout(3,1,0,0));
: addressPanel.setBorder(BorderFactory.createLineBorder(Color.WHITE));
: addressPanel.setBackground(Color.WHITE);
: addressPanel.add(new JLabel(" Faircloth"));
: addressPanel.add(new JLabel(" 163 Tamworth Drive"));
: addressPanel.add(new JLabel(" Denton, NC 27239-7014"));
: addressPanel.setMaximumSize(new Dimension(302, 65));
: addressPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
:
: image = new JLabel(icon);
: image.setBorder(BorderFactory.createLineBorder(Color.BLACK));
: image.setAlignmentX(Component.CENTER_ALIGNMENT);
:
: Box mainPanel = Box.createVerticalBox();
: mainPanel.add(image);
: mainPanel.add(Box.createRigidArea(new Dimension(0, 1)));
: mainPanel.add(addressPanel);
: return mainPanel;
: }
:
: private void createAndShowGui() {
: JFrame.setDefaultLookAndFeelDecorated(true);
: icon = createImageIcon("/check1.jpg", "Check Image");
: Container c = getContentPane();
: c.setLayout(new GridLayout(6,6,1,1));
: JPanel p;
: for(int i=0; i<36; i++) {
: c.add((p = new JPanel()));
: p.add(createMainPanel());
: }
: }
: }
: [/code]
:
Thanks for your correction and restructuring the code in more Object oriented way. Truly my code was more manual.
But Still one more problem is there...That is my mainPanel contains
and Image as well as an address label beneath the Image.
Can you pls fix that too...I apprecetiate your help.
Thanks In Advance.
But do you mean that each mainPanel should have it's own unique icon and address? If so, where does this information come from?
[b][red]This message was edited by lovelyjob at 2006-3-8 12:49:24[/red][/b][hr]
[b][red]This message was edited by lovelyjob at 2006-3-8 12:44:59[/red][/b][hr]
[b][red]This message was edited by lovelyjob at 2006-3-8 12:37:53[/red][/b][hr]
: If the path to the image is correct ("/check1.jpg"), then you should see an icon above the address, at least I do.
: But do you mean that each mainPanel should have it's own unique icon and address? If so, where does this information come from?
Ok. Thanks for the Clue.I will get corrected myself.
I understood you are placing an Icon which hides my address part.
Take Care. Bye.
: