Reputation: 23
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
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
Upvotes: 1
Reputation: 285403
Suggestions:
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...
Edit 2
Regarding your latest code post:
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