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);
}
}