Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

GUI Programming-Help!

rickysririckysri Posts: 21Member
I have a problem.The JPanel class titled p3 appears on the same line as that of the JPanle named p2...I wanted the JPanel p3 to appear columnwise and likewise for the remaining panels created... I tried the GridLayout class but that wasn't sufficient to move the labels and buttons of JPanel p3 to the next area below JPanel titled p2.....Can someone help me ASAP urgently? I have little experience with GUI programming in Java...

Thanks

Sriram

....Here is the sample code...


// Panel p2 to hold buttons
JPanel p2 = new JPanel(); //create a panel
p2.setLayout(new GridLayout(3,1));
p2.add(new JLabel("Customer "));
p2.setLayout(new FlowLayout());
p2.add(jbtAdd = new JButton("Add"));
p2.add(jbtEdit = new JButton("Edit"));
p2.add(jbtDelete = new JButton("Delete"));
add(p2);

JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(0,1));
p3.add(new JLabel("Account "));
p3.setLayout(new FlowLayout());
p3.add(jbtadd1=new JButton("Add Account"));
p3.add(jbtdel1=new JButton("Delete Account"));
add(p3);

JPanel p4 = new JPanel();
p4.setLayout(new GridLayout(3,1));
p4.add(new JLabel("Transaction "));
p4.setLayout(new FlowLayout());
p4.add(jbtdeposit = new JButton("Deposit"));
p4.add(jbtWithdrawal = new JButton("Withdrawal"));
p4.add(jbttransfer = new JButton("Transfer"));
add(p4);

//create a panel for radio buttons
JPanel jreport = new JPanel();
jreport.setLayout(new GridLayout(3,1));


jreport.add(new JLabel("Report"));
//create radio buttons
jrCustomer= new JRadioButton("Customer Profile");
jrAccount = new JRadioButton("Account Activity");
jrSummary = new JRadioButton("Account Summary");



//create radio button group and add radio buttons to it
bgreport = new ButtonGroup();
bgreport.add(jrCustomer);
bgreport.add(jrAccount);
bgreport.add(jrSummary);


// //add buttons to panel - note that button group is not added
jreport.add(jrCustomer);
jreport.add(jrAccount);
jreport.add(jrSummary);

Comments

  • zibadianzibadian Posts: 6,349Member
    : I have a problem.The JPanel class titled p3 appears on the same line
    : as that of the JPanle named p2...I wanted the JPanel p3 to appear
    : columnwise and likewise for the remaining panels created... I tried
    : the GridLayout class but that wasn't sufficient to move the labels
    : and buttons of JPanel p3 to the next area below JPanel titled
    : p2.....Can someone help me ASAP urgently? I have little experience
    : with GUI programming in Java...
    :
    : Thanks
    :
    : Sriram
    :
    : ....Here is the sample code...
    :
    :
    : // Panel p2 to hold buttons
    : JPanel p2 = new JPanel(); //create a panel
    : p2.setLayout(new GridLayout(3,1));
    : p2.add(new JLabel("Customer "));
    : p2.setLayout(new FlowLayout());
    : p2.add(jbtAdd = new JButton("Add"));
    : p2.add(jbtEdit = new JButton("Edit"));
    : p2.add(jbtDelete = new JButton("Delete"));
    : add(p2);
    :
    : JPanel p3 = new JPanel();
    : p3.setLayout(new GridLayout(0,1));
    : p3.add(new JLabel("Account "));
    : p3.setLayout(new FlowLayout());
    : p3.add(jbtadd1=new JButton("Add Account"));
    : p3.add(jbtdel1=new JButton("Delete Account"));
    : add(p3);
    :
    : JPanel p4 = new JPanel();
    : p4.setLayout(new GridLayout(3,1));
    : p4.add(new JLabel("Transaction "));
    : p4.setLayout(new FlowLayout());
    : p4.add(jbtdeposit = new JButton("Deposit"));
    : p4.add(jbtWithdrawal = new JButton("Withdrawal"));
    : p4.add(jbttransfer = new JButton("Transfer"));
    : add(p4);
    :
    : //create a panel for radio buttons
    : JPanel jreport = new JPanel();
    : jreport.setLayout(new GridLayout(3,1));
    :
    :
    : jreport.add(new JLabel("Report"));
    : //create radio buttons
    : jrCustomer= new JRadioButton("Customer Profile");
    : jrAccount = new JRadioButton("Account Activity");
    : jrSummary = new JRadioButton("Account Summary");
    :
    :
    :
    : //create radio button group and add radio buttons to it
    : bgreport = new ButtonGroup();
    : bgreport.add(jrCustomer);
    : bgreport.add(jrAccount);
    : bgreport.add(jrSummary);
    :
    :
    : // //add buttons to panel - note that button group is not added
    : jreport.add(jrCustomer);
    : jreport.add(jrAccount);
    : jreport.add(jrSummary);
    :
    Changing the layout while you're adding new objects to the GUI is useless, because the layout is determined if the window is first shown or is resized. Since the program doesn't remember the previous layouts it always uses the last layout.
    I was also unhappyt about the standard layout managers, so I looked for other ones. I myself now use the RiverLayout. This allows you to use html codes to handle line breaks, filling, etc. Just google for it and you'll find it and its documentation.
  • rickysririckysri Posts: 21Member
    Hey Zibadian

    Would you mind giving an example of the Riverlayout as the documentation looks confusing.... A clear example always helps me while writing Java Programs.....Can you please help me in this?

    Thanks

    Sriram

    : : I have a problem.The JPanel class titled p3 appears on the same line
    : : as that of the JPanle named p2...I wanted the JPanel p3 to appear
    : : columnwise and likewise for the remaining panels created... I tried
    : : the GridLayout class but that wasn't sufficient to move the labels
    : : and buttons of JPanel p3 to the next area below JPanel titled
    : : p2.....Can someone help me ASAP urgently? I have little experience
    : : with GUI programming in Java...
    : :
    : : Thanks
    : :
    : : Sriram
    : :
    : : ....Here is the sample code...
    : :
    : :
    : : // Panel p2 to hold buttons
    : : JPanel p2 = new JPanel(); //create a panel
    : : p2.setLayout(new GridLayout(3,1));
    : : p2.add(new JLabel("Customer "));
    : : p2.setLayout(new FlowLayout());
    : : p2.add(jbtAdd = new JButton("Add"));
    : : p2.add(jbtEdit = new JButton("Edit"));
    : : p2.add(jbtDelete = new JButton("Delete"));
    : : add(p2);
    : :
    : : JPanel p3 = new JPanel();
    : : p3.setLayout(new GridLayout(0,1));
    : : p3.add(new JLabel("Account "));
    : : p3.setLayout(new FlowLayout());
    : : p3.add(jbtadd1=new JButton("Add Account"));
    : : p3.add(jbtdel1=new JButton("Delete Account"));
    : : add(p3);
    : :
    : : JPanel p4 = new JPanel();
    : : p4.setLayout(new GridLayout(3,1));
    : : p4.add(new JLabel("Transaction "));
    : : p4.setLayout(new FlowLayout());
    : : p4.add(jbtdeposit = new JButton("Deposit"));
    : : p4.add(jbtWithdrawal = new JButton("Withdrawal"));
    : : p4.add(jbttransfer = new JButton("Transfer"));
    : : add(p4);
    : :
    : : //create a panel for radio buttons
    : : JPanel jreport = new JPanel();
    : : jreport.setLayout(new GridLayout(3,1));
    : :
    : :
    : : jreport.add(new JLabel("Report"));
    : : //create radio buttons
    : : jrCustomer= new JRadioButton("Customer Profile");
    : : jrAccount = new JRadioButton("Account Activity");
    : : jrSummary = new JRadioButton("Account Summary");
    : :
    : :
    : :
    : : //create radio button group and add radio buttons to it
    : : bgreport = new ButtonGroup();
    : : bgreport.add(jrCustomer);
    : : bgreport.add(jrAccount);
    : : bgreport.add(jrSummary);
    : :
    : :
    : : // //add buttons to panel - note that button group is not added
    : : jreport.add(jrCustomer);
    : : jreport.add(jrAccount);
    : : jreport.add(jrSummary);
    : :
    : Changing the layout while you're adding new objects to the GUI is
    : useless, because the layout is determined if the window is first
    : shown or is resized. Since the program doesn't remember the previous
    : layouts it always uses the last layout.
    : I was also unhappyt about the standard layout managers, so I looked
    : for other ones. I myself now use the RiverLayout. This allows you to
    : use html codes to handle line breaks, filling, etc. Just google for
    : it and you'll find it and its documentation.

  • zibadianzibadian Posts: 6,349Member
    : Hey Zibadian
    :
    : Would you mind giving an example of the Riverlayout as the
    : documentation looks confusing.... A clear example always helps me
    : while writing Java Programs.....Can you please help me in this?
    :
    : Thanks
    :
    : Sriram
    :
    : : : I have a problem.The JPanel class titled p3 appears on the same line
    : : : as that of the JPanle named p2...I wanted the JPanel p3 to appear
    : : : columnwise and likewise for the remaining panels created... I tried
    : : : the GridLayout class but that wasn't sufficient to move the labels
    : : : and buttons of JPanel p3 to the next area below JPanel titled
    : : : p2.....Can someone help me ASAP urgently? I have little experience
    : : : with GUI programming in Java...
    : : :
    : : : Thanks
    : : :
    : : : Sriram
    : : :
    : : : ....Here is the sample code...
    : : :
    : : :
    : : : // Panel p2 to hold buttons
    : : : JPanel p2 = new JPanel(); //create a panel
    : : : p2.setLayout(new GridLayout(3,1));
    : : : p2.add(new JLabel("Customer "));
    : : : p2.setLayout(new FlowLayout());
    : : : p2.add(jbtAdd = new JButton("Add"));
    : : : p2.add(jbtEdit = new JButton("Edit"));
    : : : p2.add(jbtDelete = new JButton("Delete"));
    : : : add(p2);
    : : :
    : : : JPanel p3 = new JPanel();
    : : : p3.setLayout(new GridLayout(0,1));
    : : : p3.add(new JLabel("Account "));
    : : : p3.setLayout(new FlowLayout());
    : : : p3.add(jbtadd1=new JButton("Add Account"));
    : : : p3.add(jbtdel1=new JButton("Delete Account"));
    : : : add(p3);
    : : :
    : : : JPanel p4 = new JPanel();
    : : : p4.setLayout(new GridLayout(3,1));
    : : : p4.add(new JLabel("Transaction "));
    : : : p4.setLayout(new FlowLayout());
    : : : p4.add(jbtdeposit = new JButton("Deposit"));
    : : : p4.add(jbtWithdrawal = new JButton("Withdrawal"));
    : : : p4.add(jbttransfer = new JButton("Transfer"));
    : : : add(p4);
    : : :
    : : : //create a panel for radio buttons
    : : : JPanel jreport = new JPanel();
    : : : jreport.setLayout(new GridLayout(3,1));
    : : :
    : : :
    : : : jreport.add(new JLabel("Report"));
    : : : //create radio buttons
    : : : jrCustomer= new JRadioButton("Customer Profile");
    : : : jrAccount = new JRadioButton("Account Activity");
    : : : jrSummary = new JRadioButton("Account Summary");
    : : :
    : : :
    : : :
    : : : //create radio button group and add radio buttons to it
    : : : bgreport = new ButtonGroup();
    : : : bgreport.add(jrCustomer);
    : : : bgreport.add(jrAccount);
    : : : bgreport.add(jrSummary);
    : : :
    : : :
    : : : // //add buttons to panel - note that button group is not added
    : : : jreport.add(jrCustomer);
    : : : jreport.add(jrAccount);
    : : : jreport.add(jrSummary);
    : : :
    : : Changing the layout while you're adding new objects to the GUI is
    : : useless, because the layout is determined if the window is first
    : : shown or is resized. Since the program doesn't remember the previous
    : : layouts it always uses the last layout.
    : : I was also unhappyt about the standard layout managers, so I looked
    : : for other ones. I myself now use the RiverLayout. This allows you to
    : : use html codes to handle line breaks, filling, etc. Just google for
    : : it and you'll find it and its documentation.
    :
    :
    Here's an example of a JLabel left of a row filling JTextField. Then follows a row of 2 centered buttons. I've left out the actual creation of the objects for readability, since those are layout independent.
    [code]
    add(myLabel, "br"); // start a new row
    add(myEdit, "hfill"); // fill the rest of the row
    add(myOK, "br center");
    add(myCancel, "tab center"); // tab gives a space between the objects
    [/code]
    If you resize the window, you'll notice that the label remains the same size, while the edit fills the entire row.
Sign In or Register to comment.