This question already has answers here:
Why is my table not visible when it's in a JScrollPane?
(2 answers)
Closed 8 years ago.
I'm busy making a level designer for a game I'm planning to make in Java. I have a panel for settings, for example the size of the level or what the background will be. You can also select images to put on the other panel, the 'DesignerPanel' and then you should be able to click on that panel to put it in a specific place. I put the DesignerPanel in a JScrollPane to scroll around bigger levels.
The problem is that the JScrollPane doesn't appear and neither does the panel that should be in it. I have found some questions about the scroll bars not appearing, but in my case nothing appears. Well, at least, almost nothing.
You can see really small stripes of the DesignerPanel to the right and below. However, there is no trace of the JScrollPane or the rest of the panel. Resizing or minimizing and unminimizing the screen doesn't make a difference. I would post an image and I think it would surely help, but apparently I need reputation to do that, so sorry about that.
Here's the relevant code I have so far. I have hidden most of the code because it is not important for this error.
package games;
import java.awt.*;
import java.io.*;
import javax.swing.*;
class LevelDesignerScreen extends JFrame
{
private SettingsPanel sp;
private DesignerPanel dp;
private JScrollPane scroller = new JScrollPane();
LevelDesignerScreen()
{
sp = new SettingsPanel(this);
add(sp, BorderLayout.WEST);
dp = new DesignerPanel(sp);
dp.setSize(1000, 1000);
scroller.setPreferredSize(new Dimension(600, 600));
scroller.add(dp);
add(scroller, BorderLayout.CENTER);
}
public static void main(String[] args)
{
new LevelDesignerScreen();
}
}
You should use
scroller = new JScrollPane(dp);
or
scroller.setViewportView(dp);
instead of scroller.add(dp).
And on a more general note: if you are having problems with layout, put prime colored line borders on all involved components to see what takes up space and what doesn't.
Related
This question already has answers here:
Java items appear only after the window is resize
(2 answers)
Closed 5 years ago.
I've encountered what I am pretty sure is a glitch, and have not found any way around it. I, at present, have only a simple window that has a text field and a label. When I first run the program, what appears is an empty window, when I resize the window, either by maximizing or just manually resizing a little bit, the components appear, what's going on here?
public class Calculator {
public static void main(String[] args) {
JFrame mainFrame = new JFrame("Calculator");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(300,400);
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
JPanel mainPanel = new JPanel();
mainFrame.add(mainPanel);
JTextField mainField = new JTextField(20);
mainPanel.add(mainField);
JLabel mainLabel = new JLabel("Orange");
mainPanel.add(mainLabel);
}
}
By default the size of all components is (0, 0), so there is nothing to paint.
Components need to be added to the frame BEFORE the setVisible() method. Then when the frame is made visible the layout manager is invoked and components are given a size/location.
You're adding components to the frame after calling setVisible(true).
This question has already been asked.
Java items appear only after the window is resize
I'm trying to add a JScrollPane to my JList but for whatever reason it isn't working. I've read several tutorials both on here and other sites and it seems i'm following the directions correctly. I've tried it using both a DefaultListModel and no DefaultListModel seeing if it would make a difference. I've also tried resizing the widget itself and that doesn't work, either.
Here is my code. itemNames is an array of Strings[] which contain various souvenir names that i'm adding to the JList. i'm using BorderLayout() and the panel i'm attempting to add the JScrollPane to is utilizing a GridBagLayout():
souvenirList = new JList(itemNames);
souvenirList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
souvenirList.setLayoutOrientation(JList.VERTICAL);
souvenirList.setVisibleRowCount(-1);
scrollPane = new JScrollPane(souvenirList);
gbc3.gridx = 1;
gbc3.gridy = 1;
centerPanel.add(scrollPane, gbc3);
c.add(centerPanel, BorderLayout.CENTER);
The box shows up with the names in it, but no JScrollPane :( I was hoping someone could help me out, maybe point out a simple mistake I may be making. Thanks to all in advance. Please don't post links to tutorials on the topic cus believe me, i've read them thoroughly.
EDIT: In this small runnable example I built, using mostly code from my previous snippet, the bar shows up fine! Why could this be? Could it have something to do w the JPanel i'm adding it to? Or maybe the GridBagLayout? I'm so confused here....
import javax.swing.*;
import java.awt.*;
public class JScroll extends JFrame
{
JList souvenirList;
JScrollPane scrollPane;
Container c = getContentPane();
private String[] itemNames = {"mug","cap","tee shirt","sweat shirt","pennant","mini stick",
"bobblehead","paper bag","foam paw","thunderstix"};
public JScroll()
{
setLayout(new FlowLayout());
souvenirList = new JList(itemNames);
souvenirList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
souvenirList.setLayoutOrientation(JList.VERTICAL);
//souvenirList.setVisibleRowCount(-1);
scrollPane = new JScrollPane(souvenirList);
c.add(scrollPane);
}
public static void main(String[] args)
{
JScroll frame = new JScroll();
frame.setSize(400,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
The issue was centered around the fill() method within GridBagLayout(). I didn't notice it was set to VERTICAL, meaning the JList was going to request more vertical space. Changing to HORIZONTAL fixed the issue.
This question already has answers here:
JFrame: How to disable window resizing?
(8 answers)
Closed 9 years ago.
how to get non resizable frame in gui i confused about this because i am using setLayout(new FlowLayout()); so if i drag the size of the frame the location of my button is going to disarrange . here is my code so far
import java.awt.*;
import javax.swing.*;
public class aw extends JFrame
{
private JTextField aw1;
private JLabel aww;
private JButton aw2;
public aw()
{
setLayout(new FlowLayout());
aww = new JLabel("Enter Your Password");
add(aww);
aw1 = new JTextField(15);
add(aw1);
aw2 = new JButton("Enter");
add(aw2);
}
public static void main(String args [])
{
aw v = new aw();
v.setSize(200,200);
v.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
v.setVisible(true);
}
}
I think setResizable(false) is what you're looking for
SIDE NOTES
Also, instead of setSize(). You should just pack() the frame. You can use EmptyBorders if you want empty space.
If you wanted the frame to be re-sizable and you want all the components centered when resizing, You could always wrap them all in a JPanel, then add the JPanel to the frame.
Use Java naming convention. Class names start with capital letters.
Run Swing apps form the Event Dispatch Thread, see Initial Threads
Put all the content into a JPanel, that would let you configure the pack() element, please ensure that you use an Empty Border. In the Object we have an accesor by setResizable set it to false.
Keep a note of rest and then use a Singleton thread model to run the Event-Dispatch Thread.
Thanks to AndrewThompson for his extra-ordinary knowledge that I was able to make the necessary updates
Alright I'm relatively new to programming and it may be just something simple that I'm missing but the other threads related to this topic the poster didn't give adequate information relative to their issue for others to provide quality answers so I will give it a shot.
public BenchUI(JFrame j){
jf = j;
init();
add(mainPanel);
topPanelButtons();
selectedCustomer();
rentalOptions();
clientListBox();
}
At this point i can point out that everything works perfectly until I add the clientListBox() method. (below)
public void clientListBox(){
clientList = new JComboBox(moo);
clientList.setPreferredSize(new Dimension(460,30));
gbc.gridx = 0;
gbc.gridy = 0;
leftSide.add(clientList,gbc);
}
i can comment it out and get my whole GUI back working perfectly but without a JComboBox.
moo is String [] moo = {"Fish","Goat", "Monkey"};
a dummy string just for testing purposes and initialized at the start.
So any idea why my GUI completely disappears when I place in the clientList?
If anything else is necessary I'll be watching this thread and can provide additional information.
As a side note I keep getting warnings for "Raw Types" but it works without specifiying, could I potentially run into trouble by not specifying my JComboBox?
EDIT:
ok I believe I've duplicated whatever the issue is in this code
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
public class main {
public static void main(String[] args){
JFrame jf = new JFrame();
jf.setExtendedState(JFrame.MAXIMIZED_BOTH);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setResizable(false);
BenchUI bu = new BenchUI(jf);
jf.add(bu);
}
}
public class BenchUI extends JPanel{
JPanel one;
JFrame jf;
JComboBox<String> clientList;
String[] moo = {"Goat", "Fish", "Donkey"};
public BenchUI(JFrame j){
jf = j;
one = new JPanel(new GridBagLayout());
one.setBackground(Color.blue);
one.setPreferredSize(new Dimension(300,300));
clientList = new JComboBox<String>(moo);
one.add(clientList);
add(one);
}
}
with the clientList stuff commented out I get my silly little blue panel and once it is added I lose the blue panel and the combobox doesnt show up as well...betting on this is a facepalm issue at this point >.<
EDIT: to include the main class.
EDIT: took out the comment marks for the JComboBox constructor and implementer
Your posted sort of sscce-like (not a real SSCCE by the way since we can't run it) code doesn't add any such as the JComboBox to the JPanel and adds no components such as the current JPanel to the JFrame.
public class BenchUI extends JPanel{
JPanel one;
JFrame jf;
JComboBox<String> clientList;
String[] moo = {"Goat", "Fish", "Donkey"};
public BenchUI(JFrame j){
jf = j;
one = new JPanel(new GridBagLayout());
one.setBackground(Color.blue);
one.setPreferredSize(new Dimension(300,300));
//clientList = new JComboBox<String>(moo);
//one.add(clientList);
add(one);
}
}
and so it makes sense that none of the components will show up on any JFrame. You will want to read the Swing tutorials on how to add components to other components (or containers) and how to create and show a JFrame. Have a look at How to Use Swing Components.
Edit
Your latest code now does in fact add the BenchUI JPanel to the JFrame, but still you add no components to the BenchUI JPanel, and in fact you don't even construct your JComboBox but only create a JComboBox variable. Again, I strongly urge you to read the Swing tutorials which I've linked to above as well as the general Java tutorials.
Edit 2
Some general advice:
If you want to add a component to a GUI you must first create the component object. You are declaring your clientList JComboBox, but you never create the object.
Then you must add the component object to a container that eventually will be part of the hierarchy leading to a top level window such as a JFrame, JDialog, JApplet and such. You never add a clientList object to the GUI.
You should add your components to the top level window before calling pack() on the top level window -- which tells all the layout managers to lay out all the components they hold.
You should then call setVisible(true). One problem with your code (other than not creating important components and not adding them to the GUI!) is that you're calling setVisible(true) on your JFrame way too early before adding anything to the GUI.
Read the Swing tutorial, but especially the one on using layout managers and on adding components to a top level window.
Edit 3
OK, now you're creating your JComboBox, but you still are adding all components to your JFrame after setting it visible. Please re-check my 3rd and 4th bullets in the bullet list above.
This code, when run, will make a window but not at the specified dimensions. What is wrong with it?
import javax.swing.*;
import java.awt.*;
public class Windowing {
void JFrame(){
JFrame frames = new JFrame("Total recall");
frames.setSize(1000,8000);
frames.setVisible(true);
frames.pack();
//Buttons push = new Buttons();
//((Buttons) push).buttons();
JTextField wager = new JTextField(1);
wager.setSize(100,200);
wager.setVisible(true);
wager.setLocation(100, 200);
frames.add(wager);
//frames.add(push);
}
}
You could remove the call to frames.pack(); it overrides the previously set frame size.
However, what you really want to do is remove the frames.setSize(1000,8000) and move frames.pack() down to the bottom of the method; that will ensure that the frame is big enough to display its contents but not too big to fit on the screen.
If you call pack before adding anything to the frame (like you are doing now), it will make the window extremely small; it's probably appearing near the upper left of your screen, but you won't notice it unless you know where to look.
It looks like you have a number of "opportunity areas" here.
To start, it seems like you set frame size to 1000x8000 because you didn't see any change right?
Secondly you call setVisible on the textField because you didn't see that either.
And finally you're setting the size of the textfield ( I guess because you're seeing it take the whole frame )
The problem here is that you have to invoke pack and setVisible at the end of the construction. Also, you have to learn how to use layout managers and frames.
Swing, is very powerful, but it is a bit hard to grasp at the beginning.
These two links will be helpful:
How to make frames
Using Layout Managers
I've changed your code and the result looks like this:
Here's the modified source code.
import javax.swing.*;
import java.awt.*;
public class Windowing {
public static void main( String [] args ) {
Windowing windowing = new Windowing();
windowing.showFrame();
}
void showFrame(){
JFrame frame = new JFrame("Total recall");
JButton push = new JButton("Push");
JTextField wager = new JTextField(15);
// Panels do have "FlowLayout"
JPanel panel = new JPanel();
panel.add(wager);
panel.add(push);
frame.add( panel );
frame.pack();
frame.setVisible(true);
}
}
Try to use setPreferredSize(Dimension) instead.