Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding a JScrollPane to a JInternalFrame

Status
Not open for further replies.

pipk

Programmer
Feb 20, 2001
455
GB
Greetings.

Does anyone out there know how to correctly add a JScrollPane to a JInternalFrame?
I am currently having a bit of trouble.

I am using an anonymous inner class to create a JInternalFrame (to add to my DesktopPane), things is, I am trying to add a user defined class (extending a JPanel) to the frame when I create it. This user defined class is supposed to create a JScrollPane, but it just doesn't seem to be appearing - the frame stays blank!, any clues?
When i remove the JScrollPane bit and just add the buttons direct to the panel of the user defined class, the buttons appear - but I need them in a scrollpane.

If you need a copy of the classes, I can post them.

thanks in advance

pipk;-)
 
post them :) If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Cheers for the interest Leon.

Here is my inner class that creates the Internal Frame. As far as I can tell, this is fine.

selectLog.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e)
{
JInternalFrame frame = new JInternalFrame(
"Select Diver's Log from List", true, true, true, true);

Container p = frame.getContentPane();

try{
DiverNameSelection panel = new Diver NameSelection (connectDBase);

p.add(panel, BorderLayout.CENTER);
frame.setSize( 400, 400);
frame.setOpaque(true);
theDeskTop.add(frame);
}
catch(SQLException sqlex){
sqlex.printStackTrace();
}
}
}
);

No problem there, but it is my Userdefined class DiverNameSelection that doesn't seem to want me to add a JScrollPane to the panel it extends from. I am quite sure that it is my complete lack of understanding of JScrollPane that is the cause of the problem.

I have posted only the constructor as this is obviously where the problem lies for me - so yes I do have an actionPerformed method further on.

// Diver Name Selection Class

import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class DiverNameSelection extends JPanel implements ActionListener
{
private JTextArea output;
private Vector diverNames;
private Connection connection;
private ResultSetMetaData rsmd;
private JPanel namePanel;
private JButton diverNameButtons[];
private String divers[], divers1[], divers2[], tokenArray[];
private String testString, testString1, testString2, testString3, testString4;
private JScrollPane scroller;

public DiverNameSelection(Connection c) throws SQLException
{
connection = c;

setLayout(new GridLayout(0, 1));

getDiverNames();
namePanel = new JPanel();
namePanel.setLayout(new GridLayout(0, 1));
//JScrollPane scroller = new JScrollPane();
divers = new String[diverNames.size()];

testString = diverNames.toString();
formatDiverNames();

diverNameButtons = new JButton[divers.length];

for(int i = 0; i < divers.length; i++){
diverNameButtons = new JButton(divers);
diverNameButtons.addActionListener(this);
//scroller.add(diverNameButtons);
namePanel.add(diverNameButtons);
}
//namePanel.add(scroller);

add(namePanel);
}

This version works fine and adds the buttons to the panels. But an earlier version was that I was also creating a JScrollPane and trying to add thebuttons to this, and then adding the scrollpane to the Panel itself. (see comments).
Any suggestions (that don't involve me puting myself out of my own misery!!!?)

p.s.

I have a great book for learning java, but am deplete of a really decent (i.e. comprehensive and detailed, and covering SWING in depth) reference book, suggestions on this would be welcome also.
 
Hi,

I didn't have the time to seriously take a look through your program. But I have created a simpler program for you as a reference.

Sorry about the messy codes, I am in a rush for time.

import java.awt.*;
import javax.swing.*;
public class testFrame extends JFrame
{
InternalPanel internalPanel;
JInternalFrame jif;
public testFrame()
{
getContentPane().setLayout(null);
setSize(200,200);
setResizable(false);

jif = new JInternalFrame();
internalPanel = new InternalPanel();

JScrollPane jsp = new JScrollPane(internalPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jsp.setBounds(new Rectangle(10,10,100,100));

jif.getContentPane().setLayout(null);
jif.setSize(200,200);
jif.getContentPane().add(jsp, null);

jif.setBounds(new Rectangle(10,10,150,150));
jif.show();

getContentPane().add(jif, null);
}

class InternalPanel extends JPanel
{
public InternalPanel()
{
setSize(100,100);

JButton b = new JButton(&quot;veryveryveryverylongtext&quot;);
add(b);
}
}

public static void main(String[] args)
{
testFrame tf = new testFrame();
tf.show();
}
}

Regards,
Leon
(p.s. I will take a look at your program later on.) If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Many thanks Leon, I shall let you know the outcome.

pipk
 
Hi pipk,

I am not sure if this is what you want but I have made a little alteration to your program. I notice some common mistakes that you have made.

When you are declaring your JButtons in the for loop, you must do this

diverNameButtons = new JButton(divers);

instead of

diverNameButtons = new JButton(divers);

You can't declare a JButton with an Array of String as input and then assign the Jbutton to an Array of Jbutton without specifying the index. Here is the changed codes:-

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class testFrame extends JFrame
{
DiverNameSelection internalPanel;
JInternalFrame jif;
public testFrame()
{
getContentPane().setLayout(null);
setSize(200,200);
setResizable(false);

jif = new JInternalFrame();
internalPanel = new DiverNameSelection();

JScrollPane jsp = new JScrollPane(internalPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jsp.setBounds(new Rectangle(10,10,100,100));

jif.getContentPane().setLayout(null);
jif.setSize(200,200);
jif.getContentPane().add(jsp, null);

jif.setBounds(new Rectangle(10,10,150,150));
jif.show();

getContentPane().add(jif, null);
}

class InternalPanel extends JPanel
{
public InternalPanel()
{
setSize(100,100);

JButton b = new JButton(&quot;veryveryveryverylongtext&quot;);
add(b);
}
}

public class DiverNameSelection extends JPanel implements ActionListener
{
private JTextArea output;
//private Vector diverNames;
//private Connection connection;
//private ResultSetMetaData rsmd;
private JPanel namePanel;
private JButton diverNameButtons[];
private String divers[], divers1[], divers2[], tokenArray[];
private String testString, testString1, testString2, testString3, testString4;
//private JScrollPane scroller;

public DiverNameSelection()
{
setLayout(new GridLayout(0, 1));

//getDiverNames();
namePanel = new JPanel();
namePanel.setLayout(new GridLayout(0, 1));
//JScrollPane scroller = new JScrollPane();
divers = new String[3];
divers[0] = &quot;Diver 1&quot;;
divers[1] = &quot;Diver 2&quot;;
divers[2] = &quot;Diver 3&quot;;

//testString = diverNames.toString();
//formatDiverNames();

diverNameButtons = new JButton[divers.length];

for(int i = 0; i < divers.length; i++){
diverNameButtons = new JButton(divers);
diverNameButtons.addActionListener(this);
//scroller.add(diverNameButtons);
namePanel.add(diverNameButtons);
}
//namePanel.add(scroller);

add(namePanel);
}

public void actionPerformed(ActionEvent e)
{
}
}

public static void main(String[] args)
{
testFrame tf = new testFrame();
tf.show();
}
}

Regards
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Many thanks Leon, your help is appreciated.

Speaking of which I am not too sure how I made it onto the &quot;Forums top experts list&quot; - as my Java knowledge is fairly basic.

 
Well, I don't think I have great knowledge on Java either but somehow I am on the list too? haha... :D If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top