Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have working piece of code that adds a scroll bar to JPanel in the following form
add(scrollPane,"*");
I usually would use
add(scrollPane);
Seemingly the former and the later syntax vary in the way they layout the scroll bar inside the JPanel . I can see the differences but anyone know a formal reason for it ?
What does the * mean here ?
Take a look at Container#add(Component, Object).
The particular meaning will depend on the layout manager been used and could be ignored completely if the layout manager does not support constraints.
Consider GridBagLayout for example, in order to add components (with any real control/meaning), you would need to pass it a reference to a GridBagConstraints. BorderLayout is the same.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
I have the following, code which almost does what I want except it doesn't automatically stretch to the right, so the whole label can be seen. I've tried different constrains on it, but it doesn't change, setting the JFrame bigger works, but I would like it to resize depending on the label length, so it doesn't occupy more space on the screen than needed.
The UI Window
Run JFrame.pack() to resize the frame according to it's components.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I currently have a menu, that whenever I click on one of the options, I want to clear the screen (to clear all my JLabels and text areas). I use the code
getContentPane().removeAll();
getContentPane().repaint();
add(comboBoxOptions);
to clear the screen. After that I try to add a combo box, which adds but it doesn't show up. I can click on the options but it's hidden somehow I guess. How could I fix this?
How could I fix this?
Use a CardLayout, see How to Use CardLayout for more details
Swing's layout management API is lazy, it won't update the layout's automatically, it waits till you tell it to. This is a good thing.
You need to use revalidate to force the container hierarchy to be relaid out and repaint to schedule a repaint of the view, for example
getContentPane().removeAll();
add(comboBoxOptions);
revalidate();
repaint();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a problem with my project. I'm making a tool for numerical methods (a lesson in college). I have done nearly all of the project but I have a problem with my design.
When I resize it before making anything it resizes well but, if I set the size of the matrix after the matrice processes the panel isn't resizing.
The code works well if the rank is <=5, but the bigger matrices cause that problem.
I'm using window builder and the code is messy but I'll be glad if you try to help me.
Thanks for your helps!
Some of things that I noticed in your code.
Don't use null layout at all and avoid setBounds() method
Always hand over it to Layout manager to set the position and size the components.
Use ActionListener for JButton instead of MouseListener if you want to capture click event only.
Note: I can't run your code on my system due to character encoding issue. You have used some character other than English in your code.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm using gridbaglayout and I want my window to be 700 x 500 always. I also was my JButtons to be the exact same size but if the word inside them is longer they get longer and I can't have this. How can I do this? Please help me with good code I been trying for hours.
I tried to do frame.setSize and panel.setSize but neither work and I need this fast! Please help!
The basic answer is, you don't. That's not the point of any layout manager.
A layout manager simple makes decisions about how best to layout it's children based on the sizing hints that they provide.
Remember, while it might look great on your screen, the next computer you run it on may make it look like crap.
If you "must" define the size of anything, then you need to override the getPreferredSize method of your component and return an appropriate size hint.
Having said that, I wouldn't do this for components like JButton (or actually anything other than JPanel and JComponent), the way they calculate their sizes are complicated and best left alone.
You can modify the size of components through the use of Borders and, in the case of GridBagLayout, Insets and modifying the GridBagConstraints properties.
Have a closer look at How to Use GridBagLayout for some more ideas
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am still learning java in college but I just would like to know the way or ways to put the JComponent's in specific location and specify their size.
I have seen the methods setLocation() and that sort of methods but I just really wanna know how to put a Component without using those layout managers(flow, grid, border?).
Here is the code I would like to apply that knowledge to: they are two overloaded methods that display the same window.
public void addWelcome()
{
setWindow();
MaxDimension = new Dimension(WelcomeLabelDimensionWidth,WelcomeLabelDimensionHeight);
getWindow().setMaximumSize(MaxDimension);
MinDimension = new Dimension(WelcomeLabelDimensionWidth,WelcomeLabelDimensionHeight);
getWindow().setMinimumSize(MinDimension);
getWindow().setTitle("Bills");
getWindow().setSize(WindowWidth,WindowHeight);
getWindow().setDefaultCloseOperation(EXIT_ON_CLOSE);
getWindow().setLayout(new GridLayout(5,1,2,0));
getWindow().add(getWelcomeLabel());
getWindow().add(getGreetLabel());
getWindow().add(getDescriptionLabel());
getWindow().add(getInstructionLabel());
getWindow().add(getStartButton());
getWindow().pack();
getWindow().setVisible(true);
}
public void addWelcome(final int Width, final int Height)
{
setWindow();
MaxDimension = new Dimension(Width,Height);
getWindow().setMaximumSize(MaxDimension);
MinDimension = new Dimension(Width,Height);
getWindow().setMinimumSize(MinDimension);
getWindow().setTitle("Bills");
getWindow().setSize(WindowWidth,WindowHeight);
getWindow().setDefaultCloseOperation(EXIT_ON_CLOSE);
getWindow().setLayout(new GridLayout(5,1));
getWindow().add(getWelcomeLabel());
getWindow().add(getGreetLabel());
getWindow().add(getDescriptionLabel());
getWindow().add(getInstructionLabel());
getWindow().add(getStartButton());
getWindow().setVisible(true);
}
Apologize if code/question are not clear enough.
Thanks in advance.
You can use an Absolute Layout, which makes you responsible for positioning everything. Do note that this also makes it nigh impossible to create a layout that will be usable with different screen and window sizes (so yeah, learn to use proper layouts).
Yes a layout manager is the best solution
An IDE like eclipse or netbeans can help you effortlessly visually position your components with different layouts.
It autogenerates code which you can easily observe and learn.
If you want to code it yourself you have all resources in this link
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html