Results 1 to 8 of 8
  1. #1
    Atomant2013 is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Absolute Beginner trying to get one frame, with 2 panels.

    Hi Guys

    I'm completely new to java but love it so far. But at the moment im trying to do something completely frustrating.

    I would like, one panel split vertically into two frames..... like looking at a book with the seem down the middle.
    Across the top of each panel I would like aset of buttons that when clicked show info on football teams. I would like to use radio buttons, but have the left panel work independantly from the right panel. Ie, so I can select two different teams and do a comparrison.

    So far, I have got only one panel, (with the buttons working), but i gant get the split, or the second panel. Do I need ti include co-ordinates or something?

    I am using netbeans.... my code is below

    (p.s. sorry if I havent used the forum ettiquette but I checked all other threads and none i found helped)

    Thanks

    /** -----------------------------------------------------------------------
    Author : Anthony Barlow
    Date Created : 21 December 2012
    File Name : H2H.java
    Purpose : Assignment 1
    --------------------------------------------------------------------------- */

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

    class H2H extends JFrame implements ItemListener

    {
    JPanel panel = new JPanel();

    String[] styles = { "Alvescot", "Bampton", "Carterton", "Ducklington", "Eynsham", "FC Hollybush" };
    JComboBox box = new JComboBox( styles );
    JRadioButton radioButton1 = new JRadioButton( "Alvescot");
    JRadioButton radioButton2 = new JRadioButton( "Bampton");
    JRadioButton radioButton3 = new JRadioButton( "Carterton");
    JRadioButton radioButton4 = new JRadioButton( "Ducklington");
    JRadioButton radioButton5 = new JRadioButton( "Eynsham");
    JRadioButton radioButton6 = new JRadioButton( "FC Hollybush");
    ButtonGroup team = new ButtonGroup();
    JTextArea txtArea = new JTextArea( 30, 60 );

    JRadioButton radioButton7 = new JRadioButton( "Alvescot");
    JRadioButton radioButton8 = new JRadioButton( "Bampton");
    JRadioButton radioButton9 = new JRadioButton( "Carterton");
    JRadioButton radioButton10 = new JRadioButton( "Ducklington");
    JRadioButton radioButton11 = new JRadioButton( "Eynsham");
    JRadioButton radioButton12 = new JRadioButton( "FC Hollybush");
    ButtonGroup team2 = new ButtonGroup();
    JTextArea txtArea2 = new JTextArea( 30, 60 );
    public H2H()

    {
    super("Input window");
    setSize( 800,600);
    setDefaultCloseOperation( EXIT_ON_CLOSE );
    add(panel);


    team.add( radioButton1 );
    team.add( radioButton2 );
    team.add( radioButton3 );
    team.add( radioButton4 );
    team.add( radioButton5 );
    team.add( radioButton6 );

    panel.add( radioButton1 );
    panel.add( radioButton2 );
    panel.add( radioButton3 );
    panel.add( radioButton4 );
    panel.add( radioButton5 );
    panel.add( radioButton6 );
    panel.add( box );
    panel.add( txtArea );


    radioButton1.addItemListener( this );
    radioButton2.addItemListener( this );
    radioButton3.addItemListener( this );
    radioButton4.addItemListener( this );
    radioButton5.addItemListener( this );
    radioButton6.addItemListener( this );
    box.addItemListener( this );

    setVisible( true);
    }

    @Override

    public void itemStateChanged( ItemEvent event)
    {
    if( event.getItemSelectable()==radioButton1) txtArea.setText( " Alvescot "
    + "\n Home Ground: Farm Park, Carterton Road, Alvescot"
    + "\n Manager: Paul Bircher"
    + "\n Current Record: P7, W5, D0, L2, F23 A7 Pts 15 "
    + "\n Leading Scorer: Chris Lawrence"
    + "\n "
    + "\n Titles: 3"
    + "\n Cups Won: 1"
    + "\n Premier Champions: 1999, 2003"
    + "\n Division One Champions: 1998"
    + "\n Junior Shield Winners: 2004"
    + "\n All Time Top Scorer: Ady Hussey (204)"
    + "\n "
    + "\n NOTES"
    + "\n Manager Paul Bircher has been at the helm for 24 years"
    + "\n Steve Bircher, son of Manager Paul, is hot on the heels of all time goal scorer Ady Hussey with 197 goals");

    if( event.getItemSelectable()==radioButton2) txtArea.setText( "Bampton"
    + "\n Home Ground: Farm Park, Carterton Road, Alvescot"
    + "\n Manager: Paul Bircher"
    + "\n Current Record: P7, W5, D0, L2, F23 A7 Pts 15 "
    + "\n Leading Scorer: Chris Lawrence"
    + "\n "
    + "\n Titles: 3"
    + "\n Cups Won: 1"
    + "\n Premier Champions: 1999, 2003"
    + "\n Division One Champions: 1998"
    + "\n Junior Shield Winners: 2004"
    + "\n All Time Top Scorer: Ady Hussey (204)"
    + "\n "
    + "\n NOTES");
    if( event.getItemSelectable()==radioButton3) txtArea.setText( "Carterton"
    + "\n Home Ground: Farm Park, Carterton Road, Alvescot"
    + "\n Manager: Paul Bircher"
    + "\n Current Record: P7, W5, D0, L2, F23 A7 Pts 15 "
    + "\n Leading Scorer: Chris Lawrence"
    + "\n "
    + "\n Titles: 3"
    + "\n Cups Won: 1"
    + "\n Premier Champions: 1999, 2003"
    + "\n Division One Champions: 1998"
    + "\n Junior Shield Winners: 2004"
    + "\n All Time Top Scorer: Ady Hussey (204)"
    + "\n "
    + "\n NOTES");
    if( event.getItemSelectable()==radioButton4) txtArea.setText( "Ducklington"
    + "\n Home Ground: Farm Park, Carterton Road, Alvescot"
    + "\n Manager: Paul Bircher"
    + "\n Current Record: P7, W5, D0, L2, F23 A7 Pts 15 "
    + "\n Leading Scorer: Chris Lawrence"
    + "\n "
    + "\n Titles: 3"
    + "\n Cups Won: 1"
    + "\n Premier Champions: 1999, 2003"
    + "\n Division One Champions: 1998"
    + "\n Junior Shield Winners: 2004"
    + "\n All Time Top Scorer: Ady Hussey (204)"
    + "\n "
    + "\n NOTES");
    if( event.getItemSelectable()==radioButton5) txtArea.setText( "Eynsham"
    + "\n Home Ground: Farm Park, Carterton Road, Alvescot"
    + "\n Manager: Paul Bircher"
    + "\n Current Record: P7, W5, D0, L2, F23 A7 Pts 15 "
    + "\n Leading Scorer: Chris Lawrence"
    + "\n "
    + "\n Titles: 3"
    + "\n Cups Won: 1"
    + "\n Premier Champions: 1999, 2003"
    + "\n Division One Champions: 1998"
    + "\n Junior Shield Winners: 2004"
    + "\n All Time Top Scorer: Ady Hussey (204)"
    + "\n "
    + "\n NOTES");
    if( event.getItemSelectable()==radioButton6) txtArea.setText( "FC Hollybush"
    + "\n Home Ground: Farm Park, Carterton Road, Alvescot"
    + "\n Manager: Paul Bircher"
    + "\n Current Record: P7, W5, D0, L2, F23 A7 Pts 15 "
    + "\n Leading Scorer: Chris Lawrence"
    + "\n "
    + "\n Titles: 3"
    + "\n Cups Won: 1"
    + "\n Premier Champions: 1999, 2003"
    + "\n Division One Champions: 1998"
    + "\n Junior Shield Winners: 2004"
    + "\n All Time Top Scorer: Ady Hussey (204)"
    + "\n "
    + "\n NOTES");








    if( (event.getItemSelectable()== box ) &&
    ( event.getStateChange()==ItemEvent.SELECTED))
    txtArea.append( event.getItem().toString() + " selected");

    }
    public static void main ( String[] args )

    {
    H2H gui = new H2H();
    }

    }

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Absolute Beginner trying to get one frame, with 2 panels.

    Using absolute positioning is not recommended when creating a swing application. You should use the most suited layout manager to layout the components of your application. For this I suggest you to explore the following documents on layout manager: A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Absolute Beginner trying to get one frame, with 2 panels.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Atomant2013 is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Re: Absolute Beginner trying to get one frame, with 2 panels.

    Quote Originally Posted by wsaryada View Post
    Using absolute positioning is not recommended when creating a swing application. You should use the most suited layout manager to layout the components of your application. For this I suggest you to explore the following documents on layout manager: A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    Thats great, thank you. I'll take a look at that now.

  5. #5
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Absolute Beginner trying to get one frame, with 2 panels.

    I suggest you to explore the following documents on layout manager: A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    I suggest that you focus on using a BorderLayout for your JFrame, this will give you 5 areas, NORTH, CENTER, SOUTH, EAST, and WEST. You could put your buttons in a JPanel and put the JPanel in the NORTH. This button panel should probably use a FlowLayout which will just put the buttons where there's room, and will leave their size alone.

    You could have another panel for your two JTextAreas and have this use a GridLayout with 1 row and 2 columns. You could place this JPanel with the text areas in it in the CENTER of the JFrame.

    This is how you use layout managers and panels to have Swing automatically position and size your Components.

  6. #6
    Atomant2013 is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Re: Absolute Beginner trying to get one frame, with 2 panels.

    Thanks guys... this is brilliant.

    I'm ok with the theory... but its the actual CODE which I struggle with. I'm self taught and don't know the rules of java just yet... I'm really enjoying it though

    any help with the code itself would be much appreciatred...

  7. #7
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Absolute Beginner trying to get one frame, with 2 panels.

    any help with the code itself would be much appreciatred...
    In your constructor, for your JFrame, to setup a BorderLayout, you would do:

    setLayout(new BorderLayout());

    Since you want to have two panels that have the same radio buttons in them and they each work the same way, it makes sense to make this a subclass of JPanel. This way, you do the programming only once, and you easily have two instances of the panel by instantiating the class twice and add the panels twice to the JFrame.

    Java Code:
    class TeamsPanel extends JPanel {
    
        public TeamsPanel() {
             setLayout(new GridLayout(1,2));
             add(new TeamPanel());
             add(new TeamPanel());
        }
    
    }
    The above code defines a panel, that will be added to JFrame in Border.CENTER which will in turn have a panel for each Team.

    The TeamPanel declaration would look like this:

    Java Code:
    class TeamPanel extends JPanel implements ItemListener {
    }
    It would define all of the radio buttons or combox box, and it would have a text area in it. And it is where the ItemListener and the itemStateChanged would go. This panel would have a BorderLayout, so that you could put the button panel across the top and put the text area in the center.

    I hope that this helps without doing too much for you.

  8. #8
    Atomant2013 is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Re: Absolute Beginner trying to get one frame, with 2 panels.

    Thank you again... I THINK I follow that. Im guessing you're saying I should have one file for the main program and a smaller sub file for the buttons etc?

    How much more can I push this and ask for help in how this is done... in stupid person terms as I'm not yet offais with all java speak.

    thanks once again..... its already looking a lot better.

    Also, I have another question but I'll save that for now :)

Similar Threads

  1. Frame with 2 Panels
    By collwill in forum New To Java
    Replies: 9
    Last Post: 01-10-2013, 06:49 AM
  2. Help an absolute beginner for free...
    By efth in forum New To Java
    Replies: 6
    Last Post: 05-11-2011, 08:11 AM
  3. Advice for first step for absolute beginner
    By FormerBGIuser in forum New To Java
    Replies: 8
    Last Post: 03-07-2011, 10:54 AM
  4. Absolute beginner
    By duff18 in forum New To Java
    Replies: 15
    Last Post: 02-19-2011, 10:07 AM
  5. Replies: 2
    Last Post: 02-08-2010, 12:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •