Java swing scrollpane doesn't show the scroll bar - java

I was working on a java swing gui project for my course. When I was doing that, I found that I had too much information in a panel and it was not able to show everything. As a result, I wanted to add a Jscrollpane and I did some research about how to use this function but it didn't seem to work for my project, and I had no reason why even after I tried almost everything I could find on google.
Here is my code:
JFrame frame = new JFrame();
JPanel listpanel = new JPanel();
JScrollPane musiclist = new JScrollPane();
JButton selectButton = new JButton("Select song");
frame.setTitle("Music Player");
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setBounds(190,100,840,600);
Container c = frame.getContentPane();
musiclist.setViewportView (listpanel);
musiclist.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
musiclist.setPreferredSize(new Dimension(10, 1100));
musiclist.setBounds(0, 0, 190, 1000);
musiclist.setLayout(null);
listpanel.setLayout(null);
listpanel.setBackground(Color.GRAY);
listpanel.setBounds(0, 10, 160, 600);
selectButton.setBounds(55,20,100,30);
selectButton.setOpaque(true);
selectButton.setBackground(Color.LIGHT_GRAY);
selectButton.setBorder(null);
selectButton.setBorderPainted(false);
listpanel.add(selectButton);
c.add(musiclist, BorderLayout.WEST);
frame.setVisible(true);
musiclist.setVisible(true);
The problem now is that the scroll bar never shows up even I set the vertical to always show. I am just new to java, so any help will be helpful and thank you all for your time!

JFrame frame = new JFrame();
JPanel listpanel = new JPanel();
JScrollPane musiclist = new JScrollPane();
JButton selectButton = new JButton("Select song");
frame.setTitle("Music Player");
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(190,100,840,600);
Container c = frame.getContentPane();
musiclist.setViewportView (listpanel);
musiclist.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
musiclist.setPreferredSize(new Dimension(100, 1100));
musiclist.setBounds(0, 0, 190, 1000);
listpanel.setBackground(Color.GRAY);
listpanel.setBounds(0, 10, 160, 600);
selectButton.setBounds(55,20,100,30);
selectButton.setOpaque(true);
selectButton.setBackground(Color.LIGHT_GRAY);
selectButton.setBorder(null);
selectButton.setBorderPainted(false);
JList mlist=new JList();
DefaultListModel lmodel=new DefaultListModel();
lmodel.addElement("Song 1");
lmodel.addElement("Song 2");
lmodel.addElement("Song 3");
mlist.setModel(lmodel);
listpanel.setLayout(new BorderLayout());
listpanel.add(mlist, BorderLayout.CENTER);
listpanel.add(selectButton, BorderLayout.SOUTH);
c.add(musiclist, BorderLayout.WEST);
musiclist.setVisible(true);
frame.setVisible(true);

Related

How to have a JButton that moves as I scroll on the frame?

I have a Panel which I have made scrollable in my frame.
What I need is to add a button that stays fixed in the lower right corner even when I scroll.
I'm new to Java Swing so would appreciate all and any help that I can get.
mainPanel = new SimulationPanel(); //class SimulationPanel extends JPanel
//making mainPanel scrollable
mainPanel.setPreferredSize(new Dimension(((int)(WIDTH*1.2)), HEIGHT));
JScrollPane scrollPane = new JScrollPane(mainPanel);
scrollPane.setViewportView(mainPanel);
// Settings for JFrame
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame("Warehouse Simulator");
frame.setContentPane(scrollPane);
frame.setSize(screenSize.width, screenSize.height);
frame.setResizable(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
I would use nested panels with the outer one be with BorderLayout. Then one with FlowLayout and align FlowLayout.RIGHT and the button inside it.
public class Example extends JFrame {
public Example() {
super("");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JTextArea textArea = new JTextArea(10000, 0);
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane, BorderLayout.CENTER);
JButton button = new JButton("button");
JPanel panelWithButton = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panelWithButton.add(button);
add(panelWithButton, BorderLayout.PAGE_END);
setLocationByPlatform(true);
pack();
setSize(600, 600);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new Example().setVisible(true);
});
}
}
Result:
I would go for a BoxLayout. Add another panel (metaPanel) in which your first put your scrollingPanel, and then you add a button. Instead of usgin scrollingPanel as contentPane, you use metaPanel. Example (the example works, but you need to modify it to make the interface look nice):
JPanel mainPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(mainPanel);
scrollPane.setViewportView(mainPanel);
JPanel metaPanel = new JPanel();
BoxLayout boxlayout = new BoxLayout(metaPanel, BoxLayout.Y_AXIS);
metaPanel.setLayout(boxlayout);
metaPanel.add(scrollPane);
metaPanel.add(new JButton("button"));
// Settings for JFrame
frame = new JFrame("Warehouse Simulator");
frame.setContentPane(metaPanel); // Put metaPanel here
frame.setSize(500, 300);
frame.setResizable(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);

How can I move a table downward in Java?

Is it possible to move a table downward in Java. I tried setBorder, setLocation, ... but it didn't work. What should I do?
JLabel label = new JLabel("Enter proper data: ");
label.setBounds(0, 0, 120, 50);
frame.add(label);
JButton btn = new JButton("Click");
btn.setBounds(100, 300, 80, 50);
frame.add(btn);
String data [][] = new String [6][4];
String column[]={"No.","Player 1","Player 2","Strategy"};
JTable table = new JTable(data, column);
JScrollPane sp=new JScrollPane(table);
frame.add(sp);
frame.setSize(300,400);
frame.setVisible(true);
Please look at this image:
Looks good for a BorderLayout for the outer panel (outlined in blue). The big red label goes in the PAGE_START and the (scroll pane of the) table goes in the CENTER.
Put the button in a panel with a FlowLayout (centered), the panel with the red outline, in the PAGE_END of the border layout. Done.
All extra height (if the user makes the GUI bigger) will be given to the table.
I changed the code based on #AndrewThompson suggestion and result was good.
JFrame frame = new JFrame("Play");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel outerPanel = new JPanel(new BorderLayout());
JPanel topPanel = new JPanel(new BorderLayout());
JLabel label = new JLabel("Enter proper data: ");
JButton btn = new JButton("Click");
String data [][] = new String [100][4];
String column[]={"No.","Player 1","Player 2","Strategy"};
JTable table = new JTable(data, column);
JScrollPane sp=new JScrollPane(table);
topPanel.add(label, BorderLayout.PAGE_START);
topPanel.add(sp, BorderLayout.CENTER);
topPanel.add(btn, BorderLayout.PAGE_END);
outerPanel.add(topPanel);
frame.add(outerPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

How do I add a component in the upper-left of a JScrollPane?

I have created a JScrollPane with a RowHeaderView, a ColumnHeaderView and a ViewPortView. I added JPanels in diffrent colors and noticed, that there is one cornor left, on the upper-left where you cant just add a Component. I wanted to ask, how it is possible to add a Component there.
Here a image. The area I mean is green:
And here my Code:
public class Example {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
JPanel panel0 = new JPanel();
panel0.setBackground(Color.yellow);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.red);
panel1.setPreferredSize(new Dimension(30, 200));
JPanel panel2 = new JPanel();
panel2.setBackground(Color.blue);
panel2.setPreferredSize(new Dimension(200, 30));
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(panel0);
scrollPane.setRowHeaderView(panel1);
scrollPane.setColumnHeaderView(panel2);
scrollPane.setBackground(Color.green);
frame.add(scrollPane);
frame.setVisible(true);
}
}
It's easy. Use the method setCorner
scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, new JButton());

JPanels: One with a JTextArea and another with a JLabel

I've been at this for a good while now, but I cannot seem to get a hand of it. I'm trying to produce a JPanel that has a JTextArea above and two JLabels below, but my JLabel ends up on the left side of my JTextArea and I cannot make the other appear.
Here's my code (sorry for the display stuff- just filler really):
public JPanel contentPane() {
JPanel something = new JPanel();
String information = "Please";
info = new JTextArea(information, 4, 30);
info.setEditable(false);
info.setLineWrap(true);
info.setWrapStyleWord(true);
JPanel one = new JPanel(new BorderLayout());
one.setBackground(Color.WHITE);
one.setLocation(10, 10);
one.setSize(50, 50);
one.add(info, BorderLayout.CENTER);
something.add(one, BorderLayout.NORTH);
JPanel two = new JPanel(new BorderLayout());
two.setBackground(null);
two.setLocation(220, 10);
two.setSize(50, 50);
two.add(new JLabel("Please work"), BorderLayout.EAST);
two.add(new JLabel("Oh gosh, please"), BorderLayout.WEST);
something.add(two, BorderLayout.SOUTH);
something.setOpaque(true);
return something;
}
public static void GUI() {
JFrame frame = new JFrame("You Guessed It!");
DisplayStudent panel = new DisplayStudent();
frame.setContentPane(panel.contentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 150);
frame.setVisible(true);
}
Please and thank you to anyone who takes the time to help.
When you create you something, you don't specify any layout manager, but later on you attempt to add one to something using BorderLayout constants -- which will not work, since the default layout manager for a JPanel is FlowLayout.
Try this instead;
JPanel something = new JPanel(new BorderLayout());

How to move components in a jframe to a certain spot

so im having trouble moving components to a certain part of the jframe, ive tried using BorderLayout and setBounds() but both of those don't seem to work.
// sets the start button
start = new JButton(imgStart);
start.setPreferredSize(new Dimension(150, 55));
start.setFocusable(false);
start.addActionListener(this);
// sets the controls button
controls = new JButton(imgControl);
controls.setPreferredSize(new Dimension(150, 55));
controls.setFocusable(false);
controls.addActionListener(this);
controls.setBounds(151, 56, 0, 0);
JFrame frame = new JFrame();
frame.setContentPane(this);
frame.add(start);
frame.add(controls, BorderLayout.WEST);
frame.setTitle("Freedom Planet");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(this);
frame.setResizable(false);
frame.setSize(imgBackground.getIconWidth(), imgBackground.getIconHeight());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
I also have the some problem with a panel im using in a different part of my program
// creates a panel called Info and and adds components to them
JPanel Info = new JPanel();
//Info.setBorder(BorderFactory.createLineBorder(Color.black, 5));
Info.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 10));
//Info.setBounds(290, 180, -10, 10);
Info.setOpaque(false);
Info.add(lblTime);
Info.add(lblTimer);
Info.add(lblScore);
Info.add(lblShowScore);
addKeyListener(this);
setFocusable(true);
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setContentPane(this);
frame.add(Info, BorderLayout.SOUTH);
frame.add(lblLevel);
frame.setTitle("Freedom Planet");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(this);
frame.setResizable(false);
frame.setSize(imgBackground[0].getIconWidth(), imgBackground[0].getIconHeight());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
I'm trying to move the components horizontally
Then maybe you can use a Swing Border. An EmptyBorder will allow you to add extra space around the components.

Categories

Resources