Reputation: 1180
I am a beginner in Java GUI.
I am trying to build user interface with JList where the user select an item from already defined list, and a panel related to that particular item appears on the right side of the list. That is my goal. Though, what i want to achieve first is to be able to display certain panel when certain menu item is selected of which i find it difficult due my . . .
This is what i have done so far . . .
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
public class MainGUI extends JFrame{
JList list = new JList(
new String[]{"Create Account","Borrow Book","Return Book",
"Add Book","Delete Book","Display Details"}
);
public MainGUI()
{
JPanel panel = new JPanel();
JPanel panel1 = new JPanel();
list.setForeground(Color.RED);
list.setBackground(Color.WHITE);
list.setSelectionForeground(Color.GREEN);
list.setSelectionBackground(Color.LIGHT_GRAY);
list.setFixedCellWidth(150);
list.setFixedCellHeight(50);
list.setFont(new Font("Serif",Font.BOLD,16));
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
panel.add(list);
add(panel,BorderLayout.WEST);
}
public static void main(String[] args) {
MainGUI frame = new MainGUI();
frame.setSize(500, 350);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Upvotes: 0
Views: 1431
Reputation: 1819
What you're looking for is called a "Card Layout." A card layout is like a deck of cards where you can swap the top user facing card with another card in the deck. Each card in the deck would be another JPanel containing your various GUIs. You will need a panel for creating accounts, borrowing a book, returning a book, etc. The Java tutorial on Oracle's website walks you through the process fairly well but they use a Combobox instead of a list like you.
http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html
In your case, you probably want to put the card layout in your Border Layout's center panel. Then use your list as the trigger to change which card is displayed. If you need help with how to detect when a user selects something in a list see the JList tutorial from Oracle.
http://docs.oracle.com/javase/tutorial/uiswing/components/list.html
Upvotes: 2