I have two JPanel
components in a JFrame
. the first panel (firstP
) contains a JList
which loads message headers from news servers. In the second panel (secondP
) I would like to load the body of the messages.
When I select an item from list it opens a news panel where the body of the message is loaded (this is how it works now) but I don't like this.
I would like to load the body if message in the secondP
when select the header from the firstP
(in the same frame).
How it work now:
How I would like to work:
Here is the code for the two panels
public GroupViewer(NewsGroupList groupl, NewsGroup group)
{
super(group.name());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
Close();
}
});
JMenu M1;
JMenuBar Bar;
GroupList=groupl;
this.group=group;
JPanel container = new JPanel();
JPanel mainP=new JPanel();
JPanel secondPanel= new JPanel();
container.setLayout(new GridLayout(1,2));
GridBagLayout grid=new GridBagLayout();
GridBagConstraints c=new GridBagConstraints();
secondPanel.setLayout(grid);
c.weightx=0;
c.weighty=0;
c.gridwidth=1;
c.anchor=GridBagConstraints.EAST;
c.fill=GridBagConstraints.NONE;
JLabel lbl=new JLabel("Subject :");
grid.setConstraints(lbl, c);
secondPanel.add(lbl);
c.gridwidth=GridBagConstraints.REMAINDER;
c.anchor=GridBagConstraints.WEST;
c.fill=GridBagConstraints.HORIZONTAL;
Subject=new JTextField("");
grid.setConstraints(Subject, c);
secondPanel.add(Subject);
if (Type==VIEW)
Subject.setEditable(false);
c.gridwidth=1;
c.anchor=GridBagConstraints.EAST;
c.fill=GridBagConstraints.NONE;
JLabel lbl1=new JLabel("Newsgroups :");
grid.setConstraints(lbl1, c);
secondPanel.add(lbl1);
c.gridwidth=GridBagConstraints.REMAINDER;
c.anchor=GridBagConstraints.WEST;
c.fill=GridBagConstraints.HORIZONTAL;
if (((Type&NEWS)!=0)||((Type&VIEW)!=0))
NewsGroups=new JTextField(group.name());
else
NewsGroups=new JTextField("");
if (Type==VIEW)
NewsGroups.setEditable(false);
grid.setConstraints(NewsGroups, c);
secondPanel.add(NewsGroups);
c.gridwidth=1;
c.anchor=GridBagConstraints.EAST;
c.fill=GridBagConstraints.NONE;
JLabel lbl2=new JLabel("To :");
grid.setConstraints(lbl2, c);
secondPanel.add(lbl2);
c.gridwidth=GridBagConstraints.REMAINDER;
c.anchor=GridBagConstraints.WEST;
c.fill=GridBagConstraints.HORIZONTAL;
To=new JTextField("");
grid.setConstraints(To, c);
secondPanel.add(To);
if (Type==VIEW)
To.setEditable(false);
if (Type==VIEW)
{
c.gridwidth=1;
c.anchor=GridBagConstraints.EAST;
c.fill=GridBagConstraints.NONE;
JLabel lbl3=new JLabel("Date :");
grid.setConstraints(lbl3, c);
secondPanel.add(lbl3);
c.gridwidth=GridBagConstraints.REMAINDER;
c.anchor=GridBagConstraints.WEST;
c.fill=GridBagConstraints.HORIZONTAL;
Date=new JTextField(formatDate(ref.getDate()));
Date.setEditable(false);
grid.setConstraints(Date, c);
secondPanel.add(Date);
}
c.weightx=1;
c.weighty=1;
c.gridwidth=GridBagConstraints.REMAINDER;
c.fill=GridBagConstraints.BOTH;
Body=new JTextArea("", 40, 80);
grid.setConstraints(Body, c);
secondPanel.add(Body);
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
JScrollPane scrollPane = new JScrollPane(Body);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
grid.setConstraints(scrollPane, c);
secondPanel.add(scrollPane);
if (Type==VIEW)
Body.setEditable(false);
if (((Type&REPLY)!=0)||((Type&VIEW)!=0))
{
if ((Type&REPLY)!=0)
{
int pos=-1, pos1;
boolean last=false;
String OrigBody;
try
{
OrigBody=RefArticle.getBody(Group.getServer());
do
{
pos1=OrigBody.indexOf('\n', pos+1);
if (pos1==-1)
{
last=true;
pos1=OrigBody.length()-1;
}
Body.append(">"+OrigBody.substring(pos+1, pos1+1));
pos=pos1;
}
while (!last);
}
catch (Exception ex)
{
System.out.println("Unable to get body for this article\n"+ ex.getMessage());
}
}
else
{
try
{
Body.setText(RefArticle.getBody(Group.getServer()));
}
catch (Exception ex)
{
System.out.println("Unable to get body for this article\n"+ ex.getMessage());
}
}
String subject=RefArticle.getHeaderField("Subject");
if (subject.toLowerCase().startsWith("re: ")||((Type&VIEW)!=0))
Subject.setText(subject);
else
Subject.setText("Re: "+subject);
if (((Type)!=0)||((Type&VIEW)!=0))
To.setText(RefArticle.getHeaderField("From"));
}
attachments=new JPanel();
attachments.add("Center", new JLabel("No attachments yet ."));
split=new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, secondPanel, attachments);
split.setOneTouchExpandable(true);
getContentPane().add(split);
if (Type==VIEW)
{
HViewer=new HeaderViewer(RefArticle.getHeaderField("Message-ID"));
HViewer.setArticle(RefArticle);
}
split.setDividerLocation(0.9);
listModel=new DefaultListModel();
lst=new JList(listModel);
JScrollPane scroller=new JScrollPane(lst);
lst.addListSelectionListener(this);
mainP.add("Center", scroller);
Bar=new JMenuBar();
M1=new JMenu("Group");
I_Refresh=new JMenuItem("View");
I_Refresh.addActionListener(this);
M1.add(I_Refresh);
JMenuItem jmi;
M1.addSeparator();
M1.add(jmi=new JMenuItem("Close"));
jmi.addActionListener(this);
Bar.add(M1);
Bar.add(M1);
setJMenuBar(Bar);
getContentPane().add("Center", container);
container.add(mainP);
container.add(secondPanel);
pack();
show();
}
And here the method which I use to read the body of the messages when I select an item from list. The problem is that it opens a new window with the information and I don't want a new window.
I don't know how to modify the Article Composer constructor to not open a new window. (I want a G.U.I something like Mozilla Thunderbird, in the left part of the JFrame
select the header of message and it opens the body for reading on the right side of the frame without opening a new window)
public void valueChanged(ListSelectionEvent e)
{
int index=((JList)e.getSource()).getSelectedIndex();
if (index != -1)
{
CurrentArticle=(NewsArticle)lst.getSelectedValue();
group.setArticleRead(CurrentArticle);
if (CurrentArticle != null)
{
if (viewer==null)
{
***secondPanel.add(new ArticleComposer(GroupList, group, CurrentArticle, ArticleComposer.VIEW));***
}
else
{
viewer.setArticle(CurrentArticle);
if (viewer.isShowing()==false)
viewer.show();
}
}
}
else
{
CurrentArticle=null;
if (viewer!=null)
viewer.clearText();
}
}