Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30
  1. #21
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Can't change the background of my content pane

    It is a zip file though… I tried again, tell me if it works

    Attachment 2252

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

    Default Re: Can't change the background of my content pane

    I think you may have some misconceptions about what inheritance is for and how it works, and that this is causing your problem. Your Sliders class extends GUI when it shouldn't. Instead it should be a fully self-contained class. And since Sliders inherits from GUI, yes, it has a contentPane field, but you must understand that this contentPane has absolutely no relationship to the contentPane object held by the displayed GUI object, because the first one belongs to the Sliders object, not to the displayed GUI object.

    I suggest
    • That you do not have Sliders extend GUI, that all the variables that Sliders uses and that are held by GUI be moved over into the Sliders class.
    • That you give GUI a public void setBackgroundColor(Color c) method, and in this method set the contentPane's background color to the one passed into the method's parameter.
    • That you give Sliders a GUI field, say called gui, but don't initialize it where you declare it (in the class).
    • That you give Sliders a constructor that accepts a GUI parameter
    • That you give construct the Sliders inside of the GUI class and that you pass the reference to the currently visualized GUI object (called "this") into the Sliders constructor
    • That Sliders initializes its GUI field with the GUI object passed into its constructor parameter
    • That you call the GUI's setBackgroundColor(...) method on the GUI field from inside Slider's ActionListener.


    Edit: and in fact, I doubt that you want GUI to extend FileMenu since GUI does not pass the "is-a" test -- GUI isn't in its most basic sense a FileMenu thing in the same way that a dog is-an animal. Make sense?
    Last edited by Fubarable; 12-11-2011 at 05:21 AM.

  3. #23
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Can't change the background of my content pane

    I know what you want me to do(kinda), except i do not know how to do it

  4. #24
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Can't change the background of my content pane

    to be more precise, I kind of know how to create my own methods and such, but I don't know how to access them when my program works, and I don't understand where the information is 'returned to' when you write return, and how to access it.

    like i know if i was to write the "add" part of the calculator, it would be something like
    Java Code:
    public double getFirstValue(double x)
    {
        x = Double.parseDouble(screenLabel.getText());
        screenLabel.setText("0");
        return x;
    }
    then i would somehow get a second user input, and when he presses equal I would add the two.
    I might be totally wrong, as I have never tried it since i don't understand how it works yet.
    If you could give me an example for adding it would be very much appreciated.

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

    Default Re: Can't change the background of my content pane

    Give it a try. It's a worthwhile exercise, trust me, and it will get your program to work eventually.

    Edit: I'm not clear on what you're asking, since I was mainly focusing on your contentPane issue. But the key is to have each section of a program responsible for its own actions, and to try to adhere to MVC or something similar as much as possible where M is your model and is what does the actual calculations, V is your view, is the GUI portion and is what interacts with the user, and C is your control or the actions that underpin your views interactable components such as the action listeners that respond to button pushes.
    Last edited by Fubarable; 12-11-2011 at 04:21 PM.

  6. #26
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Can't change the background of my content pane

    But like how to I tell the program to go to this method?

  7. #27
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Can't change the background of my content pane

    I sent you a code by PM, I am not sure at all if this is even close to what it it supose to do (mostly because it doesn't work)

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

    Default Re: Can't change the background of my content pane

    Here is a simplistic example of what I meant -- of separating the code into model, view and control. There are four classes present,

    JiffiMvc: This essentially is nothing but a main method that creates a Swing thread, creates all three components of our program, a model object, control object, and a view object, and ties them all together with appropriate setter methods, and then displays the view in a JFrame.

    JiffiView: The view or GUI portion of the program. This creates a JPanel and populates it with fields, buttons, and what not, gives the buttons ActionListeners that call methods of the control.

    JiffiModel: the "brains" of the program. This class is responsible for the "logic" of the program and nothing else. It must have no knowledge of the view or gui, and in fact must be usable with all sorts of views. In other words, this part of the program can be the logic for a Swing GUI (as it is here), for a simple console program, for a web program, etc...

    JiffiControl: the class that ties the model and control together. It has model and control fields that allow it to interact with both. It has public methods that are called by the view.

    e.g.,

    JiffiMvc.java
    Java Code:
    import javax.swing.*;
    
    // main class that ties all together
    public class JiffiMvc {
    
       private static void createAndShowGui() {
          // create all components
          JiffiView view = new JiffiView();
          JiffiControl control = new JiffiControl();
          JiffiModel model = new JiffiModel();
          
          // make necessary connections
          view.setControl(control);
          control.setView(view);
          control.setModel(model);
    
          // display it all
          JFrame frame = new JFrame("Jiffi MVC");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(view.getMainPanel()); // we display the view
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }

    JiffiView.java
    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    
    //class responsible for all GUI concerns
    //holds a control field allowing it to 
    //communicate with the control
    public class JiffiView {
       private JTextField entryField = new JTextField(10);
       private JButton addBtn = new JButton("Add");
       private JButton equalBtn = new JButton("Equals");
       private JTextField resultsField = new JTextField(10);
       private JPanel mainPanel = new JPanel();
    
       // the control field. This must be set for this to work
       private JiffiControl control;
    
       public JiffiView() {
          mainPanel.add(entryField);
          mainPanel.add(addBtn);
          mainPanel.add(equalBtn);
          mainPanel.add(new JLabel("Results:"));
          mainPanel.add(resultsField);
    
          // don't allow the user the ability to directly interact with this
          resultsField.setEditable(false);
    
          // give the JButtons ActionListeners that do nothing more than
          // call control methods. Check to make sure that control
          // has been set before trying to call its methods
          addBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                if (control != null) {
                   control.addAction();
                }
             }
          });
          equalBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                if (control != null) {
                   control.equalsAction();
                }
             }
          });
       }
    
       // the main JPanel that holds the GUI
       public JPanel getMainPanel() {
          return mainPanel;
       }
    
       // set control -- to allow control to work
       public void setControl(JiffiControl control) {
          this.control = control;
       }
    
       public String getEntryFieldText() {
          return entryField.getText();
       }
    
       public void clearEntryField() {
          entryField.setText("");
          entryField.requestFocusInWindow();
       }
    
       public void setResultsField(String text) {
          resultsField.setText(text);
       }
    }

    JiffiControl.java
    Java Code:
    //this class ties the view to the model. It has intimate
    //knowledge about both
    public class JiffiControl {
    
       // it connects to the view and the model through
       // these variables which must be set to work.
       private JiffiView view;
       private JiffiModel model;
    
       // we set the connections with these methods
       public void setView(JiffiView view) {
          this.view = view;
       }
    
       public void setModel(JiffiModel model) {
          this.model = model;
       }
    
       // responds to the add button
       public void addAction() {
          if (view != null && model != null) {
    
             try {
                // get String from view
                String text = view.getEntryFieldText();
    
                // change it to a number
                double number = Double.parseDouble(text);
    
                // pass it into the model
                model.addNumber(number);
    
             } catch (NumberFormatException e) {
                // TODO: show joptionpane warning user that text entered
                // is not a valid number
             }
    
             view.clearEntryField();
          }
       }
    
       // responds to the view's equals button
       public void equalsAction() {
    
          // get the total from the model
          double total = model.getTotal();
    
          // display it in the view
          view.setResultsField(String.valueOf(total));
    
          // clear the model so it can start over again
          model.clearTotal();
       }
    
    }

    JiffiModel.java
    Java Code:
    // this class does the math. It has no knowledge about the view whatsoever.
    // If it has knowledge about the control, it is limited.
    public class JiffiModel {
       private double total = 0;
    
       public void addNumber(double number) {
          total += number;
       }
    
       public void clearTotal() {
          total = 0;
       }
    
       public double getTotal() {
          return total;
       }
    
    }
    Last edited by Fubarable; 12-11-2011 at 05:41 PM.

  9. #29
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Can't change the background of my content pane

    Thank you so much for this demonstration.
    This is all new for me, and as it is late where I live I will start analizing it tomorrow, but I get a much better idea of what I'm supposed to do.
    Again, thank you so much for your time and help!

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

    Default Re: Can't change the background of my content pane

    You're welcome. To improve on the above, I'd create model, view and control interfaces which would allow me to more easily change code in one class without having to alter too much code in the other classes. For example if the view implemented a Viewable interface:

    Java Code:
    import javax.swing.JPanel;
    
    public interface Viewable {
    
       public abstract JPanel getMainPanel();
    
       public abstract void setControl(JiffiControl control);
    
       public abstract String getEntryFieldText();
    
       public abstract void clearEntryField();
    
       public abstract void setResultsField(String text);
    
    }
    And the view class itself would be changed like so:
    Java Code:
    public class JiffiView implements Viewable {

    And then have all classes use Viewable in place of JiffiView,

    Java Code:
    //this class ties the view to the model. It has intimate
    //knowledge about both
    public class JiffiControl {
    
       // it connects to the view and the model through
       // these variables which must be set to work.
       private Viewable view;
       private JiffiModel model;
    
       // we set the connections with these methods
       public void setView(Viewable view) {
          this.view = view;
       }
    except for one spot the main class where the actual view object is created:

    Java Code:
       private static void createAndShowGui() {
          // create all components
          Viewable view = new JiffiView2(); // uses interface except here
          JiffiControl control = new JiffiControl();
          JiffiModel model = new JiffiModel();
    This way a new view can be used, and no code in any other class would have to be changed except for the single line above.

    My new View with use of more complex layouts:

    JiffiView2.java
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class JiffiView2 implements Viewable {
       private JTextField entryField = new JTextField(10);
       private JButton addBtn = new JButton("Add");
       private JButton equalBtn = new JButton("Equals");
       private JTextField resultsField = new JTextField(10);
       private JPanel mainPanel = new JPanel();
    
       private JiffiControl control;
    
       public JiffiView2() {
          JPanel centerPanel = new JPanel();
          centerPanel.add(new JLabel("Enter a Number:"));
          centerPanel.add(entryField);
          centerPanel.add(Box.createHorizontalStrut(25));
          centerPanel.add(new JLabel("Results:"));
          centerPanel.add(resultsField);
          
          JPanel btnPanel = new JPanel(new GridLayout(1, 0, 15, 0));
          btnPanel.add(addBtn);
          btnPanel.add(equalBtn);
          
          mainPanel.setLayout(new BorderLayout(5, 5));
          mainPanel.add(centerPanel, BorderLayout.CENTER);
          mainPanel.add(btnPanel, BorderLayout.PAGE_END);
          mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    
          resultsField.setEditable(false);
    
          addBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                if (control != null) {
                   control.addAction();
                }
             }
          });
          equalBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                if (control != null) {
                   control.equalsAction();
                }
             }
          });
       }
    
       @Override
       public JPanel getMainPanel() {
          return mainPanel;
       }
    
       @Override
       public void setControl(JiffiControl control) {
          this.control = control;
       }
    
       @Override
       public String getEntryFieldText() {
          return entryField.getText();
       }
    
       @Override
       public void clearEntryField() {
          entryField.setText("");
          entryField.requestFocusInWindow();
       }
    
       @Override
       public void setResultsField(String text) {
          resultsField.setText(text);
       }
    }

    None of this is absolutely necessary, but it shows how separating concerns can make it much easier to enhance and upgrade sub-portions of the program without having to change other code.
    Last edited by Fubarable; 12-11-2011 at 06:07 PM.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. adding multiple jpanels to content pane
    By tooktook22 in forum AWT / Swing
    Replies: 2
    Last Post: 01-19-2011, 07:40 PM
  2. Centering things a content pane
    By RKhadder in forum New To Java
    Replies: 1
    Last Post: 09-28-2010, 02:43 AM
  3. Update the JFrame after change the Content Pane
    By alisonchan30 in forum AWT / Swing
    Replies: 1
    Last Post: 04-26-2010, 07:22 AM
  4. Changing content pane of a JFrame?
    By dunafrothint in forum AWT / Swing
    Replies: 1
    Last Post: 03-11-2010, 11:21 PM
  5. Replies: 3
    Last Post: 01-06-2008, 12:13 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
  •