PDA

View Full Version : layout Problem



neo_philiac
05-06-2010, 04:13 PM
Hi,

I am trying to make thumbnail viewer and having problem putting the thumbnail in the proper place.

All I am trying to do is this:


iconPanel.add(iconBar, BorderLayout.NORTH);
for (int x = 0; x < 4; x++) {
iconBar.add(emptyThumbnail,BorderLayout.CENTER);
repaint();
validate();
}


Ant then whenever I scroll,


for (k = index; k < indexLimit; k++) {
JButton bt = new JButton(new ImageIcon(tmpImg));
//Add action listener to button
bt.addActionListener(this);
bt.setActionCommand(Integer.toString(k));
iconBar.add(bt, BorderLayout.CENTER);
}

But its not working, I dont see only one empty thumbnail at the start and on scroll i see 4 at time(which is fine!). But can you suggest a better layout strategy?

Thanks

Here is a sample code:

Yes the empty thumbnails are place holders.
And here is the code.(lets forget about the mouseWheel stuff for now)


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package desktopapplication2;

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

/* FrameDemo.java requires no other files. */
public class imageLoad {

/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static JPanel iconPanel = new JPanel();
private static JPanel iconBar = new JPanel();
private static JButton emptyThumbnail = new JButton();

private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Add the two basic panels to the main panel
iconPanel.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, (new Color(169, 218, 146))));
iconPanel.setBackground(new Color(218, 235, 201));

// Lets set the empty icon thumbnails.
emptyThumbnail.setPreferredSize(new Dimension(160, 120));
emptyThumbnail.setBackground(new Color(207, 207, 207));
emptyThumbnail.setBorder(BorderFactory.createLineBorder(new Color(156, 156, 156)));


// We add two glue components. Later in process() we will add thumbnail buttons
// to the toolbar inbetween thease glue compoents. This will center the
// buttons in the toolbar.
iconBar.setBackground(new Color(218, 235, 201));
iconBar.setPreferredSize(new Dimension(788, 140));
iconBar.add(Box.createGlue());
iconBar.add(Box.createGlue());

for (int x = 0; x < 4; x++) {
iconBar.add(emptyThumbnail);
}
iconBar.repaint();
// 1.Add buttonbar
iconPanel.add(iconBar, BorderLayout.NORTH);

frame.getContentPane().add(iconPanel, BorderLayout.CENTER);



//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {
createAndShowGUI();
}
});
}
}