• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

adding a JScrollPane to a dynamic user interface?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a dynamic user iterface, where the JTextFields are added dynamically to a JPanel within the JFrame upon the click of a JButton. How do I use the JScrollPane so that the Vertical Scroll bar shows up for the JPanel as soon added TextFields go out of range? Any ideas/piece of code to help me out will be of great help!
Regards
-Deepak
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create a JScrollPane pass the panel where JTextField is added. Add the JScrollPane to the Frame.
JScrollPane pane = new JScrollPane(<panel>);
frame.getContentPane().add(<pane>);
 
Javaoops
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create a JScrollPane by passing the panel to which the JTextfields is added. Then add the JScrollPane to the Frame.
JScrollPane pane = new JScrollPane(panel);
frame.getContentPane().add();
 
Javaoops
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

frame.getContentPane().add(pane);
 
Deepak Raj
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajan, I tried what you said but it didn't seem to work. I am adding textfields dynamically to the Panel. The problem lies in showing the Vertical scroll bar once the text fields in the JPanel exceeds the Panel's size. The scrolls don't seem to show up. Here is part of my code where the problem might lie.
public Scenario() // Constructor
{
textPanel = new JPanel();
textPanel.setLayout(null);
textPanel.setSize(508, 520);
textPanel.setLocation(10,10);
//..........lines of code
//.............
}
public void addComponent(String text)
{
j = j + 30;
i = i+1;
String lineno = Integer.toString(i) + ".";
field = new JLabel(lineno);
field.setBounds(90,j,20,24);
textPanel.add(field);

if (addline){
content = new JTextField(text);
content.setBounds(108,j,400,24);
content.setBackground(new Color(255,255,255));
textPanel.add(content);
addline = false;
invscenario = false;
}
else if(invscenario) {
content = new JTextField(text);
content.setBounds(108,j,50,24);
content.setEditable(false);
textPanel.add(content);
try{
if (content.getText().length()!= 0)
{
Statement statement1 = conn.createStatement();
String query1 = "SELECT * FROM scenario " + "WHERE scenario_num LIKE '" + text + "'";
rs = statement1.executeQuery(query1);
rs.next();
int recordNumber = rs.getInt(1);
if(recordNumber != 0)
{
labelcontent = new JLabel(rs.getString(3));
labelcontent.setBounds(165,j,100,24);
textPanel.add(labelcontent);
}
}
}// end try
catch(SQLException e)
{
System.out.println(e);
}
invscenario = false;
addline = false;
}
scrollPane = new JScrollPane(textPanel);
scrollPane.add(textPanel);
contentPane.add(scrollPane);
contentPane.repaint();
show();
}

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic