Results 1 to 3 of 3
  1. #1
    florentp is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default Next/Prev Button, Chapter Buttons

    Hey guys, I'll try to be as clear as possible with what im trying to do, but please forgive me in advance if i dont make it understandable. So here goes, Im trying to make a java "book" like it has buttons that lead to different chapters and possibly a previous and next button. but my problem is that the buttons are at a different panel, called my control panel, and the display is another panel. So my question is how do i get my control panel buttons to affect what is displayed on my display panel? I hope i made that as clear as possible =/:confused:

    Thanks in advance! :D
    -Paul.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Two patterns may help you here. One is the observable pattern which Swing frequently uses in it GUI/model widgets, and the other is the MVC pattern. Wikipedia has articles on both, and both could be applied here to solve your problem.

  3. #3
    florentp is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    err I've been reading the patterns on wikipedia and am ashamed to say that I don't understand it at all =/. I'm a novice java user and when I asked my teacher, all he said to me was "they're all in the same class" or something. Didnt really help as much considering he was the one who assigned it to us. Anywho, maybe if i posted my code it'd be more helpful.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.border.*;
    public class Book extends JFrame
    {
    JFrame frame;
    Toolkit toolkit;
    Dimension screen;
    Container contentPane;
    int screenWidth, screenHeight,
    headerX, headerY, headerWidth, headerHeight,
    displayX, displayY, displayWidth, displayHeight,
    controlX, controlY, controlWidth, controlHeight;


    public static void main (String [] args)
    {
    Book myBook = new Book ();
    }
    public Book ()
    {
    frame = new JFrame ();
    toolkit = frame.getToolkit ();
    screen = toolkit.getScreenSize ();
    screenWidth = screen.width;
    screenHeight = screen.height;

    headerX = (int) (screenWidth * 0.1);
    headerY = (int) (screenHeight * 0.03);
    headerWidth = (int) (screenWidth * 0.8);
    headerHeight = (int) (screenHeight * 0.07);

    displayX = (int) (screenWidth * 0.01);
    displayY = (int) (screenHeight * 0.12);
    displayWidth = (int) (screenWidth * 0.77);
    displayHeight = (int) (screenHeight * 0.77);

    controlX = (int) (screenWidth * 0.79);
    controlY = (int) (screenHeight * 0.12);
    controlWidth = (int) (screenWidth * 0.20);
    controlHeight = (int) (screenHeight * 0.77);


    frame.setTitle ("My Java Book");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    frame.setBounds (0, 0, screenWidth, screenHeight);
    frame.setLayout (null);

    contentPane = frame.getContentPane ();

    HeaderPanel1 h1 = new HeaderPanel1 (headerX, headerY, headerWidth, headerHeight);
    contentPane.add (h1);
    DisplayPanel1 d1 = new DisplayPanel1 (displayX, displayY, displayWidth, displayHeight);
    contentPane.add (d1);
    ControlPanel1 c1 = new ControlPanel1 (controlX, controlY, controlWidth, controlHeight);
    contentPane.add (c1);
    //frame.pack ();
    frame.setVisible (true);
    }
    }


    class HeaderPanel1 extends JPanel
    {
    private Border border;
    public HeaderPanel1 (int x, int y, int width, int height)
    {
    border = BorderFactory.createRaisedBevelBorder ();
    setBorder (border);
    setBounds (x, y, width, height);
    }
    public void paintComponent (Graphics g)
    {
    Font big = (new Font("sansserif", Font.BOLD, 20));
    g.setFont(big);
    g.setColor(Color.RED);
    g.drawString ("History of Basketball", 350, 35);

    }
    }

    class DisplayPanel1 extends JPanel
    {
    private Border border;
    public DisplayPanel1 (int x, int y, int width, int height)
    {
    border = BorderFactory.createRaisedBevelBorder ();
    setBorder (border);
    setBounds (x, y, width, height);
    }
    public void paintComponent (Graphics g)
    {
    g.drawString ("PLAY BALL", 50, 50);
    }

    }

    class ControlPanel1 extends JPanel
    {
    JButton prev,next,chap1,chap2,chap3;
    private Border border;
    public ControlPanel1 (int x, int y, int width, int height)
    {
    border = BorderFactory.createRaisedBevelBorder ();
    setBorder (border);
    setBounds (x, y, width, height);
    setLayout(null);
    Font label = (new Font("sansserif", Font.BOLD+Font.ITALIC, 20));

    prev = new JButton ("Previous");
    prev.setBounds(10,550,120,50);
    prev.setFont(label);
    add(prev);

    next = new JButton ("Next");
    next.setBounds(130,550,120,50);
    next.setFont(label);
    add(next);

    }

    private class PrevListener implements ActionListener{
    public void actionPerformed (ActionEvent e){
    DisplayPanel1.drawString(""); (i guess this is the part where im having difficulties.)
    }
    }
    }

    Thanks in advance for any help!

    -novie java user

Similar Threads

  1. Buttons and TextFields
    By Xystus777 in forum New To Java
    Replies: 27
    Last Post: 03-17-2009, 02:26 AM
  2. Replies: 2
    Last Post: 10-30-2008, 12:48 AM
  3. Applet buttons
    By h3ckf1r3 in forum Java Applets
    Replies: 6
    Last Post: 09-22-2008, 10:15 PM
  4. How to use SWT Buttons
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:44 PM
  5. Next and Previous Buttons
    By JavaNewb in forum New To Java
    Replies: 1
    Last Post: 05-09-2008, 02:23 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
  •