Second row of gridbaglayout scrolling out of container - java

I am trying to achieve a layout similar to that of a carousel. It needs to have images added horizontally with a checkbox field in the second row. I have a panel within a jscrollpane and individual images are added to the panel as labels. Please see screen shot.
screenshot
When I scroll the pane , the first row containing the images stays well within the panel..but if you notice the second row of checkboxes , it scrolls out of the panel. Here is the code ...
JLabel lab1=new JLabel();
for (int ii=0; ii<imageFiles.length; ii++) {
GridBagConstraints constraint = new GridBagConstraints();
lab1 = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
constraint.gridx = ii;
constraint.gridy =0;
jPanel9.add(lab1,constraint);
}
for (int ii=0; ii<imageFiles.length; ii++) {
GridBagConstraints constraint1 = new GridBagConstraints();
constraint1.anchor = GridBagConstraints.SOUTH;
chkbox = new Checkbox("asdasdada");
constraint1.gridx = ii;
constraint1.gridy =1;
jPanel9.add(chkbox, constraint1);
}
Not sure what is wrong..Any help is much appreciated..Thanks..

The problem is that you are mixing AWT components (heavyweight) with Swing components (lightweight). I have 2 recommendations:
Don't mix heavyweight and lightweight components
Try to use lightweight components as much as possible
So in your code, replace Checkbox by JCheckbox and it should work just fine.

Related

jscrollpane slow with inital viewing of complex image

I am having problems with scrolling on a JScrollPane.
I have a list of images in a JList which I am placing on a JScrollPane. The list can be very long, sometimes hundreds or thousands of images. I add the JScrollPane to a JPanel, and the image gets drawn successfully, the JScrollPane adds scroll bars when needed and the scroll bars can move the image. The response from the scrollbars are good if the list of images is simple (10 - 20).
However, lists that run into the hundreds cause the vertical scrolling to initially run very slowly until all the images have been shown, then the scrolling speed returns to normal.
Can anyone suggest a technique to speed the initial scrolling response? I have experimented with adjusting the viewport settings (eg JViewport.BACKINGSTORE_SCROLL_MODE, JViewport.BLIT_SCROLL_MODE) but these did not lead to any improvement in the initial scrolling response.
The code that I am using is represented below.
prList = new JList(prListModel); // a list of interactive images for the JScrollPane
MouseAdapter listener = new PrChangeListener(prList, this);
prList.addMouseListener(listener);
prList.addMouseMotionListener(listener);
renderer = new PrRenderer();
renderer.setPreferredSize(getIconPreferredSize());
prList.setCellRenderer(renderer);
prList.setBackground(Color.WHITE);
prList.setSelectionBackground(new Color(225,225,225));
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
//try separate linked scroll bar for scale bar ...
prListModel scaleBarListModel = new prListModel();
scaleBarListModel.addElement(intArray[0]); // a scale bar to sit above the JScrollPane
scaleBarList = new JList(scaleBarListModel);
scaleBarList.setCellRenderer(renderer);
scaleBarList.setBackground(Color.WHITE);
scaleBarList.setSelectionBackground(Color.WHITE);
scaleBarScrollPane = new JScrollPane(scaleBarList,JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
prListModel.removeElementAt(0);
prScrollPane = new JScrollPane(prList);
prScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
prScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scaleBarScrollPane.getHorizontalScrollBar().setModel(prScrollPane.getHorizontalScrollBar().getModel());
JPanel twoPanePanel= new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.VERTICAL;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx=0;
gbc.gridy=0;
gbc.weightx=1;
gbc.weighty=0.1;
twoPanePanel.add(scaleBarScrollPane, gbc);
gbc.gridx=0;
gbc.gridy=1;
gbc.weighty=0.9;
twoPanePanel.add(prScrollPane,gbc);
add(twoPanePanel);

For loop to create button array not displaying properly in JPanel

So i'm creating an array of buttons that is supposed to display and 8,8 grid, instead it displays very small buttons spreading across the window (31 buttons in a row for two rows then two more buttons on the third). If I replace:
gamePanel1.add(buttons[a][b]);
with:
frame.add(buttons[a][b]);
... it display correctly but when initialising the array, I have to resize the window to see the buttons as it does not fit to contents.
Here is the code to create the buttons:
contentPane.setLayout(new BorderLayout());
JPanel gamePanel1 = new JPanel();
buttons = new JButton[boardsize][boardsize];
mineBoard = new int[9][9];
for (int a = 0; a < boardsize; a++)
for (int b = 0; b < boardsize; b++) {
buttons[a][b] = new JButton("");
buttons[a][b].setBounds(30+gridsize*a,30+gridsize*b,gridsize,gridsize);
gamePanel1.add(buttons[a][b]);
buttons[a][b].addMouseListener(new MouseListener(a,b));
setx(a);
sety(b);
settried(false);
setmine(false);
}
contentPane.add(gamePanel1, BorderLayout.CENTER);
Can anyone tell me how I might fix this or show me how with this code I may use a different layout - i tried grid layout for the buttons but could not get it working at all.
First create a Panel as:
JPanel panel=new JPanel();
Then set the layout as
panel.setLayout(new GridLayout(8,8));
Then using a for loop create and add the buttons and the buttons will be displayed in eight by eight grid. Thanks.

JList border disappears on scrolling

I've five lists (enclosed in a jscrollpane) added to a jpanel in a group layout. The problem with the lists is that when a scrollbar appears automatically, the border on the bottom/top disappears (lists 2,3,4). How do I make sure that all lists look the same w.r.t border just like the first/last lists?
I've tried setting viewportborder using setViewPortBroder, but it doesn't change things much. 2,3,4 appear with light border while the rest of the lists have double borders.
EDIT
Adding code sample:
Each list you see is created using this code:
MyJList jList = new MyJList(value);
jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jList.setModel(listModel);
jList.setMinimumSize(new Dimension(135, 300));
jList.setMaximumSize(new Dimension(135, 300));
jList.clearSelection();
jList.setSelectionBackground(Color.darkGray);
jList.setSelectionForeground(Color.white);
jList.setBorder(new LineBorder(Color.darkGray, 1));
jList.setFixedCellHeight(30);
jList.setFixedCellWidth(100);
Font font = jList.getFont();
jList.addListSelectionListener(new ListListener());
return jList;
MyList is an extension of JList. It does nothing special, other than storing some domain related metadata. And, then lists are added to the middle panel like this:
private void layoutLists(JLabel[] labels, JList[] lists) {
panel.removeAll();
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
JScrollPane[] jScrollPanes = new JScrollPane[lists.length];
for (int index = 0; index < lists.length; index++) {
jScrollPanes[index] = new JScrollPane(lists[index]);
jScrollPanes[index].setBorder(BorderFactory.createEmptyBorder());
jScrollPanes[index].setMinimumSize(new Dimension(135, 300));
jScrollPanes[index].setMaximumSize(new Dimension(135, 300));
jScrollPanes[index].setPreferredSize(new Dimension(135, 300));
}
for (int index = 0; index < labels.length; index++) {
gbc.gridx = index;
gbc.gridy = 0;
gbc.insets = new Insets(8, 8, 8, 8);
panel.add(labels[index], gbc);
gbc.gridy = 1;
if (index == labels.length - 1) {
gbc.insets = new Insets(8, 8, 8, 13);
}
panel.add(jScrollPanes[index], gbc);
}}
The explanation of the top/bottom part of the inner (JList) border not being visible is that ... they are not visible if the vertical scrollBar appears (the list is scrolled off)
If you insist, either:
switch the borders - empty on the list itself and lineBorder on the scrollPane or
set the viewportBorderProperty of the scrollPane to the lineBorder
Beware: it's not recommended to fiddle with the default LAF settings - where-to or not the components have a border should be left to the ui to guarantee consistent visuals across your application. Nor does it look exactly good to have the left border line beside (either outside or inside) the scrollPane's vertical scrollbar.
Edit
Just noticed that you already tried the second option (and are not satisfied with the result :-) But then: where do you want the vertical border line if the scrollBar is visible? Anyway, back to the beware: the outcome is highly LAF dependent ...

JScrollPane not scrolling when a JPanel is added

I was hoping someone would be able to help. This seems like it should be a simple problem but for the life of me I can't work it out.
Problem: I am creating a JPanel that is made up of panels containing 5 labels each with ImageIcons. [sounds confusing]
I am then adding this panel to a JScrollPane. But when it is displayed the images are showing and correctly placed but I am unable to scroll down to see the panels that are off the screen.
here is a screenshot: http://img841.imageshack.us/img841/36/screenshot20120510at160.png
Here is the snippet of code I am using to populate the panels and add the JScrollPane.
private void setSeriesViewContainer(){
container = new BackgroundPanel(backGround, BackgroundPanel.TILED);
//container.setPreferredSize(new Dimension(650,500));
container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
FlowLayout flowLayout = new FlowLayout();
JPanel[] jp = new BackgroundPanel[10];
for (int i = 0; i < jp.length; i++) {
jp[i] = new BackgroundPanel(backGround, BackgroundPanel.TILED);
jp[i].setLayout(flowLayout);
for (int j = 0; j < 10; j++) {
jp[i].add(new JLabel(new ImageIcon(getClass().getResource("/placeHolder.png"))));
}
}
for (int i = 0; i < jp.length; i++) {
container.add(jp[i]);
}
public void init(){
seriesViewContainer = new javax.swing.JScrollPane(container);
seriesViewContainer.setBorder(null);
seriesViewContainer.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
seriesViewContainer.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
seriesViewContainer.setPreferredSize(new java.awt.Dimension(700, 300));}
I have searched around for the solution but have not come up with one as yet.
container.setPreferredSize(new Dimension(x,y)); the dimensions of container should be larger than the dimensions of the scrollpane.
from what I've read setPreferredSize() is not a good thing to use though. The problem is probably the LayoutManager for container or jp.
same problem here: Java Swing: JScrollPane not working
Have you tried to call revalidate() to the JScrollPane and/or container after each add ?

GridBagLayout padding

I am using a GridBagLayout to (currently) display two rows. I am aware this layout is overkill for this task, but am trying to learn how to use it. The problem is that I have added the two panels to the two separate rows and there is a huge gap around the content (see image and code below):
alt text http://www.imagechicken.com/uploads/1264533379009864500.png
Image background;
public Table(){
super();
ImageIcon ii = new ImageIcon(this.getClass().getResource("pokerTableV2.png"));
background = ii.getImage();
setSize(Constants.FRAME_WIDTH, Constants.TABLE_HEIGHT);
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.fill = GridBagConstraints.HORIZONTAL;
JButton button = new JButton("hello world");
JPanel panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(800,100));
panel1.add(button, BorderLayout.CENTER);
panel1.setBackground(Color.yellow);
add(panel1, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
JPanel middlePanel = new JPanel();
middlePanel.setPreferredSize(new Dimension(800,350));
middlePanel.add(button, BorderLayout.CENTER);
middlePanel.setBackground(Color.blue);
add(middlePanel, constraints);
}
Use
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1d;
constraints.weighty = 1d;
JavaDoc for weightx/weighty says:
Specifies how to distribute extra horizontal/vertical space.
JavaDoc for fill:
This field is used when the component's display area is larger
than the component's requested size. It determines whether to
resize the component, and if so, how.
Unfortunately, with GridBagLayout, if the contents do not fill the entire container that it is in, it will automatically center all its contents within its container. That is why you are getting a really large gap.
There are essentially two ways to fix this:
The hard way: Fiddle with the GridBagConstraints. I had limited success with this when trying to avoid the centre-ing behaviour.
The easy way: Put the GridBagLayout inside of a FlowLayout, and then set the alignment of the FlowLayout to top-left, or whatever you wish.
I have asked, and answered, this question myself sometime last week.
So in your case you are adding your panel1 and middlePanel directly to the JFrame (?), with a GridBagLayout
JFrame (GridBagLayout)
- panel1
- middlePanel
I would suggest this alternative structure instead, to get rid of all the extra space (and centre alignment as well, if you like).
JFrame (FlowLayout)
- JPanel (GridBagLayout)
- panel1
- middlePanel
HTH

Categories