0

i have a question regarding a program i am working on. It`s a database manager for a MqSQL db , written in Java. So i have the following program structure.

So i have a main class that extends JFrame, whichh is the main frame of the interface, like this (removed the unecessary code not relevant to the discussion) :

public class MainInterface extends JFrame {

   public MainInterface {
       ................
    MainInterface.setLayout(new FlowLayout());
    MainInterface.setVisible(true);

    TopToolbar toolbar;
    try {
         toolbar = new TopToolbar();
         MainInterface.add(toolbar);
         ResultsPanel Results = new ResultsPanel();
         MainInterface.add(Results);

    } catch (IOException e) {
        e.printStackTrace();
    }
  }

TopToolbar and ResultsPanel are 2 other classes that extend JPanel, the TopToolbar class having a JToolBar with buttonsadded to it (Move Forward, MoveBack, Add entry)

 public class TopToolbar extends JPanel implements ActionListener {  

 TopToolBar()
 {
  //constructor in which i was adding button to the toolbar, not relevat
 }

 } 

 public void actionPerformed(ActionEvent e) {

     String cmd = e.getActionCommand();

     if (MoveFirst.equals(cmd)){
         try {
            DatabaseAccess disp = new DatabaseAccess();
            disp.getResults();
            int id = disp.return_id();

            System.out.println(id);

        } catch (//exceptions) {

            e1.printStackTrace();
        }
     }

That is the ActionListener event for the next button, which should trigger reading the next entry in the db

DatabaseAccess is another class with initializes the DB connection , and has these 2 methods :

public void getResults() throws SQLException {

    Connection con = (Connection) DriverManager.getConnection(URL, user, "")
    sql = (Statement) con.createStatement();
    result_set = sql.executeQuery("select * from persons");

    while (result_set.next()) {

    id = result_set.getInt(1);
    name = result_set.getString(2);

    }       

}

public int return_id(){
    return id;

}

The return_ID method returns (and it does work) the ID (first key in the database, will obviously add methods for the rest of the entries in the db). Now i want to show the ID in the final JPanel, the one called ResultsSet (just 3 JLabels and 3 TextFields for the ID , Name etc., in a GridLayout).

Since the dababase class creation (and subsequently the methods forr reading the db and returning the result) is done inside the ActionPerformed method inside the TopToolBar Jpanel, i can`t access it from the MainInterface JFrame and then use something like

  ResultsPanel DispResults = new ResultsPanel();
  add(DispResults);
  DispResults.setID(id_Value)

where setID would be a method in the ResultsPanel that uses the JTextBox setText to set the text .

Hoope i`ve managed to explain my issue as clear as i can.

4
  • Variable names should NOT start with an upper case character. Some of your variables are correct and some are not. Be consistent if you expect people to take the time and read your code! Commented Aug 28, 2015 at 20:58
  • Whoops, didn't know that. I'm still learning. I`ll correct them ASAP. Thanks for the tip. Commented Aug 28, 2015 at 21:00
  • As duffymo has comment, you should avoid extending from JFrame, apart from locking your into a single use case, making it impossible to re-use you UI in other ways, you're not adding any new functionality to the frame. Have a look at 5 Reasons to Use Composition over Inheritance in Java and OOP as s starting point for more details Commented Aug 29, 2015 at 1:16
  • "i have a question" Great! What is your question? If there is an actual question buried among those words, can you add a '?' to the end of it? If not, please think of an actual question (not a requirement) edit it into the question, and be sure to add the '?'. Commented Aug 29, 2015 at 3:40

1 Answer 1

3

I disagree with several of your choices.

You should not extend JFrame. Create a JPanel and give it to a JFrame to display.

I would dissociate the database interactions from the Swing UI. Create them as interface-based POJOs on their own, without a UI. Get them tested, written, and working perfectly.

Once the database interactions are perfect, give an instance of that object to the class that controls the application. Give it a text-only interface for driving the database actions.

Once the controller is obeying every text command perfectly, using your fully tested database component, then have it create an instance of your JPanel and give it to a JFrame. It will make the same calls to its controller owner that your text UI has already executed successfully.

Computer science is all about decomposition. You solve big problems by breaking them into smaller ones.

If you'd like to see a great example of what your app might look like, download SQL Squirrel.

Sign up to request clarification or add additional context in comments.

10 Comments

Not sure how, except to point out that there's no way the subclass overrides or extends the existing behavior. It's knowing that inheritance isn't always necessary - composition is often a better option. it is in this case.
Much more than opinion, but I don't care whether you or the OP accept it or not. I don't have to prove that decomposition is a good way to solve problems.
Please, offer us your proof so we can see how it's done. Or do you just comment without ever putting yourself out there?
I'm not sure what proof you're asking for? You've made a bold statement about not doing something, something I agree with and as an experience developer, understand, but the OP doesn't have such benefit. You've mentioned composition over inheritance, which is an absolutely awesome point, that alone would be enough to add weight to your comment, which would further support your suggestion of de-coupling the code. if you don't want to, then don't, I just thought it would make a good answer into a great answer, that's all
FYI: I removed them because they were adding noise and I didn't want to clutter the comments as it was obvious your weren't interest in some friendly feedback, again, sorry for bothering you
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.