In my software, I have a ScrollPane whose view has a Box Layout and holds JPanels. A button allows to add a JPanel to that Box, and i'd like to find a way to place it in a certain position.
Here is the example code:
public class MyClass {
//run a swing App
public static void main(String[] args) throws IOException {
SwingUtilities.invokeLater(new ScrollPaneExample());
}
}
class ScrollPaneExample implements Runnable{
private Box boxHolder;
private JPanel scrollPaneContainer;
//show a JFrame
@Override
public void run(){
JFrame mainFrame = initFrame();
mainFrame.setPreferredSize(new Dimension(300, 200));
mainFrame.setLocationRelativeTo(null);
mainFrame.pack();
mainFrame.setVisible(true);
}
//init JFrame and add to it a scrollpane and a button
private JFrame initFrame() {
JFrame mainFrame = new JFrame("MyFrame");
mainFrame.setLayout(new BorderLayout());
mainFrame.getContentPane().add(initScrollPane(),BorderLayout.CENTER);
mainFrame.getContentPane().add(initButtonAdd(),BorderLayout.SOUTH);
return mainFrame;
}
//init scrollpane which contains a boxholder for colored panels
private Component initScrollPane() {
scrollPaneContainer = new JPanel( new BorderLayout() );
boxHolder = Box.createVerticalBox();
scrollPaneContainer.add(boxHolder, BorderLayout.PAGE_START);
return new JScrollPane(scrollPaneContainer);
}
//init a button which add a panel to the boxholder on pressed
private Component initButtonAdd() {
JButton button = new JButton("addPanel");
button.setBackground(Color.green);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
boxHolder.add(createPanel());
scrollPaneContainer.revalidate();
}
});
return button;
}
//create a panel setting its background to a random color
private Component createPanel() {
JPanel panel = new JPanel();
panel.setBackground(randomColor());
panel.setPreferredSize(new Dimension(100,50));
panel.addMouseListener(new MouseListener() {
public void mousePressed(MouseEvent e) {
/** TODO code to replace the clicked panel with a new one*/
}
@Override public void mouseClicked(MouseEvent e) {}
@Override public void mouseReleased(MouseEvent e) {}
@Override public void mouseEntered(MouseEvent e) {}
@Override public void mouseExited(MouseEvent e) {}
});
panel.add(new JLabel("a colored Panel"));
return panel;
}
//generate a randomColor
private Color randomColor() {
Random rand = new Random();
float r = rand.nextFloat() / 2f ;
float g = rand./***/nextFloat() / 2f;
float b = rand.nextFloat() / 2f;
Color randomColor = new Color(r, g, b);
return randomColor;
}
}
I'd like to do something like:
int indexPosition = 2;
boxHolder.add(createPanel(), indexPosition);
UPDATE 1-2 First of all: thanks. I noticed that the example code provided does not reflect properly my code, and i have lost the objective while explaining my problem. I'll keep the previous question as it is since it can be usefull to someone else. Let's move on.
I need to replace a certain panel, so, what we did before with a given index, should be implemented with someting more dynamic. i've added a mouseListener to our Jpanels. On click, they should be replaced by a new panel. Something like:
JPanel replacePanel = createPanel();
// next istruction leads to a compilation error since Box do not has this method
int indexOfClickedPanel= boxHolder.indexOf(this);
boxHolder.add(replacePanel,indexOfClickedPanel);
In my code i use a JLabel instead of a button. Its icon changes on mouseEntered/Exited, and add a certains panel onPressed. It sounds to me more appropriate, any suggestion will be appreciated btw.
MouseListener
, add anActionListener
to yourButton
. - trashgod