I'm trying to add few panel to JScrollpane and in turn to frame, but i'm not able to scroll.
Here is the code.
first_panel = new JPanel();
first_panel.setBackground(Color.red);
second_panel = new JPanel();
second_panel.setBackground(Color.blue);
third_panel = new JPanel();
third_panel.setBackground(Color.green);
fourth_panel = new JPanel();
fourth_panel.setBackground(Color.red);
fifith_panel = new JPanel();
fifith_panel.setBackground(Color.blue);
six_panel = new JPanel();
six_panel.setBackground(Color.green);
final_panel = new JPanel();
final_panel.setLayout(null);
final_panel.setBackground(Color.gray);
final_panel.add(first_panel);
final_panel.add(second_panel);
final_panel.add(third_panel);
final_panel.add(fourth_panel);
final_panel.add(fifith_panel);
final_panel.add(six_panel);
first_panel.setBounds(10,10,200,100);
second_panel.setBounds(10,10,200,200);
third_panel.setBounds(10,10,200,300);
fourth_panel.setBounds(10,10,200,400);
fifith_panel.setBounds(10,10,200,500);
six_panel.setBounds(10,10,200,600);
panel_scroll = new JScrollPane(final_panel);
panel_scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
final_panel.setBounds(0,0,400,400);
// scroll_panel = new JPanel();
// scroll_panel.setLayout(null);
// scroll_panel.add(panel_scroll);
// panel_scroll.setBounds(0,0,400,400);
frame.getContentPane().add(panel_scroll);
Can someone helpme with this. Thanks in advance.
use standard LayoutManagers instead of AbsoluteLayout, then Swing GUI will be continuously or proportionally resize with JFrame
use GridLayout() for final_panel in the case that all JPanels can have got the same size on the screen
override getPreferredSize for JPanel, for inital and correct PreferredSize on the screen (greater dimension for final_panel than JScrollPane)
is required to override ScrollPanel.setPreferredScrollableViewportSize(new Dimension(int, int));, then to call JFrame.pack() for inital and correct PreferredSize on the screen
is required to override JScrollPane.getVerticalScrollBar().setUnitIncrement(int); for natural scrolling
Related
public void start_Gui() {
JFrame window = new JFrame("Client Program");
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
window.setContentPane(panel);
panel.setLayout(new GridLayout(1,2));
JLabel leftside = new JLabel();
leftside.setLayout(new GridLayout(2, 1));
JTextArea rightside = new JTextArea();
rightside.setEditable(false); //add scroll pane.
rightside.setBorder(BorderFactory.createLineBorder(Color.BLACK));
rightside.setLayout(new FlowLayout());
JTextArea client_text_input = new JTextArea();
client_text_input.setBorder(BorderFactory.createLineBorder(Color.BLACK));
leftside.add(client_text_input);
JLabel buttons_layer = new JLabel();
JButton login = new JButton("Login");
JButton logout = new JButton("Logout");
buttons_layer.setBorder(BorderFactory.createLineBorder(Color.BLACK));
buttons_layer.setLayout(new GridLayout(2, 1));
buttons_layer.add(login);
buttons_layer.add(logout);
leftside.add(buttons_layer);
panel.add(leftside);
panel.add(rightside);
window.setSize(300, 400);
window.setResizable(false);
window.setVisible(true);
}
I am working on a simple java chat client gui application. (the server etc, is done by others).
It is not a big project, but my only problem is that whatever I do to try to resize any components on the above GUI, won't work.
For example:
JTextArea client_text_input = new JTextArea();
client_text_input.setSize(100,200);
Won't work.
Thanks for the help.
In Swing, you have two options for layout: do everything manually or let a LayoutManager handle it for you.
Calling setSize() will only work when you're not using a LayoutManager. Since you're using a GridLayout you'll have to use other ways to specify what you want.
Try calling setPreferredSize() and setMinimumSize().
Two things - firstly you should be setting the preferredSize of the scrollpane, but secondly, trying to resize it inside the componentResized handler isn't a very effective technique because the 'resized' events aren't continuous.
check resizing text area in a JFrame
but setXxxSize (for ContainersChilds) works as chaims if you change from setSize() (for TopLayoutContainer) to setPreferredSize() and you have to call pack() before setVisible()
I am new to Java and learning GUI now a days. I want to add space on top of may FirstPlayer name I am using JPanel with GridLayout but when I add invisible box as the first element to set my elements in the center but I got nothing as my desire. Please help me.
Here is my code:
JPanel main = new JPanel();
GridLayout layout = new GridLayout(6,1);
layout.setVgap(10);
JPanel parentPanel = new JPanel(layout);
parentPanel.setOpaque(false);
parentPanel.add(Box.createRigidArea(new Dimension(80,0)));
parentPanel.add(main.getFirstName());
parentPanel.add(main.getFirstField());
parentPanel.add(Box.createRigidArea(new Dimension(20,0 )));
parentPanel.add(main.getSecondName());
parentPanel.add(main.getSecondField());
main.add(parentPanel,BorderLayout.CENTER);
JFrame frame = new JFrame("Player Menu");
frame.add(main,BorderLayout.CENTER);
frame.add(Box.createRigidArea(new Dimension(100,0 )),BorderLayout.NORTH);
frame.setVisible(true);
frame.pack();
frame.setSize(900,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
I am creating a game which has a background image with cards displayed overtop. I would like to place the background image and cards such that they're always centered vertically and horizontally, even upon resizing the JFrame.
Currently, I am creating the cards (each a JPanel) and adding them into a container JPanel (no layout manager), then I add that Jpanel to the JFrame. After that I place the background image in a JPanel, then add that JPanel to the JFrame. The result is: The background image is hidden behind the cards and revealed when removing each card as desired. The background image is always centered but the card's JPanel does not move around upon resize. I am having a hard time getting the cards to always be centered, no matter what I try. I also need to add another JPanel to the JFrame in the South border, so that will need to work as well. I appreciate your assistance!
In the class that extends JFrame:
setSize(1060,700);
cardPanel = new JPanel();
cardPanel.setSize(1060,700);
cardPanel.setOpaque(false);
cardPanel.setLayout(null);
...card.setLocation(x, y); //loop through cards
...cardPanel.add(card); //and add each one
add(cardPanel, BorderLayout.CENTER); //add cardPanel to JFrame
//Add background image
bgPanel = new JPanel();
URL url = getClass().getResource("images/dragon_bg.png");
imgIcon = new ImageIcon(url);
JLabel background = new JLabel(imgIcon);
bgPanel.setLayout(new BorderLayout());
bgPanel.add(background);
add(bgPanel, BorderLayout.CENTER);
setVisible(true);
I would like to place the background image and cards such that they're always centered vertically and horizontally, even upon resizing the JFrame.
Then you need to use layout managers on your panels. The layout manager is responsible for redoing the layout.
How to add two JPanels to a JFrame in the center?
You could try using the OverlayLayout for this. I think the basic code would be:
JPanel contentPane = new JPanel( new GrigBagLayo9ut() );
frame.add(contentPane, BorderLayout.CENTER);
JPanel overlay = new JPanel()
overlay.setLayout( new OverlayLayout(overlay) );
contentPane.add(overlay, new GridBagConstraints()); // this should center the overlay panel
overlay.add(yourCardPanel); // you care panel must use a suitable layout
overlay.add(new JLabel() ); // use a JLabel for the background not a custom panel
I also need to add another JPanel to the JFrame in the South border,
The default layout manager for a JFrame's content pane is a BorderLayout. We already added the game panel to the center, so know you just add your other panel to the SOUTH.
If the OverlayLayout doesn't work the way you want then you will need to nest panels. Something like:
JPanel center = new JPanel( new GridBagLayout() );
frame.add(center, BorderLayout.CENTER);
JLabel background = new JLabel(...);
background.setLayoutManager( new GridBagLayout() );
center.add(background, new GridBagConstraints());
background.add(yourCardPanel, new GridBagConstraints());
Edit:
Using nested panels:
import java.awt.*;
import javax.swing.*;
public class GridBagLayoutCenter extends JPanel
{
public GridBagLayoutCenter()
{
setLayout( new BorderLayout() );
JLabel background = new JLabel( new ImageIcon("mong.jpg") );
background.setLayout( new GridBagLayout() );
add(background, BorderLayout.CENTER);
JPanel tiles = new JPanel();
tiles.setPreferredSize( new Dimension(200, 200) );
tiles.setBackground( Color.RED );
background.add(tiles, new GridBagConstraints());
add(new JLabel("SOUTH"), BorderLayout.SOUTH);
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("GridBagLayoutCenter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new GridBagLayoutCenter() );
frame.setSize(400, 400);
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
The preferred size of the "tiles" panel should not be hardcoded. The size should be determined by your custom layout manager based on the tiles that you add to the panel. The size should not change as tiles are removed.
I ultimately decided to place the background image in the card panel itself, then put the card panel in a box layout manager so that it's always centered. I renamed cardPanel to gameBoard. Definitely could be cleaner, but I can only work with my requirements.
setSize(new Dimension(1000, 600));
gameBoard = new JPanel();
gameBoard.setLayout(null);
gameBoard.setOpaque(false);
Dimension expectedDimension = new Dimension(920, 500);
gameBoard.setPreferredSize(expectedDimension);
gameBoard.setMaximumSize(expectedDimension);
gameBoard.setMinimumSize(expectedDimension);
//add cards to gameBoard here
JLabel background = new JLabel( new ImageIcon( getClass().getResource("images/graphic.png") ) );
background.setLocation(79,0); //manually center graphic
background.setBounds(new Rectangle(0, 0, 920, 500));
gameBoard.add(background);
Box centerBox = new Box(BoxLayout.Y_AXIS);
centerBox.setOpaque(true);
centerBox.setBackground(Color.WHATEVER);
centerBox.add(Box.createVerticalGlue());
centerBox.add(gameBoard);
centerBox.add(Box.createVerticalGlue());
add(centerBox);
setVisible(true);
Is there any way that I could add panels in bottom-to-top order?..
I've tried some layout managers, but still i couldn't get it..
need help.
JPanel mainpanel = new JPanel();
JPanel panel_1 = new JPanel();
JPanel panel_2 = new JPanel();
JPanel panel_3 = new JPanel();
mainpanel.add(panel_1);
mainpanel.add(panel_2);
mainpanel.add(panel_3);
mainpanel.add(panel_4);
mainpanel.add(panel_5);
mainpanel.add(panel_n);
You should be able to specify an index for adding the panel. Ie:
mainpanel.add(panel_1, 0);
mainpanel.add(panel_2, 0);
mainpanel.add(panel_3, 0);
This will always add each panel in the first position.
MigLayout works here as well - and increases maintainabilty:
JPanel mainpanel = new JPanel(new MigLayout();
private void addPanel(JPanel newPanel) {
mainpanel.add(newPanel, "dock north");
}
Add the panel in order, In which order you want to display in the frame.
Let p1,p2,p3 are the subv-panels and p is main panel.
You want order of panel as folowing : p2,p3,p1
p.add(p2);
p.add(p3);
p.add(p1);
then finally add it to your frame.
I think this will help you.
public void start_Gui() {
JFrame window = new JFrame("Client Program");
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
window.setContentPane(panel);
panel.setLayout(new GridLayout(1,2));
JLabel leftside = new JLabel();
leftside.setLayout(new GridLayout(2, 1));
JTextArea rightside = new JTextArea();
rightside.setEditable(false); //add scroll pane.
rightside.setBorder(BorderFactory.createLineBorder(Color.BLACK));
rightside.setLayout(new FlowLayout());
JTextArea client_text_input = new JTextArea();
client_text_input.setBorder(BorderFactory.createLineBorder(Color.BLACK));
leftside.add(client_text_input);
JLabel buttons_layer = new JLabel();
JButton login = new JButton("Login");
JButton logout = new JButton("Logout");
buttons_layer.setBorder(BorderFactory.createLineBorder(Color.BLACK));
buttons_layer.setLayout(new GridLayout(2, 1));
buttons_layer.add(login);
buttons_layer.add(logout);
leftside.add(buttons_layer);
panel.add(leftside);
panel.add(rightside);
window.setSize(300, 400);
window.setResizable(false);
window.setVisible(true);
}
I am working on a simple java chat client gui application. (the server etc, is done by others).
It is not a big project, but my only problem is that whatever I do to try to resize any components on the above GUI, won't work.
For example:
JTextArea client_text_input = new JTextArea();
client_text_input.setSize(100,200);
Won't work.
Thanks for the help.
In Swing, you have two options for layout: do everything manually or let a LayoutManager handle it for you.
Calling setSize() will only work when you're not using a LayoutManager. Since you're using a GridLayout you'll have to use other ways to specify what you want.
Try calling setPreferredSize() and setMinimumSize().
Two things - firstly you should be setting the preferredSize of the scrollpane, but secondly, trying to resize it inside the componentResized handler isn't a very effective technique because the 'resized' events aren't continuous.
check resizing text area in a JFrame
but setXxxSize (for ContainersChilds) works as chaims if you change from setSize() (for TopLayoutContainer) to setPreferredSize() and you have to call pack() before setVisible()