Question

First of all, I'm not looking for a solution, I'm asking what is the most acceptable way for doing this. Now I have a main frame which has a drawing JPanel and a couple JScrollPane just for logging things. I have put all log related views in a class and added to the main frame like this:

logViews = new LogViews(this);

I think I should not add the whole class to the object, but how else can I make it work? Should I extend the main frame or make an interface? The log views need to communicate with the main panel too, so it must be connected both ways.

Ok so if its not clear enough:

LogViews is a custom class I made, it looks something like this, but with more JLists:

public class LogViews {
        private DefaultListModel logModel;
        private JList<String> logList;

        public LogViews(MainFrame frame){
            logModel = new DefaultListModel();
            logList = new JList();
            logList.setModel(logModel);
            JScrollPane logListScroll = new JScrollPane(logList);
            frame.add(logListScroll, BorderLayout.NORTH);
        }
}

It works, I just wonder if I can do it without adding the MainFrame in the constructor.

Was it helpful?

Solution

You can make LogViews a JDialog something like this

public class LogViews extends JDialog {
    private DefaultListModel logModel;
    private JList<String> logList;

    public LogViews(JFrame frame, boolean modal){
        super(frame, true);
        logModel = new DefaultListModel();
        logList = new JList();
        logList.setModel(logModel);
        JScrollPane logListScroll = new JScrollPane(logList);

        setLayout(new BorderLayout());
        add(logListScroll);
        pack();
    }
}

You can make it open with the click of a button

button.addActionListener(new ActionPerformed(ActionEvent e){
    @Override
    public void actionPerformed(ActionEvent e) {
        new LogViews(MainFrame.this, true);
    }
}

What you can do to allow it to "communicate" with the MainFrame is just to have it as an inner class so it has access to all the class members of the MainFrame. Other than that, it's hard to offer a complete solution, because have provided much as to what needs to be communicated.


If you want it to appear in the MainFrame, you can just LogViews a JPanel and add that panel to the MainFrame. Again, having it as an inner class of MainFrame so it can access all the fields of MainFrame


EDIT

What you can also do, if you want is as a separate class, is have a setter. Like

public void setLogModel(DefaultListModel model) {
    logList.setModel(model);
}

And/Or a couple methods to add and remove from the list

public void addToList(String element) {
    logModel.addElement(element);
}

Then just call those methods from the MainFrame. But for your scenario, I would just make the LogViews a JPanel, instead of the JDialog, and instantiate it in the MainClass and add it to your MainFrame. Something like

public class LogViews extends JPanel
    private DefaultListModel logModel;
    private JList<String> logList;

    public LogViews(){
        logModel = new DefaultListModel();
        logList = new JList();
        logList.setModel(logModel);
        JScrollPane logListScroll = new JScrollPane(logList);

        setLayout(new BorderLayout());
        add(logListScroll);
    }

    public void addToList(String element) {
        logModel.addElement(element);
    }

    public void setLogModel(DefaultListModel model) {
        logList.setModel(model);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top