How to collect JScrollPanes into a Container variable? (urgent...)
Please help me in following problem:
I can collect succesfully one JScrollPane into a Container variable allComponents and then show this Container in application window. However, I would like to collect several JScrollPanes into this same Container variable but it does not work. Should I use some kind of add command?
I tried something like
allComponents.add(jspane);
but I got error code "variable allComponents might not have been initialized" allthough I have tried my best to initialize it.
Thanks for your kind help!
Snippets from my program:
class SwingApplication implements ActionListener
{
...
public Container createComponents()
{
Container allComponents;
...
jspane = new JScrollPane(panel);
jspane2 = new JScrollPane(cards);
allComponents = jspane;// THIS WORKS BUT I WOULD LIKE TO COLLECT BOTH jspane AND jspane2 WITH ADD COMMAND
return allComponents;
}
...
class DragImage
{ public static void main(String[] args)
{
JFrame frame = new JFrame("SwingApplication");
SwingApplication app = new SwingApplication();
Container contents = app.createComponents();
frame.getContentPane().add(contents, BorderLayout.CENTER);
...
Message was edited by:
wonderful123
# 1
What do you mean by Colletc?
You mead ADD JScrollPanes to a Container?
Do you have some JScrollPanes with some components and you would like to add them to a Container? Is that right?
PS.
Please use the code format tag to show your code:
class SwingApplication implements ActionListener
{
...
public Container createComponents()
{
Container allComponents;
...
jspane = new JScrollPane(panel);
jspane2 = new JScrollPane(cards);
allComponents = jspane; // THIS WORKS BUT I WOULD LIKE TO COLLECT BOTH jspane AND jspane2 WITH ADD COMMAND
return allComponents;
}
...
class DragImage
{ public static void main(String[] args)
{
JFrame frame = new JFrame("SwingApplication");
SwingApplication app = new SwingApplication();
Container contents = app.createComponents();
frame.getContentPane().add(contents, BorderLayout.CENTER);
# 3
THanks for your interest!!
Perhaps I give the whole source code
The problem lies in the following place:
allComponents = jspane;
return allComponents;
}
I would like to use some kind of command
allComponents.add(jspane);
but it does not seem to work. I get the error "variable allComponents might not be initialized".
The program should print images in two rows. With my current knowledge i can only print one row of images.
Thanks!
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import javax.swing.*;
class SwingApplication implements ActionListener
{
// extends JScrollPane
private JScrollPane jspane;
private JScrollPane jspane_n;
private JPanel panel;
private JLabel label;
private Icon icon;
private JPanel panel2;
private JLabel label2;
private Icon icon2;
private JPanel panel_n;
private JLabel label_n;
private Icon icon_n;
private JTextField testText;
private int k;
private static String labelPrefix = "Testing: ";
private int numClicks = 0;
final JLabel label_testing = new JLabel(labelPrefix + "nothing");
JPanel cards; //a panel that uses CardLayout
final String BUTTONPANEL = "JPanel with JButtons";
final String TEXTPANEL = "JPanel with JTextField";
public Container createComponents()
{
Container allComponents;
icon = new ImageIcon("kirjain_a.gif");
label = new JLabel();
label.setIcon(icon);
icon2 = new ImageIcon("kirjain_b.gif");
label2 = new JLabel();
label2.setIcon(icon2);
icon_n = new ImageIcon("numero_1.gif");
label_n = new JLabel();
label_n.setIcon(icon_n);
label.setTransferHandler(new ImageSelection());
label2.setTransferHandler(new ImageSelection());
label_n.setTransferHandler(new ImageSelection());
MouseListener mouseListener = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
JComponent sourceEvent = (JComponent)e.getSource();
// Object sourceEvent = whatHappened.getSource();
if (sourceEvent == label)
{ numClicks++;
label_testing.setText(labelPrefix + numClicks);
}
if (sourceEvent == label2)
{
numClicks--;
label_testing.setText(labelPrefix + numClicks);
}
JComponent comp = (JComponent)e.getSource();
TransferHandler handler = comp.getTransferHandler();
handler.exportAsDrag(comp, e, TransferHandler.COPY);
}
};
label.addMouseListener(mouseListener);
label2.addMouseListener(mouseListener);
label_n.addMouseListener(mouseListener);
panel = new JPanel();
// panel.setLayout(new GridLayout(1,3));
panel.add(label);
panel.add(label2);
panel_n = new JPanel();
panel_n.add(label_n);
//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(panel_n, BUTTONPANEL);
cards.add(panel_n, TEXTPANEL);
panel.add(label_testing);
jspane = new JScrollPane(panel);
jspane_n = new JScrollPane(cards);
//jspane.setLayout(new GridLayout(3,2));
//jspane.getViewport().add(panel);
//jspane_n.getViewport().add(cards);
//allComponents.add(panel, BorderLayout.PAGE_START);
//allComponents.add(cards, BorderLayout.CENTER);
//allComponents.add(jspane);
//allComponents.add(jspane_n);
allComponents = jspane;
return allComponents;
}
//label.addActionListener(this);
//k=0;
public void actionPerformed(ActionEvent whatHappened)
{ Object sourceEvent = whatHappened.getSource();
if (sourceEvent == label)
{ numClicks++;
label_testing.setText(labelPrefix + numClicks);
}
//repaint();
}
} // end class OwnPanel
class CloserClass extends WindowAdapter
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
}
class DragImage
{ public static void main(String[] args)
{
JFrame frame = new JFrame("SwingApplication");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SwingApplication app = new SwingApplication();
Container contents = app.createComponents();
frame.getContentPane().add(contents, BorderLayout.CENTER);
//Display the window.
//addWindowListener(new CloserClass());
frame.pack();
frame.setVisible(true);
}
} // end class DragImage
# 6
Hello!
When looking some examples I just found that I my problems get solved if I make some changes and begin to use following structures:
public void addComponentToPane(Container allComponents)
{
...
allComponents.add(panel, BorderLayout.PAGE_START);
allComponents.add(cards, BorderLayout.CENTER);
allComponents.setVisible(true);
}
...
class DragImage
{ public static void main(String[] args)
{
JFrame frame = new JFrame("SwingApplication");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SwingApplication app = new SwingApplication();
app.addComponentToPane(frame.getContentPane());
...
It seems that Java method
addComponentToPane()
is quite essential.
I am so happy that this hard problem seems now to be solved.
Thanks for all help that I recieved from you!
-Wonderful123