Scroll pane isn't coming down when add components - java

I have this simple program where I upload images into a java db database (just add their location to the data base as well as their type) then I show them in the program in a scrollpane the problem is the scrollpane isn't scrolling down,
this is my code
`
JPanel cont = new JPanel();
cont.setPreferredSize(new Dimension());
cont.setLayout(new FlowLayout(FlowLayout.LEFT));
while(sports_re.next()){
String location=sports_re.getString("LOCATIONN");
for(int i=0;i<100;i++){
JLabel picLabe = new JLabel();
picLabe.setIcon(new ImageIcon(new
ImageIcon(location).getImage().getScaledInstance(100, 100,
Image.SCALE_SMOOTH)));
cont.add(picLabe);
}
}
jScrollPane1.getViewport().setView(cont);
`
I tried to add width and height to the dimension and it worked but I have to put a huge number for it to work (if I put a small number it won't allow me to scroll ) It's not right
this is what it looks like without the "cont.setPreferredSize(new Dimension());"
I want to make it dynamic, please help, thank you

cont.setPreferredSize(new Dimension());
Remove this to let LayoutManager calculate the size. TGhen scrollpane reflect the calculated size rather than fixed you provided.

Related

Change JTextField height on Swing

I'm trying to fix the height of the "amountField" text field, but I can't.
I would like the height of amountField to have the same height as the JComboBox that it's above, so it looks better.
Right now, the JTextField looks very tall compared with the rest of design.
I've tried everything that I've read in this forum, but nothing seems to work.
I don't know if it's relevant, but this whole JPanel (WithdrawalScreen) is inside another JPanel with BorderLayout. This panel is the center part of it
Thanks
PictureHere
public class WithdrawalScreen extends JPanel {
Public JPanel init() {
this.setLayout(new GridLayout(0,1));
account = new JLabel("account");
accountSelect = new JComboBox(labels);
amount = new JLabel("amount");
amountField = new JTextField("");
submit = new JButton("SUBMIT");
this.add(account);
this.add(accountSelect);
this.add(amount);
this.add(amountField);
this.add(submit);
return this;
}
}
Try creating the Grid Layout with 5 rows and 1 column. I think the height is messed up because you are not setting the constructor arguments properly.
new GridLayout(5,1);
Grid layout will stretch the component and give the same size to all of its components. In order to keep the "default" size of each component, you can use BoxLayout with BoxLayout.Y_AXIS parameter in its constructor. Another way would be to use a dummy-nested JPanel with another layout. Let's say FlowLayout.
JTextField textField = new JTextField(10);
JPanel nestedPanel = new JPanel(new FlowLayout());
nestedPanel.add(textField);
gridLayoutPanel.add(nestedPanel);
JTextField will not be stretched. nestedPanel will be. Do some experiments yourself and you will find the way that fits your needs.
A link that will help you: A visoual guide to Layout Managers.

Java JTextField view from the Right

I have a JTextField that I'm trying to get to automatically view from the Right (Not align to the right), so if the text is to long for the JTextField it will display the last characters in the String instead of the beginning.
Ive been searching for ages trying to locate an answer but keep coming up with aligning.
The 2 images below show what i get and what I'm after, the text is "123456789_123", the JTextField is only big enough to contain the "123456789" but i want to see the "56789_123" instead without having to focus on the field. (i can use something other than a JTextField if needed, tried a JTextArea but had the same issue).
What i Get
What I'm after
I can not just make the Field bigger as I'm restricted by other Objects in my program. Usually the text fits fine but every now and then its too big.
Found a work around.
you create a JScrollPane, make its vertical and horizontal bars invisible by setting there dimensions to 0,0. then scroll to end using 'setValue' to max.
hope this helps anyone trying to do something similar.
JTextArea editArea2 = new JTextArea(5,5);
editArea2.setText("123456789_12345");
editArea2.setAlignmentX(CENTER_ALIGNMENT);
asd = new Dimension();
asd.height = 20;
asd.width = 70;
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(editArea2);
scrollPane.setMinimumSize(asd);
scrollPane.setMaximumSize(asd);
scrollPane.getVerticalScrollBar().setPreferredSize (new Dimension(0,0));
scrollPane.getHorizontalScrollBar().setPreferredSize (new Dimension(0,0));
scrollPane.getVerticalScrollBar().setMinimumSize(new Dimension(0, 0));
scrollPane.getVerticalScrollBar().setMinimumSize(new Dimension(0, 0));
scrollPane.getVerticalScrollBar().setMaximumSize(new Dimension(0, 0));
scrollPane.getVerticalScrollBar().setMaximumSize(new Dimension(0, 0));
scrollPane.getHorizontalScrollBar().setValue( scrollPane.getHorizontalScrollBar().getMaximum() );

Java swing GUI creation

I'm developing a Java GUI and I need:
A label in first row(only one label).
Starting 2nd row need to add say 100 buttons which extends to multiple lines(width shouldn't go beyond the visible screen)
In a new line one more Label
From next line say 100 buttons which extends to multiple lines(width shouldn't go beyond the visible screen)...
[OPTIONAL] If the components exceeds JFrame height then need a scroll facility to the main window (only vertical)
I have a strange results with flow layout, sometimes it stick to visible width, sometimes it sets even 500 buttons in a single row.
I have tried every layout and also multipanes. Still no luck.
Please guide.. just need an idea, No need of code
Updated with code: Sorry guys, that was my first question to stackoverflow
Thanks for prompt response
Infact i tried many, here is a simple one.
setLayout(new FlowLayout());
setTitle("JAVA GUI");
setSize(500,500);
setVisible(true);
add(new JLabel("row 1"));
JPanel panel1 = new JPanel(new FlowLayout());
for(int i=0;i<200;i++){
panel1.add(new JButton("b"+i));
}
add(panel1);
Here the panel1 is appearing in a sigle row which goes beyond the visible part of the screen.
I think this can be solved by setting maximumsize to Jframe, but no idea how to set its size to FULL SCREEN.
You can try MigLayout.
http://www.miglayout.com/
Also this question is not really a question for stack overflow. A good way to ask your question would be to post your code and tell us what is wrong with it and what it is supposed to do.
While this is not the norm for 'good' stackOverflow questions, I don't have any problem with it myself. Some people cannot deal with anything except code. I would suggest that, if you're going to post code, that you take the trouble to post code that will compile, run, and demonstrate your situation. It really helps those of us out here understand what you're seeing and what you're trying to do.
You talk about "rows"; be aware that rows and columns are terms used with things like GridLayout and GridBagLayout, but I don't think they're appropriate for what you describe.
In your description, you don't say what you want scrolled. It would appear you want the entirety of the UI scrolled, I'll assume that for now.
I would try a JPanel with BoxLayout, oriented vertically, for the overall main UI. You will put some things into that:
The first JPanel.
Another JPanel, set with FlowLayout, holding the first bunch of buttons.
Another JPanel with the next JLabel
And a fourth JPanel, set with FlowLayout, holding the second bunch of buttons.
Now, I would put the top-level panel into a JScrollPane, and then put that into the CENTER section of a Frame (with its default BorderLayout), and see what happens. To tell the truth, I'm not sure, but these are the things I would start with.
I cannot tell, without running code, why you get odd behavior sometimes.
As said in a previous comment, using a ContentPane is the way to go. Here is a working example of what you want:
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("JAVA GUI");
JPanel panel1 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
int nbLines = 10;
for (int i = 0; i < nbLines; i++) {
JPanel linePanel = new JPanel(new FlowLayout());
linePanel.add(new JLabel("row " + i));
for(int j = 0; j < 50; j++) {
linePanel.add(new JButton("b" + j));
}
panel1.add(linePanel);
}
frame.setContentPane(panel1);
//frame.setSize(500, 500);
frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
frame.pack();
frame.setVisible(true);
}
}
And here is what I get:
If you want to have left-aligned buttons you can use:
JPanel linePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

Java: How to nest JPanel in a GridLayout?

I want to know how to nest JPanels using a GridLayout. This is how it should look like.
I approached this problem by 2 ways so far,
using JPanels and
using JLabels,
and none of them worked (only the first panel created is shown).
Here is the code for the JPanel approach:
int x=20, y=20;
JPanel [] panels = new JPanel[3];
JLabel animal = new JLabel(new ImageIcon(getClass().getResource("Pictures/animal.gif")));
JLabel map = new JLabel(new ImageIcon(getClass().getResource("Pictures/map.gif")));
JLabel mountain = new JLabel(new ImageIcon(getClass().getResource("Pictures/mountain.gif")));
for(int i=0;i<panels.length;i++)
{
if(i>0)
{
x+=x;
y+=y;
}
panels[i] = new JPanel(new GridLayout(2,2));
panels[i].setPreferredSize(new Dimension(x,y));
if(i==0)
panels[i].add(new JPanel());
else
panels[i].add(panels[i-1]);
panels[i].add(mountain);
panels[i].add(map);
panels[i].add(animal);
}
add(panels[2]);
One option is to create a class that will represent a panel divided into the grid with the images. The only issue would be the top left quadrant which would usually contain the nested panel, at some point you want this to contain just a blank panel. So maybe something like this (barring various optimizations):
class GridPanel extends JPanel{
JLabel mountain, map, animal;
public GridPanel(JPanel panel){
super();
setLayout(new GridLayout(2, 2));
animal = new JLabel(new ImageIcon(getClass().getResource("pictures/animal.gif")));
map = new JLabel(new ImageIcon(getClass().getResource("pictures/map.gif")));
mountain = new JLabel(new ImageIcon(getClass().getResource("pictures/mountain.gif")));
add(panel);
add(mountain);
add(map);
add(animal);
}
}
Notice that it accepts the panel that is to be displayed in the top left corner of the grid. This coud then be called with the panel specified. So at the point where you want to create the main panel:
JPanel grid = new GridPanel(new JPanel()); //initial
for(int i = 1; i <= 5; i++){
grid = new GridPanel(grid);
}
add(grid);
The initial grid is created with a blank JPanel. And every subsequent grid will contain the previous one as the top left panel. You have to resize your images and such and maybe even avoid loading the images multiple times etc. But that is another question. This example shows 5 nested panels.
Just to be clear, you should use ImageIO to load the images once and reuse the images. For example You can create a BufferedImage like this in your main class:
BufferedImage mointainImg = ImageIO.read(new File("pictures/mountain.gif"));
And when you want to create the JLabel you can do this:
mountain = new JLabel(new ImageIcon(mountainImg));
And the advantage is that you can manipulate the image a bit if you want.
One issue that you have is that the images are not scaled. To scale images, use Image.getScaledInstance(). Proper scaling would at least fix the problem of the visible images being cut off. It also might cause the other images to be shown as they might just be hiding behind the visible images because they are too big.

center a panel in a box layout row in java

I have this code below to create a page inside of a tab.
I want each layout in one row of the overall box layout but i want the elements to stay in their original size and not expand to fill the width of the overall window. does anyone know what lines of code i need to change or what is the best way of doing this?! The image attached shows what it looks like at the moment
public void createPage4() {
panel4 = new JPanel();
panel4.setLayout(new BoxLayout(panel4, BoxLayout.Y_AXIS));
navigatePanel = new JPanel();
navigatePanel.setLayout(new BoxLayout(navigatePanel, BoxLayout.X_AXIS));
previousButton.setText("Previous");
previousButton.setEnabled(false);
navigatePanel.add(previousButton);
navigatePanel.add(Box.createHorizontalStrut(10));
indexTextField.setHorizontalAlignment(JTextField.CENTER);
navigatePanel.add(indexTextField);
navigatePanel.add(Box.createHorizontalStrut(10));
ofLabel.setText("of");
navigatePanel.add(ofLabel);
navigatePanel.add(Box.createHorizontalStrut(10));
maxTextField.setHorizontalAlignment(JTextField.CENTER);
maxTextField.setEditable(false);
navigatePanel.add(maxTextField);
navigatePanel.add(Box.createHorizontalStrut(10));
nextButton.setText("Next");
nextButton.setEnabled(false);
navigatePanel.add(nextButton);
panel4.add(navigatePanel);
displayPanel = new JPanel();
displayPanel.setLayout(new GridLayout(5, 2, 4, 4));
firstNameLabel.setText("First Name:");
displayPanel.add(firstNameLabel);
displayPanel.add(firstNameTextField);
lastNameLabel.setText("Last Name:");
displayPanel.add(lastNameLabel);
displayPanel.add(lastNameTextField);
panel4.add(displayPanel);
}
image
BoxLayout accepting Min, Max and PreferredSize that came from JComponents
I want each layout in one row of the overall box layout but i want the elements to stay in their original size and not expand to fill the width of the overall window
I'd be to use proper LayoutManager, FlowLayout accepting only PreferredSize, and/or all JComponents layed by GridBagLayout without defininitions of GridBagConstraints stays unchanged on containers resize
doesn't make me sence (my view) for why reason (sure this is your job), but for better help sooner post an SSCCE
The easiest way is to add your panel4 to an other panel that uses GridBagLayout and then add that panel to the container. Then it will be centered and nothing will stretch on resize.
JPanel centeredPanel = new JPanel(new GridBagLayout());
centeredPanel.add(panel4); // add this panel to the container
You should also construct the textfields with a specified number of columns, like
indexTextField = new JTextField(20);

Categories