user3105297
user3105297

Reputation: 23

JFrame with a list that gives information

I have a question regarding JFrame with a list. I created a help menu button. Now the thing is:

When clicked a new window pops up as should. In this window I want to have a list with some formulas by name. Once a formula in the list is clicked I want to display in the same screen what the formula stands for.

It has to be something like formulas on the left in a scrollable list and in the same screen on the right in some sort of text box the description of the clicked formula.

Does anyone know how to do this?

menuItem = new JMenuItem("Help");
     menuItem.setMnemonic(KeyEvent.VK_H);
     menuItem.addActionListener(new ActionListener()
     {
             public void actionPerformed(ActionEvent e)
             {  

                 JFrame help = new JFrame("HELP");
                 help.setTitle("Help");
                 help.setSize(400,200);
                 help.setLocationRelativeTo(null);
                 help.setDefaultCloseOperation(help.DISPOSE_ON_CLOSE);
                 help.setVisible(true);
                 help.add(label);            

                 String labels[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "x", "y"};
                 JList list = new JList(labels);
                 JScrollPane scrollPane = new JScrollPane (list);

                 Container contentPane = help.getContentPane();
                 contentPane.add(scrollPane, BorderLayout.CENTER);



             }
     });

Upvotes: 1

Views: 151

Answers (2)

Jens Piegsa
Jens Piegsa

Reputation: 7485

Here comes a basic implementation.

How the second (formula) window may look like:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Window;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class FormulaWindow extends JDialog {

    public FormulaWindow(final Window parent) {
        super(parent, "Formula window", ModalityType.MODELESS);
        final Container cp = getContentPane();
        cp.setLayout(new BorderLayout());
        setSize(500, 400);
        final Map<String, String> formulaDescritions = new HashMap<>();
        formulaDescritions.put("Formula 1", "<html>How the world works.</html>");
        formulaDescritions.put("Formula 2", "<html>How woman work.</html>");
        formulaDescritions.put("Formula 3", "<html>How programming works.</html>");
        final JList<String> formulaList = new JList<String>(new Vector<String>(formulaDescritions.keySet()));
        formulaList.setPreferredSize(new Dimension(100, 100));
        final JLabel descriptionLabel = new JLabel();
        descriptionLabel.setVerticalAlignment(SwingConstants.TOP);
        formulaList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent evt) {
                final JList<String> list = (JList<String>) evt.getSource();
                descriptionLabel.setText(formulaDescritions.get(list.getSelectedValue()));
            }
        });
        cp.add(new JScrollPane(formulaList), BorderLayout.WEST);
        cp.add(new JScrollPane(descriptionLabel), BorderLayout.CENTER);
    }
}

How to open it on selection of a menu item in the main window:

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;

public class MainWindow extends JFrame {

    private class ShowFormulaWindowAction extends AbstractAction {
        public ShowFormulaWindowAction() {
            super("Show formulas...");
        }

        public void actionPerformed(ActionEvent actionEvent) {
            FormulaWindow formulaWindow = new FormulaWindow(MainWindow.this);
            formulaWindow.setVisible(true);
        }
    }

    public MainWindow() {
        super("Main window");
        JMenu fileMenu = new JMenu("Extras");
        fileMenu.add(new JMenuItem(new ShowFormulaWindowAction()));
        JMenuBar menuBar = new JMenuBar();
        menuBar.add(fileMenu);
        setJMenuBar(menuBar);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    public static void main(final String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MainWindow mainWindow = new MainWindow();
                mainWindow.setSize(350, 250);
                mainWindow.setVisible(true);
            }
        });
    }
}

Further notes:

The secondary window should not be a JFrame but rather a JDialog. In that way

  • it does not show another icon on the task bar
  • it can be "owned" by the main window, causing the dialog to be closed automatically, when the owner is closed

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

Suggestions:

  • The help menu that pops up should be a dialog of some sort, perhaps a non-modal JDialog.
  • You should have it use a CardLayout so that it initially shows formula names, perhaps in a JList held by a JScrollPane.
  • When a JList item is selected, swap views via the CardLayout and display the appropriate formula.

Links:

Edit
You state in comment,

It has to be something like i have my formulas on the left with a scrollbar, and in the right side of the screen some sort of textbox with the description of the formula.

Shouldn't this any any other pertinent information and restrictions be part of your original question? If I were you, I'd edit the original post and supply all necessary information so that we can fully understand your problem including pertinent code (best as an sscce). So...

  • Use another layout manager such as a GridLayout or better yet a BorderLayout to allow you to show multiple JPanels in another JPanel. I'll leave it to you to find the links to the Swing layout manager tutorials.
  • You can still use a CardLayout to swap the equations shown on the display JPanel.

Edit 2
Regarding your latest code post:

  • Again, the dialog window should be a JDialog not a JFrame. The window is dependent on the window that is displaying it and so should not be an independent application window such as a JFrame.
  • I'd place the JScrollPane in the BorderLayout.LINE_START position and the CardLayout using equation JPanel in the BorderLayout.CENTER position.
  • You don't have to use a CardLayout. If the equations are nothing but basic text, you can simply change the text of a JLabel.
  • Or if images, then swap the ImageIcon of a JLabel.
  • Or if a lot of text, then text in a non-editable, non-focusable JTextArea

Edit 3

is there a way to set the size to a fixed size of the scrollpane BorderLayout.LINE_START?

Rather than trying to set the size of anything, consider calling some methods on your JList, methods such as, setPrototypeCellValue(E prototypeCellValue) and setVisibleRowCount(int visibleRowCount) which will allow the component to set its own preferredSize based on reasonable data and initial assumptions. Please check the JList API for the details on these and other JList methods.

Upvotes: 5

Related Questions