Issue With Java JList - java

I have tried to use a Jlist but I have not been able to get it to show up in my JFrame.
Here is my code:
private static void list(){
JFrame frame = new JFrame();
frame.setTitle("Menu");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLayout(new FlowLayout());
listModel = new DefaultListModel();
listModel.addElement("Add Member");
listModel.addElement("Add Meeting");
listModel.addElement("Record Attendance");
list = new JList(listModel);
list.setVisibleRowCount(3);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
JScrollPane listScrollPane = new JScrollPane(list);
frame.add(listScrollPane, BorderLayout.CENTER);
}
Currently when I run the program the frame will open, but it is blank. Any help?

Make frame.setVisible(true); the last line of the function.

You have set the layout as frame.setLayout(new FlowLayout()); but you have used
frame.add(listScrollPane, BorderLayout.CENTER); try changing the layout to border layout eg: frame.setLayout(new BorderLayout());

Related

Java Swing GUI JLabel Not Showing

I am trying to write a Title for the main menu of my program, by using a JLabel, but it doesn't seem to appear on my screen.
import javax.swing.*;
public class GUI {
public GUI() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
panel.setLayout(new GridLayout());
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Title");
frame.pack();
frame.setSize(854,560);
frame.setVisible(true);
JLabel title = new JLabel();
title.setText("Title");
//title.setSize();
title.setVisible(true);
}
public static void main(String[] args) {
new GUI();
}
}
What am I doing wrong and how could I change the position of the Text if I manage to make it visible?
And I also want to add a button to go to the next page so if you could tell me how to do that too that would be great.
I would quickly and untested say that you are adding the label after you set the frame visible.
Do it before. Else you would have to revalidate and repaint the frame
As I can see in your code you are not adding title in panel. As a quick solution put panel.add(title); after title.setVisible(true); line in your code, it will display the label.
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
panel.setLayout(new GridLayout());
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Title");
frame.pack();
frame.setSize(854,560);
frame.setVisible(true);
JLabel title = new JLabel();
title.setText("Title");
//title.setSize();
title.setVisible(true);
panel.add(title); //<---- this one line will diaplay label in GUI

JFrame not displaying panel content

I just got started learning JFrame and is trying to create a frame containing JLabels and JTextFields frame using grouplayout, but the contents inside my panel aren't appearing when I run the program.
All help is appreciated.
package practice;
import java.awt.*;
import javax.swing.*;
public class Boxc {
public static void main(String[] args){
JFrame frame = new JFrame("---------------------(-_-)---------------------");
JLabel headText = new JLabel("Teach Me");
//head text
headText.setVerticalAlignment(JLabel.TOP);
headText.setHorizontalAlignment(JLabel.CENTER);
headText.setFont(headText.getFont().deriveFont(20f));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 500);
frame.setResizable(false);
frame.setLocationRelativeTo(null); //Center start position
frame.add(headText);
//panel 1
JPanel panel1 = new JPanel();
GroupLayout layout = new GroupLayout(panel1);
panel1.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
JLabel uInput = new JLabel("When You Type:");
JTextField uText = new JTextField("Enter Here");
JLabel iReply = new JLabel("I Reply:");
JTextField iText = new JTextField("Enter Here");
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
hGroup.addGroup(layout.createParallelGroup().
addComponent(uInput).addComponent(iText));
hGroup.addGroup(layout.createParallelGroup().
addComponent(uText).addComponent(iText));
layout.setHorizontalGroup(hGroup);
frame.add(panel1);
}}
Java is weird.
It all has to do with the ordering of your JFrame methods. You must ALWAYS do frame.setVisible(true); at the end, preferably as the last line of code in whatever you're using to initiate the JFrame. In that case,
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 500);
frame.setResizable(false);
frame.setLocationRelativeTo(null); //Center start position
frame.add(headText);
would be changed to
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 500);
frame.setResizable(false);
frame.setLocationRelativeTo(null); //Center start position
frame.add(headText);
frame.setVisible(true);
Hope this helps.
EDIT: ignore the changing from/to. Put the frame.setVisible(true); after frame.add(panel1);.

How to make JPanel scrollable in Java?

I have a class that extends JPanel, where I have overridden the paintComponent(Graphics g) method. However, I can't see the rectangles or the scroll bars I have drawn.
In the main function I have the following code below:
MyClass mainPanel = new MyClass();
mainPanel.setPreferredSize(new Dimension(1000, 1000));
mainPanel.setLayout(new BorderLayout());
JPanel scrollPanel = new JPanel();
scrollPanel.setSize(new Dimension(2000, 2000));
JScrollPane scrollPane = new JScrollPane(scrollPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); //Let all scrollPanel has scroll bars
scrollPane.setViewportView(scrollPanel);
scrollPane.setOpaque(true);
scrollPanel.revalidate();
mainPanel.add(scrollPane);
JFrame frame = new JFrame("Scrollable Panel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
I have solved this issue. MyClass extends JPanel. And I have overwritten paintComponent(Graphics g) method in which I draw rectangles through Graphics.
MyClass mainPanel = new MyClass()
mainPanel.setPreferredSize(new Dimension(7000, 1000));
mainPanel.setLayout(new BorderLayout());
JScrollPane scrollPane = new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); //Let all scrollPanel has scroll bars
scrollPane.setPreferredSize(new Dimension(1000, 900));
JFrame frame = new JFrame("Scrollable JPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scrollPane);
frame.setSize(1000, 900);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

Setting orientation within a JPanel

I am trying to use a nested JPanel, which I can then reuse in different parts of my application, eg navigation bar at the top of the page. I am having trouble setting the orientation of the items, eg I want the button to be above the textfield.
If I create them individually and add them directly to the JPanel they come out one on top of the other as inteneded, as below:
final JFrame frame = new JFrame("Nested Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel gui = new JPanel(new BorderLayout(5,5));
gui.setBorder( new TitledBorder("BorderLayout(5,5)") );
JButton button = new JButton("Button");
JButton button1 = new JButton("Button1");
gui.add(button, BorderLayout.NORTH);
gui.add(button1, BorderLayout.SOUTH);
frame.setContentPane(gui);
frame.pack();
frame.setLocationRelativeTo(null);
try {
// 1.6+
frame.setLocationByPlatform(true);
frame.setMinimumSize(frame.getSize());
} catch(Throwable ignoreAndContinue) {
}
frame.setVisible(true);
However, if I create a nested JPanel and put it inside another JPanel, so I can reuse it, they come out side by side, as below:
final JFrame frame = new JFrame("Nested Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel gui = new JPanel(new BorderLayout(5,5));
gui.setBorder( new TitledBorder("BorderLayout(5,5)") );
JPanel container = new JPanel();
JButton button = new JButton("Button");
JButton button1 = new JButton("Button1");
container.add(button, BorderLayout.NORTH);
container.add(button1, BorderLayout.SOUTH);
gui.add(container);
frame.setContentPane(gui);
frame.pack();
frame.setLocationRelativeTo(null);
try {
// 1.6+
frame.setLocationByPlatform(true);
frame.setMinimumSize(frame.getSize());
} catch(Throwable ignoreAndContinue) {
}
frame.setVisible(true);
I have tried setting the componenetOrientation,
container.setComponentOrientation(ComponentOrientation.);
but there is no option for vertical
I have tried setting the componenetOrientation
Please note the problem has nothing to do with component orientation: it's a layout manager problem as explained below.
However, if I create a nested JPanel and put it inside another JPanel, so I can reuse it, they come out side by side
Here:
JPanel container = new JPanel();
...
container.add(button, BorderLayout.NORTH);
container.add(button1, BorderLayout.SOUTH);
The default layout manager for panels is FlowLayout and it will ignore BorderLayout constraints. You'll have to set BorderLayout as layout manager not only to gui panel but to container panel as well.
JPanel container = new JPanel(new BorderLayout());
...
container.add(button, BorderLayout.NORTH);
container.add(button1, BorderLayout.SOUTH);
Try setting a layout for your JPanel. There are many Layouts available in the package java.awt. Some of them are BorderLayout, GridBagLayout, CardLayout, FlowLayout, GridLayout.
I have just added One line to your code, and now, it puts the buttons in the way you want:
final JFrame frame = new JFrame("Nested Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel gui = new JPanel(new BorderLayout(5,5));
gui.setBorder( new TitledBorder("BorderLayout(5,5)") );
JPanel container = new JPanel();
container.setLayout(new GridLayout(2,1)); // This is the line that I have added.
JButton button = new JButton("Button");
JButton button1 = new JButton("Button1");
container.add(button, BorderLayout.NORTH);
container.add(button1, BorderLayout.SOUTH);
gui.add(container);
frame.setContentPane(gui);
frame.pack();
frame.setLocationRelativeTo(null);
try {
// 1.6+
frame.setLocationByPlatform(true);
frame.setMinimumSize(frame.getSize());
} catch(Throwable ignoreAndContinue) {
}
frame.setVisible(true);
If you want to add more buttons, you can edit that line. The first Parameter of the constructor method of GridLayout is the number of vertical columns, and the second is the number of horizontal columns.

JScrollPane In JFrame with two panels

I want to add a scroll bar to my JTextArea but it just won't show up. I have read a lot of stuff on forums but all in vain. Any suggestions are highly appreciated.
Thanks in advance. Below is my code.
JPanel pan, pan2;
JTextArea text = new JTextArea();
JTextField fname = new JTextField(18);
JLabel filename = new JLabel("Filename");
JButton view = new JButton("View");
public FileReading() {
setLayout(new BorderLayout());
pan = new JPanel();
pan2 = new JPanel();
JScrollPane scroll = new JScrollPane(text);
//scroll.setBounds(400,400,400,400);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
text.setEditable(false);
scroll.setViewportView(text);
pan2.add(scroll);
//scrollpane.setViewportView(text);
pan2.setLayout(new BorderLayout());
//pan2.add(scrollpane);
pan.setLayout(new FlowLayout());
pan.add(filename, FlowLayout.LEFT);
pan.add(fname, FlowLayout.CENTER);
pan.add(view, FlowLayout.RIGHT);
view.addActionListener(this);
fname.addActionListener(this);
pan2.add(text, BorderLayout.CENTER);
pan2.add(pan, BorderLayout.SOUTH);
//BorderLayout.EAST
//add(pan, BorderLayout.SOUTH);
add(pan2);//, BorderLayout.CENTER
setVisible(true);
}
public static void main(String args[]) {
FileReading frame = new FileReading();
frame.setTitle("Enter The Full Path to the File");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(400,400,400,400);
//frame.setSize(400,400);
}
You need to add scroll (JScrollPane) to the pan2 not to the text (JTextArea)
try this
pan2.add(scroll, BorderLayout.CENTER);
in place of pan2.add(text, BorderLayout.CENTER);
EDIT
JTextArea gets added automatically when we add JScrollPane into the panel, as you have added text (JTextArea) inside JScrollPane
here -> JScrollPane scroll = new JScrollPane(text);
Can try this
add(scroll);//, BorderLayout.CENTER
add(pan, BorderLayout.SOUTH);
instead of
add(pan2);//, BorderLayout.CENTER
This way we are directly adding the scrollpane to main frame and putting other things below

Categories