1

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();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    My "guess" is, you've either extended the second component from a `JFrame` or you are create a new `JFrame` (or other window) – MadProgrammer Apr 04 '16 at 10:35
  • The JPanel that open with informations like Subject, Date, Body is loaded from separate class, i call the constructor for creating...then i modified and i don't want to open a new window when i select an element from the JList (i want to load information in the JPanel beside the JList). I've created the JPanel but I don't know how to fill it with information when I select an element from the JList without opening a new window. – Flaviu Bodea Apr 04 '16 at 10:44
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) for many alternatives to opening another top level container (such as another `JFrame`). The details of how that would be implemented in *this* case would depend on which alternate approach you think is best for this app. – Andrew Thompson Apr 04 '16 at 11:15
  • `pack();` My spider sense (and common sense) tells me this code is in a class that extends a top level container such as a `JFrame`. That was probably 'prompted' by your IDE, but it is the wrong way to go... – Andrew Thompson Apr 04 '16 at 11:26
  • *"..it opens the body for reading on the right side of the frame **without opening a new window**)"* Look, I could code something up using .. a `Font` list with details on the RHS when selected, but the core of the problem here is that the code was made using a powerful IDE which makes bad suggestions (e.g. to extend a frame or a panel to use the D'n'D GUI designer). As soon as you are ready to abandon that, we can approach a solution to this using ..Java code. – Andrew Thompson Apr 04 '16 at 11:31

1 Answers1

0

Here is the code for the two JPanels

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();
}
Laur Ivan
  • 4,117
  • 3
  • 38
  • 62