Item inside BoxLayout panel not occupying width of JPanel - java

Below panel has BoxLayout and the button's at the bottom are not occupying width of the panel and instead there is gap on the left and right side.
JPanel panel = new JPanel();
BoxLayout boxlayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(boxlayout);
JPanel framesPanel[] = new JPanel[8];
for(int i=0;i<8;i++) {
framesPanel[i] = new JPanel();
framesPanel[i].setLayout(new BoxLayout(framesPanel[i], BoxLayout.X_AXIS));
JButton jb1 = new JButton("Button 1");
jb1.setAlignmentX(Component.LEFT_ALIGNMENT);
JButton jb2 = new JButton("Button 2");
jb2.setAlignmentX(Component.LEFT_ALIGNMENT);
JButton jb3 = null;
framesPanel[i].add(jb1);
framesPanel[i].add(jb2);
if (i < 6) {
jb3 = new JButton("Button 3");
jb3.setAlignmentX(Component.LEFT_ALIGNMENT);
framesPanel[i].add(jb3);
}
panel.add(framesPanel[i]);
}
// Set size for the frame
panel.setSize(300, 300);
frame.setSize(300, 300);
// Set the window to be visible as the default to be false
frame.add(panel);
I have tried with setAlignmentX(Component.LEFT_ALIGNMENT) but this doesn't seem to have any effect.

A BoxLayout respects the maximum size of a component.
The buttons maximum size is the same as its preferred size.
When you create the buttons you could do something like:
JButton button = new JButton(...);
Dimension maximum = button.getMaximumSize();
maximum.width = Integer.MAX_VALUE;
button.setMaximumSize( maximum );
Note:
This is only a quick fix that will work if you never change the Font size of the button. For a proper solution you should really creat a custom button that extends JButton and override the getMaximumSize() method to return the above size. Then this will work if the properties of the button are changed dynamically.

Related

How to make a vertical side menu, Jmenus in swing?

I'm relatively new to java swing, so I don't know to change the menu component in swing with a vertical one.
If you want a side menu with buttons, I imagine you probably want to use a JPanel with GridLayout. Something like:
JPanel p = new JPanel();
p.setLayout(new GridLayout(2,2));
Jlabel l = new JLabel("a");
p.add(l);
JButton btn = new JButton("click");
p.add (btn);
l = new JLabel("b");
p.add(l);
btn = new JButton("click");
p.add (btn);

making a full size panel, with grid bag layout

I am making a program with 3 JSliders, for r,g,b and i want to add a panel that will change it's color for the chosen color in the sliders, everything works for me except one thing, I don't know how to make the panel in the full size of the screen, this is the best I could do, but this is still kind of small and I want to make the panel full size. can any one show me how to do it?
The program is kind of long so I will only send the part of the gridbaglayout and the panel.
private JPanel panel;
public delta(){
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
panel = new JPanel();
panel.setBackground(new Color(0 ,0 ,0));
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 6;
c.gridwidth = 3;
c.gridheight = 3;
add(panel ,c);
Simply add your panel to Jframe
jframe.add(yourpanel)
This code without any Layout will fill the window.
Edit:
JFrame frame = new JFrame();
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("HELLO");
frame.add(label);
This fills the JFrame with JLabel.

How to re-size JButton

I've bee teaching myself java and following along with the problems in the book. I'm trying to make a display for my calculator. In the example(I did not attach this) the buttons were a smaller size than what mine are and I can't figure out how to reformat them. I tried using the dimension class but it had no affect. Also, I can't get my text at the top of the calculator to align left.
Here is my code:
public class Calculator extends JFrame {
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(300, 300);
setLayout(new BorderLayout());
JPanel numberPanel = new JPanel();
add(numberPanel, BorderLayout.CENTER);
numberPanel.setLayout(new GridLayout(4, 3, 3, 3));
for(int i = 1; i < 10; i++) {
JButton button = new JButton(String.valueOf(i));
numberPanel.add(button);
}
JButton zero = new JButton("" + 0);
JButton dot = new JButton(".");
JButton clear = new JButton("C");
numberPanel.add(zero);
numberPanel.add(dot);
numberPanel.add(clear);
JPanel keyPanel = new JPanel();
add(keyPanel, BorderLayout.EAST);
keyPanel.setLayout(new GridLayout(4, 1, 3, 3));
JButton plus = new JButton("+");
JButton minus = new JButton("-");
JButton times = new JButton("*");
JButton divide = new JButton("/");
keyPanel.add(plus);
keyPanel.add(minus);
keyPanel.add(times);
keyPanel.add(divide);
JPanel equalsPanel = new JPanel();
add(equalsPanel, BorderLayout.SOUTH);
equalsPanel.setLayout(new GridLayout(1, 1));
JButton equals = new JButton("=");
equalsPanel.add(equals);
JPanel textPanel = new JPanel();
add(textPanel, BorderLayout.NORTH);
JTextField inputBox = new JTextField("0.0");
inputBox.setHorizontalAlignment(JTextField.LEFT);
inputBox.setEditable(false);
Font font = new Font("MonoSpaced", Font.BOLD, 20);
inputBox.setFont(font);
textPanel.add(inputBox);
setVisible(true);
}
public static void main(String[] args) {
new Calculator();
}
}
Imports were left off for brevity
GridLayout will laugh at you when you try and set a dimension. It does respect preferred sizes. You should select a layout manager that will respect preferred sizes. Or you can simply pack() (after you add all your components) your frame instead of setSize() and all the components preferred sizes will kick in. (Disclaimer - because of GridLayout though, if you try and resize the frame after that, you components will resize again)
See more at How to use Layout Managers. For a quick view of which layout managers respect preferred sizes and which ones don't, have a look at this post.
A common approach is to nest panels with different layout managers also, as seen here
UPDATE
As mentioned preciously, you should just call pack on the frame instead of set size. With your current code, this would cause the frame to be very small because of the preferred sizes of the components. If you want the buttons to have a bigger preferred size, you can set the font to a bigger font and/or use button.setMargins(new Insets(w,x,y,x)); to make the margins bigger. But it is preferred to pack the frame.
I would recommend using the Window Builder add-on if you’re using Eclipse. This tool will help you with many aspects of Swing. Learn by doing.
WindowBuilder Dowload Link

How to fix JPanel with Grid that occupies full width of frame?

I'm working on a layout for a simple login screen. I am using GridLayout in order to manipulate elements, but have came across an issue, It occupies full frame width like this:
Where as I want it to be with a fixed position and width,height and be vertically, horizontally centered inside a frame, but not occupy it's full size.
Possibly make Username and Password labels occupy less space so text fields are actually closer to them. I tried setting main size of my panel like .setMaximumSize(Dimension (200, 150));
But it doesn't seem to work.
//Create the frame.
JFrame frame = new JFrame("Online Shop");
JPanel mainPanel = new JPanel();
JPanel usernamePanel = new JPanel();
JPanel passwordPanel = new JPanel();
JPanel buttonPanel = new JPanel();
JButton loginButton = new JButton("Login");
loginButton.setMaximumSize(new Dimension(100, 50));
JTextField username = new JTextField();
JLabel usernameLabel = new JLabel("Username");
JPasswordField password = new JPasswordField();
JLabel passwordLabel = new JLabel("Password");
//Panel
frame.setContentPane(mainPanel);
usernamePanel.setLayout(new GridLayout(1,2));
usernamePanel.add(usernameLabel);
usernamePanel.add(username);
passwordPanel.setLayout(new GridLayout(1,2));
passwordPanel.add(passwordLabel);
passwordPanel.add(password);
mainPanel.setLayout(new GridLayout(3, 1));
mainPanel.add(usernamePanel);
mainPanel.add(passwordPanel);
mainPanel.add(loginButton);
//Event Listeners
frame.addWindowListener(new MyWindowListener());
loginButton.addActionListener(new MyActionListener());
//Sizes, Positioning
frame.setSize(720, 480);
frame.setLocationRelativeTo(null);
//Show Frame
frame.setVisible(true);
frame.setSize(720, 480);
Don't use the setSize() method.
Instead use:
frame.pack();
Also use:
JTextField username = new JTextField(10);
To give the text fields a preferred number of characters.
Now when the frame is made visible the components will be displayed at their preferred size and the frame will be sized appropriately to fit the components.
If you want a little extra space between the components and the frame then you can do:
mainPanel.setBorder( new EmptyBorder(20, 20, 20, 20) );

How can I set a JPanel maximum or preferred size that uses gridLayout?

I had problem using a very simple frame containing two JPanel.
The problem is on the layout of the Center JPanel that contains four JButton.
How can I set a better size for buttons or directly for JPanel that uses the GridLayout. On the picture the problem:
alt http://img42.imageshack.us/img42/4601/horrible.jpg
!
Here the code: ` JFrame window = new JFrame("Horrible! LOL");
JTextField textField = new JTextField("");
textField.setPreferredSize(new Dimension(200,20));
JPanel textPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
textPanel.add(textField);
window.add(textPanel, BorderLayout.NORTH);
JButton plus = new JButton("+");
//plus.setPreferredSize(new Dimension(50,50)); nothing would change
JButton minus = new JButton("-");
JButton per = new JButton("x");
JButton divide = new JButton("/");
JPanel prova = new JPanel(new GridLayout(2,2,10,10));
Dimension d = new Dimension(20,20);
prova.setMaximumSize(d); // nothing changed!
prova.add(plus);
prova.add(minus);
prova.add(per);
prova.add(divide);
window.add(prova, BorderLayout.CENTER);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(250,300);
window.setResizable(false);
window.setVisible(true);`
Which is a good solution?
Kind regards
Unfortunately gridlayout doesent respect preferred sizes. But still if you want to stick to grid layout then you can try something like this:
public static JComponent wrap(JComponent comp)
{
JPanel panel = new JPanel();
panel.add(comp);
return panel;
}
And then instead of direclty adding in to prova add like this:
prova.add(wrap(plus));
prova.add(wrap(minus));
prova.add(wrap(per));
prova.add(wrap(divide));
Tested, Works perfect!!
There are other better ways though
That's what happen to me:
It's definitely attached to the upper edge of the grid.
alt text http://img96.imageshack.us/img96/9431/stillnot.jpg
Even if in this case, in the wrap method I can set the preferredSize of buttons/comp, every buttons is on its own edge. What about others solutions. How would you position buttons for a calculator?
Kind regards and thanx angain!

Categories

Resources