How to let scrollbar's default position be at top? - java

I have a JPanel in a JScrollPane, and the JPanel contains many small JPanels. But the default position of scrollbar is not at the top, thus the small JPanels isn't shown from the first one. How to let scrollbar's default position be at top?
My code is simplified and looks like this:
JFrame resultFrame = new JFrame("Searching Result");
JPanel listPanel = new JPanel();
listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.Y_AXIS));
for (int i=0; i<5; i++) {
listPanel.add(smallPanel) // smallPanel is created in another class, which is related to i.
}
JScrollPane scrollPanel = new JScrollPane(listPanel);
scrollPanel.setPreferredSize(new Dimension(500, 750));
resultFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
resultFrame.setContentPane(scrollPanel);
resultFrame.pack();
resultFrame.setLocationRelativeTo(null);
resultFrame.setVisible(true);

Related

Can I use multiple grids?

I am trying to figure out the layout for this(the rest of the code is in the early stages) but for this block, I am trying to figure out the best(and doable) way to format it. I want it to be an 8x8 grid that I will eventually populate with the treasure/empty buttons but I also need a title up top as well as some labels and text on the left. I am unsure if I am able to do multiple grids but what I did below is try to create a 1x2 grid and then place two other grids inside, one with the info on the left(3x2), and another with the 8x8 grid for the buttons. I know it's not close to what it needs to be but none of the grids are showing up at all(it's just putting the title and then one column with 8 rows) and I wanna know if I'm even on any sort of right track, or if I'm just making things up at this point. Any tips would be appreciated, or resources about possibly nesting the grids? I can't find anything in my book about That specifically.
private void buildPanel()
{
// Create labels to display the
treasuresLeftLabel = new JLabel("Treasures left: ");
treasuresFoundLabel = new JLabel("Treasures found: ");
triesLeftLabel = new JLabel("Tries left: ");
// Create text fields for each label
treasuresLeftTextField = new JTextField(2);
treasuresLeftTextField.setEditable(false);
treasuresLeftTextField.setText(String.valueOf(20-game.getTreasuresFound()));
treasuresFoundTextField = new JTextField(2);
treasuresFoundTextField.setEditable(false);
treasuresFoundTextField.setText(String.valueOf(game.getTreasuresFound()));
triesLeftTextField = new JTextField(2);
triesLeftTextField.setEditable(false);
triesLeftTextField.setText(String.valueOf(game.getTriesLeft()));
emptyButton = new EmptyButton();
emptyButton.addActionListener(new emptyButtonListener());
treasureButton = new TreasureButton();
treasureButton.addActionListener(new treasureButtonListener());
// new JPanel object referenced by panel
panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder("Treasure Hunt"));
// Add a gridlayout to the content pane
panel.setLayout(new GridLayout(1, 2));
panel.setLayout(new GridLayout(3, 2));
panel.add(treasuresLeftLabel);
panel.add(treasuresLeftTextField);
panel.add(treasuresFoundLabel);
panel.add(treasuresFoundTextField);
panel.add(triesLeftLabel);
panel.add(triesLeftTextField);
panel.setLayout(new GridLayout(8, 8));
panel.add(treasureButton);
panel.add(emptyButton);
}
You can't use multiple grids within the same JPanel - one panel, one layout manager.
But you can nest layout managers (and thereby grids) by using nested panels.
For example you could use a BorderLayout for the first panel (containing the title at the top, the info panel on the left and the button panel in the center.
The code to construct those panel then might look like this:
// panel contains the complete UI
panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder("Treasure Hunt"));
panel.setLayout(new BorderLayout());
panel.add(new JLabel("This is the Title"), BorderLayout.PAGE_START);
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new GridLayout(3, 2));
infoPanel.add(treasuresLeftLabel);
infoPanel.add(treasuresLeftTextField);
infoPanel.add(treasuresFoundLabel);
infoPanel.add(treasuresFoundTextField);
infoPanel.add(triesLeftLabel);
infoPanel.add(triesLeftTextField);
panel.add(infoPanel, BorderLayout.LINE_START);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(8, 8));
buttonPanel.add(treasureButton);
buttonPanel.add(emptyButton);
for (int i = 0; i < 62; i++) {
buttonPanel.add(new JButton(String.format("%02d", i)));
}
panel.add(buttonPanel, BorderLayout.CENTER);

GUI components not aligning vertically inside JPanel in Card layout

Image of my GUI.
I am trying to add components to two JPanel containers and then add those panels as cards. I want the checkboxes after labels in a vertical manner. The problem I am facing is that the components are coming in a grid of two columns:
public class MyFrame extends JFrame {
MyFrame(int width, int height, String title){
setTitle(title);
setSize(width, height);
JPanel internalJP1 = new JPanel(new GridLayout(5,1));
JLabel hobby = new JLabel("Enter your hobbies:");
JCheckBox cricket = new JCheckBox("Cricket");
JCheckBox music = new JCheckBox("Music");
JCheckBox drawing = new JCheckBox("Drawing");
JCheckBox dancing = new JCheckBox("Dancing");
JCheckBox other = new JCheckBox("Other");
internalJP1.add(hobby);
internalJP1.add(cricket);
internalJP1.add(music);
internalJP1.add(drawing);
internalJP1.add(dancing);
internalJP1.add(other);
JPanel internalJP2 = new JPanel(new GridLayout(4,1));
JLabel payment = new JLabel("Payment by:");
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton cheque = new JRadioButton("Cheque", false);
JRadioButton cash = new JRadioButton("Cash", false);
JRadioButton debitCard = new JRadioButton("Debit Card", false);
JRadioButton creditCard = new JRadioButton("Credit Card", false);
buttonGroup.add(cheque);
buttonGroup.add(cash);
buttonGroup.add(debitCard);
buttonGroup.add(creditCard);
internalJP2.add(payment);
internalJP2.add(cheque);
internalJP2.add(cash);
internalJP2.add(debitCard);
internalJP2.add(creditCard);
Container c = getContentPane();
CardLayout cl = new CardLayout();
c.setLayout(cl);
c.add(internalJP1,"crd1");
c.add(internalJP2,"crd2");
cl.show(c,"crd1");
}
}
public class Jtest {
public static void main(String[] args) {
MyFrame frame = new MyFrame(300,200,"FirstFrame");
frame.setVisible(true);
}
}
Just change this line of code (in constructor of class MyFrame)
JPanel internalJP1 = new JPanel(new GridLayout(5,1));
to this
JPanel internalJP1 = new JPanel(new GridLayout(0,1));
i.e. replace 5 with 0
I find GridLayout to not be intuitive. When the row parameter of GridLayout constructor is zero, then each row will contain exactly the number of columns indicated. Hence new GridLayout(0, 1) will ensure that each row (in the grid) will contain exactly one column and it will create as many rows as required in order to ensure this.
Note that GridLayout is not the only layout manager to use when you want to place GUI components in a single column. Swing also has BoxLayout, GridBagLayout, GroupLayout and SpringLayout.
There are also third party layout managers including JGoodies and MiG Layout.
The fact that you are using a CardLayout to view one panel or the other does not affect the layout of those panels. Your problem is entirely related to the layout of the 2 panels.
There are 2 easy ways to fix the panels to make them 1-column:
fix their number of rows so that it is >= the number of elements that you actually add. Now, you have JPanel internalJP1 = new JPanel(new GridLayout(5,1)); - but add 6 elements. If you change it to JPanel internalJP1 = new JPanel(new GridLayout(6,1));, you will solve the problem for your 1st panel. The same fix applies to your second panel. Even better, use 0 to make the layout use as many rows as it needs (see docs).
use a vertical BoxLayout and do not worry about the number of elements at all - just stack them vertically. This has the additional advantage that they look better, because even if you give the layout more size than it needs, they still stack together nicely: JPanel internalJP1 = new JPanel(); internalJP1.setLayout(new BoxLayout(internalJP1, BoxLayout.Y_AXIS));

How to fill Cells of GridbagLayout with the controls in Swing?

I need to design a swing GUI which has a JFrame with a Menu on top and another main panel having three more panels in center and a separate panel in the bottom of the panel. The required design of the UI is as below
But when I run my swing application I get the output like this (all the panels are packed in the center of the window)
Below is my code
import java.awt.*;
import javax.swing.*;
public class FrontEndView {
private JFrame mainFrame;
private JPanel mainPanel,subPanelUp,subPanelDown,panelLeft,panelRight,panelCenter,panelDown;
private JScrollPane scrollPane;
private JList logViewList;
private JPanel panel1;
public FrontEndView(){
this.prepareGUI();
}
public void prepareGUI(){
mainFrame=new JFrame("GUI");
Toolkit tk = Toolkit.getDefaultToolkit();
int xSize = ((int) tk.getScreenSize().getWidth());
int ySize = ((int) tk.getScreenSize().getHeight());
mainFrame.setSize(xSize,ySize);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setResizable(true);
mainFrame.setLayout(new BorderLayout());
mainPanel=new JPanel();
mainPanel.setLayout(new GridBagLayout());
mainPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
GridBagConstraints gridbagConstMain = new GridBagConstraints();
GridBagConstraints gridbagConstSub = new GridBagConstraints();
subPanelUp=new JPanel();
subPanelUp.setLayout(new GridBagLayout());
subPanelUp.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
panelLeft=new JPanel();
panelLeft.setBorder(BorderFactory.createTitledBorder("Message Defs"));
gridbagConstSub.fill = GridBagConstraints.HORIZONTAL;
gridbagConstSub.weightx = 0.5;
gridbagConstSub.gridx = 0;
gridbagConstSub.gridy = 0;
subPanelUp.add(panelLeft, gridbagConstSub);
panelCenter=new JPanel();
panelCenter.setBorder(BorderFactory.createTitledBorder("Main Workspace"));
gridbagConstSub.fill = GridBagConstraints.HORIZONTAL;
gridbagConstSub.weightx = 0.5;
gridbagConstSub.gridx = 1;
gridbagConstSub.gridy = 0;
subPanelUp.add(panelCenter, gridbagConstSub);
panelRight=new JPanel();
panelRight.setBorder(BorderFactory.createTitledBorder("Script Viewer"));
gridbagConstSub.fill = GridBagConstraints.HORIZONTAL;
gridbagConstSub.weightx = 0.5;
gridbagConstSub.gridx = 2;
gridbagConstSub.gridy = 0;
subPanelUp.add(panelRight, gridbagConstSub);
mainPanel.add(subPanelUp,gridbagConstMain);
subPanelDown=new JPanel();
subPanelDown.setLayout(new BorderLayout());
panelDown=new JPanel();
panelDown.setBorder(BorderFactory.createTitledBorder("Log View"));
logViewList= new JList();
panelDown.add(logViewList);
gridbagConstSub.fill = GridBagConstraints.HORIZONTAL;
//gridbagConst.ipady=20;
//gridbagConst.weightx = 0.0;
gridbagConstSub.gridwidth = 5;
gridbagConstSub.gridx = 0;
gridbagConstSub.gridy = 0;
subPanelDown.add(panelDown,BorderLayout.PAGE_END);
mainPanel.add(subPanelDown, gridbagConstSub);
scrollPane=new JScrollPane(mainPanel,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
mainFrame.add(scrollPane);
mainFrame.setVisible(true);
}
public static void main(String[] args){
FrontEndView frontEnd = new FrontEndView();
}
}
I want to fill the GridBagLayout's cells with the relevant panel/control it holds as shown in the design and also each panel should have its controls filled inside (I need to add a JList inside the panelDown whose size should be the size of the panelDown JPanel).Simply I don't need any extra space visible in my JFrame. Please guide me on what is missing in my code.
I would suggest you can use nested panels with different layout managers to solve the problem.
The default layout of a frame is a BorderLayout.
So you could create a panel and add it to the PAGE_END so it displays the entire width at the bottom.
Then you can create another panel that uses a GridLayout. You can then add 3 child panels to this panel and each panel can use its own layout. Then you add this panel to the CENTER of the frame. As the frame size changes the extra spaces will be allocated to the CENTER so the panels will dynamically grow.
Edit:
Too many panels for me to take the time to understand what is happening
I was suggesting a structure like this:
frame (which by default uses a BorderLayout)
--- CENTER
panel using GrigBagLayout
childPanel1
childPanel2
childPanel3
---- PAGE_END
JScrollPane containing the JList
When you create the JList the basic code would be:
JList list = new JList(...);
list.setVisibleRowCount(5);
JScrollPane scrollPane = new JScrollPane( list );
There is no need to create a panel just to add the list to another panel. The point of setting the visible row count is to give the JList a fixed height. Scrollbars will then appear in the scroll pane as needed.
Now that the PAGE_END has a fixed height component all the reset of the space will go to the component that you add to the CENTER of the frame.
all the panels are packed in the center of the window)
The panels are displayed at their preferred sizes when you use the GridBagLayout. If the total size of all the panels is less than the size of the scrollpane then they will be in the center. If you want the panels to fill the space available, then I believe you need to use the weightx/y constraints. Read the section from the Swing tutorial on How to Use GridBagLayout which describes all the constraints.
That is why I suggested a GridLayout instead. It will make all the panels the same size and will fill the viewport of the scroll pane without playing with constraints.
mainFrame.add(menubar,BorderLayout.NORTH);
That is not how you add a menubar to the frame.
You should be using:
mainFrame.setJMenuBar(menuBar);
You were told this in your last question. Why did you not listen to the advice??? Why should we take the time to help when you don't pay attention to what is suggested.
Based on your instructions I changed my design in a way all of the outer panels are used with Border Layout and the inner most ones with more controls were used with Grid, GridBag and FlowLayouts based on the requirement. In that way the entire design could be done nicely.
Also if a particular panel within a cell of a layout needs to be expanded, I used the setPreferredSize(new Dimension(int,int)) whenever required.

java scrollpane with flowlayout

For some reason, I cannot this working. It should be simple really.
I've having a JFrame with a BorderLayout, which contains a JPanel (SOUTH) and a (CENTER) JPanel (itemPanel).
itemPanel should be wrapped in a scrollpane. Its width = x, and all of its children shares its length, so it basically works like a table with only 1 column.
For some reason, I cannot get the scrollpane to show the scrollbars (and scroll). In the JFrame:
setPreferredSize(dimension);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().add("South",controlPanel);
JScrollPane scroll = new JScrollPane(itemPanel);
scroll.setBorder(null);
getContentPane().add("Center",scroll);
super.pack();
setVisible(true);
Initilizing and adding some dummy-panels to the itemPanel:
itemPanel = new ItemPanel(); // A JPanel with a flowlayout
itemPanel.setPreferredSize(new Dimension(dimension.width,0));
for(int i = 0; i < 20; i++){
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(0,50));
p.setBackground(i%2 == 0 ? Color.GREEN : Color.YELLOW);
itemPanel.add(p);
}
if omitting itemPanel.setPreferredSize(new Dimension(dimension.width,0)); the scrollpane shows the horizontal scrollbars, but since the flowlayout does not have a width to follow, it just shows the components in one row.
A FlowLayout does NOT recalculate the preferred size of a panel. The scrollbars only appear when the preferred size of the component is greater than the size of the scroll pane.
WrapLayout may be what you are looking for.

How do i get vertical scrolling to JPanel?

I have written a code in java using swing, so that I will have a JscrollPane added to JPanel and then I will add buttons of fixed size to JPanel in vertical fashion
JPanel panel=new JPanel();
panel.setBackground(Color.WHITE);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
JScrollPane jsp=new JScrollPane(panel,v,h);
jsp.setPreferredSize(new Dimension(600,600));
jsp.setBounds(150,670,850,200);
frame.add(jsp);
then I am adding buttons to it at run time.
for(i=0;i<n;i++)
{
button[i]=new JButton();
button[i].setBounds(20,y,120,120);
button[i].setSize(120,120);
button[i].setToolTipText(file[i].toString());
button[i].setIcon(Icon);
panel.add(button[i]);
y=y+140;
}
I want to add a buttons one below the other...(i.e I want a vertical scrollbar)
i.e. button1
button2
'
'
but above code is giving me buttons in a line (i.e. I am getting horizontal scrollbar)
i.e. button1 button2...
another problem is the size of the buttons. Using btn.setSize() is not affecting size at all...
can anybody help me?
You must use an appropriate Layoutmanager like GridLayout, Boxlayout or GridBagLayout for the panel.
It depends what else you want to put into the panel.
GridLayout is easier to use IMO:
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 1)); // any number of rows, 1 column
...
panel.add(button[i]);
BoxLayout is almost as easy:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
...
panel.add(button[i]);
GridBagLayout is more powerful, allowing more than one column, components spanning more than one cell, ... needs a GridBagConstraints to add the elements:
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints(
0, RELATIVE, // x = 0, y = below previous element
1, 1, // cell width = 1, cell height = 1
0.0, 0.0 // how to distribute space: weightx = 0.0, weighty = 0,0
GridBagConstraints.CENTER, // anchor
GridBagConstraints.BOTH, // fill
new Insets(0, 0, 0, 0), // cell insets
0, 0); // internal padding
...
panel.add(button[i], constraints);
Have a look at this tutorial: Laying Out Components Within a Container (The visual guide is a good start point)
EDIT:
you can also lay out the components by hand, that is, specify the location and size of each component in the container. For this you must set the LayoutManager to null so the default manager gets removed.
JPanel panel = new JPanel();
panel.setLayout(null);
...
button[i].setLocation(x, y);
button[i].setSize(width, heigth);
// OR button[i].setBounds(x, y, width, height);
panel.add(button[i]);
You need to define an appropriate LayoutManager for your JPanel, which is responsible for how the Components added to it are positioned. The default LayoutManager is FlowLayout, which lays out Components left-to-right. For laying out Components vertically you should consider using BoxLayout or GridBagLayout.
You have to set LayoutManager for JPanel or use Box(BoxLayout.Y_AXIS) instead.
For the size of buttons use preferredSize
For your layout problem you need to change the layout manager to one that does a vertical layout. For playing around purposes you can use BoxLayout like this:
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
This is much easier if you let the layout manager do its work.
In Swing, the way the components are layout over other component ( a panel for instance ) is using a layout manager.
It is used to avoid having to compute the coordinates of all the components against each other each time the container component resizes, or a new component is added.
There are different layout mangers, the one that you need here is BoxLayout.
By using this layout you don't need to specify the button position, nor its size. The layout manager query each component and use that information to place them in the correct position and size.
For instance the following frame
Was created this ( modified version of your ) code:
import javax.swing.*;
import java.awt.*;
public class ScrollTest {
private JPanel panel;
private Icon[] icons = new Icon[3];
public void main() {
panel =new JPanel();
// Use top to bottom layout in a column
panel.setLayout( new BoxLayout( panel, BoxLayout.Y_AXIS ));
panel.setBackground(Color.WHITE);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
JScrollPane jsp=new JScrollPane(panel,v,h);
jsp.setPreferredSize(new Dimension(600,600));
jsp.setBounds(150,670,850,200);
JFrame frame = new JFrame();
frame.add(jsp);
// my addition to load sample icons
loadImages();
// simulate dynamic buttons
addButtons();
frame.pack();
frame.setVisible( true );
}
void loadImages() {
icons[0] = new ImageIcon( "a.png" );
icons[1] = new ImageIcon( "b.png" );
icons[2] = new ImageIcon( "c.png" );
}
void addButtons() {
for( int i = 0 ; i < icons.length ; i++ ) {
JButton button = new JButton();
Icon icon = icons[i];
button.setIcon( icon );
// Set the button size to be the same as the icon size
// The preferred size is used by the layout manager
// to know what the component "better" size is.
button.setPreferredSize( new Dimension( icon.getIconWidth(),
icon.getIconHeight() ) );
// This is IMPORTANT. The maximum size is used bythe layout manager
// to know "how big" could this component be.
button.setMaximumSize( button.getPreferredSize() );
panel.add( button );
}
}
public static void main( String ... args ) {
new ScrollTest().main();
}
}
I hope this helps.
One can also get a vertical scrolling for JPanel with SpringLayout. It's possible if panel's vertical size will be defined by setting a constraint SpringLayout.SOUTH. This can be done like this:
JPanel panel = new JPanel();
SpringLayout panelLayout = new SpringLayout();
panel.setLayout(panelLayout);
// Adding components to the panel here
// .....
// That's what defines panel's exact size and makes its scrolling possible
panelLayout.putConstraint(SpringLayout.SOUTH, panel, 0,
SpringLayout.SOUTH, lastComponentOfThePanel);
JScrollPane panelScrollPane = new JScrollPane(panel);
where lastComponentOfThePanel is a component at the bottom of a panel.
Hope this will help somebody. In my opinion, SpringLayout is very powerful layout manager, and sometimes it's very difficult or almost impossible to replace this one with GridBagLayout.
What about?
JScrollPane scrollPane = new JScrollPane(yourpanel);
container.add(scrollPane);

Categories

Resources