Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JScrollPane Problem  RSS feed

 
Shalabh soni
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Scrollbar Does not appear properly when i run this code. It only appears when i decrease the size of the Frame. Please help. The Component is cut from top and bottom at other times.


import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;

public class Demo extends JFrame
{
public static void main(String args[])
{
Demo a=new Demo();

}


Demo()
{
super("Demo");
setVisible(true);
Dimension dim=new Dimension(1099,800);

JPanel MainPanel1=new JPanel();
MainPanel1.setBorder(new BevelBorder(BevelBorder.RAISED));
MainPanel1.setPreferredSize(new Dimension(dim.width-400,500));
JLabel questions[]=new JLabel[40];
int chapters=40;
int i;
JLabel chaptername[]=new JLabel[40];JLabel[] Section=new JLabel[40];
GridBagLayout gridbag=new GridBagLayout();

GridBagConstraints Constraints=new GridBagConstraints();
Insets insets;
MainPanel1.setLayout(gridbag);
Constraints.gridy=0;
int c=0;
for(i=0;i<chapters;i++)
{
c++;
questions[i]=new JLabel(i+"");
chaptername[i]=new JLabel(i+"");
Section[i]=new JLabel(i+"");
Constraints.gridx=0;
Constraints.gridy=Constraints.gridy+1;
insets=new Insets(0,0,0,10);//Constraints.gridwidth=3;
Constraints.insets=insets;Constraints.fill=GridBagConstraints.NONE;
JLabel ChName=new JLabel("Enter the Name for Chapter : "+(i+1));

ChName.setMinimumSize(ChName.getPreferredSize());
//Constraints.weightx=1;
gridbag.setConstraints(ChName,Constraints);
MainPanel1.add(ChName);

Constraints.fill=GridBagConstraints.NONE;
Constraints.gridx=1;
chaptername[i].setMinimumSize(chaptername[i].getPreferredSize());
Constraints.gridy=Constraints.gridy;
insets=new Insets(0,0,10,10);
Constraints.insets=insets;
//Constraints.weightx=1;
gridbag.setConstraints(chaptername[i],Constraints);
MainPanel1.add(chaptername[i]);


Constraints.gridx=2;
Constraints.gridy=Constraints.gridy;
Constraints.fill=GridBagConstraints.NONE;
insets=new Insets(0,0,0,10);
Constraints.insets=insets;
Section[i].setMinimumSize(Section[i].getPreferredSize());
//Constraints.weightx=5;
gridbag.setConstraints(Section[i],Constraints);
MainPanel1.add(Section[i]);

Constraints.gridx=3;
Constraints.gridy=Constraints.gridy;
Constraints.fill=GridBagConstraints.NONE;
insets=new Insets(0,0,0,30);
Constraints.insets=insets;
//Constraints.weightx=1;
questions[i].setMinimumSize(questions[i].getPreferredSize());

gridbag.setConstraints(questions[i],Constraints);
MainPanel1.add(questions[i]);



Constraints.gridy++;


}


JScrollPane And =new JScrollPane(MainPanel1);

And.getViewport().add(MainPanel1);
add(And);
}
}
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, use code tags. You can edit your post to add them. Don't forget to add proper indentation as well.

Try to add "pack();" at the end of your constructor. Also check out the Javadoc of java.awt.Window what that method does.
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You where asked to use the "Code" tags when you originally posted this question on the Sun forums. Why do you think it would be any different here? If you want help then post readable code.
 
Shalabh soni
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried Using pack as well but there is nothing productive coming up .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!