This is my code:
frame2 = new JFrame("Confirmation");
frame2.setLayout(new BorderLayout());
JRadioButton y,n,c;
panel = new JPanel();
ButtonGroup buttonGroup = new ButtonGroup();
y = new JRadioButton("Add");
buttonGroup.add(y);
panel.add(y);
n = new JRadioButton("Update");
buttonGroup.add(n);
panel.add(n);
c = new JRadioButton("Delete");
buttonGroup.add(c);
panel.add(c);
y.setSelected(true);
b1=new JButton();
b1.setBounds(300,100,2,2);
b1.setIcon(new ImageIcon(searchresult.class.getResource("/images/yes.png")));
b2=new JButton();
b2.setBounds(100,10,2,2);
b2.setIcon(new ImageIcon(searchresult.class.getResource("/images/no.png")));
panel.add(b1);
panel.add(b2);
frame2.add(panel);
frame2.setSize(182,150);
frame2.setVisible(true);
Right now this gives me the following output
whereas I want this
with an increased width but I am not able to do it..Could anyone provide me with further details that could help me
JPanel uses a FlowLayout by default, which, as the name suggests, layouts out components one after the after, in a flow...
Two choices. Use a compound layout, using BorderLayout as the base, create JPanel that uses a GridLayout for the radio buttons (using 0 rows and 1 column), add this to the CENTER position of the base panel.
Create a second JPanel using a FlowLayout and your buttons to it. Add this to the SOUTH position of the base pane.
Second choice is to use a GridBagLayout
Take a look at Laying out Components within a Container for more details
Related
I am attempting to design a panel with MiGFormat that has a label at the top, and two buttons at the bottom - a yes/no prompt.
I achieve this closely, but the label yesOrNoText (text is "TEST") is not fully centered:
I initialize the panel containing this prompt like so:
private JPanel createYesNoPrompt() {
JPanel panel = new JPanel(new MigLayout());
panel.setBorder(BorderFactory.createLineBorder(Color.red));
JButton yesButton = new JButton("Yes");
JButton noButton = new JButton("No");
yesOrNoText = new JLabel();
yesOrNoText.setText("TEST");
yesOrNoText.setFont(panel.getFont().deriveFont(Font.BOLD, 30f));
yesOrNoText.setHorizontalAlignment(SwingConstants.CENTER);
Dimension dimension = new Dimension(500, 125);
Font font = panel.getFont().deriveFont(Font.BOLD, 20f);
yesButton.setFont(font);
yesButton.setBackground(new Color(35, 138, 35));
yesButton.setPreferredSize(dimension);
noButton.setFont(font);
noButton.setBackground(new Color(183, 19, 19));
noButton.setPreferredSize(dimension);
yesButton.addActionListener(e -> isYes = true);
noButton.addActionListener(e -> isYes = false);
panel.add(yesOrNoText, "wrap, dock center");
panel.add(yesButton);
panel.add(noButton);
return panel;
}
Then, I add it to gamePanel, then gamePanel to mainPanel, then mainPanel to the frame.
gamePanel.add(YesOrNoPanel, "align center");
mainPanel.add(gamePanel);
add(mainPanel);
I'm unsure of what would be causing yesOrNoText to not become fully centered within the YesNoPanel. Please let me know if I need to clarify anything!
Thank you.
I needed to make the add call for the yesNo label span 2 cells. By adding one component in the first row, then adding two in the next, I essentially created a 2x2 grid.
panel.add(yesOrNoText, "wrap, align center, span 2 1");
panel.add(yesButton);
panel.add(noButton);
Notice that on the first component I add yesOrNoText I use span to tell MiGFormat to take up two cells for this component. I can then center that with the remaining two components because it becomes the only component in the row.
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));
I'm trying Swing programming but I can't do what I want.
I would like to place a top bar button with 2 lines of button but I just have 1 line in my case.
Here is my code:
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
setMinimumSize(new Dimension(1000,500));
setMaximumSize(new Dimension(1000,500));
JPanel panelButton = new JPanel();
JPanel panelTopButton = new JPanel();
JPanel panelBottomButton = new JPanel();
panelTopButton.add(dashboard);
panelTopButton.add(journal);
panelTopButton.add(myPlans);
panelTopButton.add(myFavorites);
panelTopButton.add(shoppingCart);
panelBottomButton.add(profile);
panelBottomButton.add(notifications);
panelButton.add(panelTopButton, BorderLayout.NORTH);
panelButton.add(panelBottomButton, BorderLayout.SOUTH);
contentPane.add(panelButton,BorderLayout.NORTH);
//Display
setSize(400,120);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
I have this
And I want this
Can somebody help me?
You need one panel for each line.
Try to do this:
JPanel panelButtonsL1 = new JPanel();
JPanel panelButtonsL2 = new JPanel();
panelButtonsL1.add(dashboard);
panelButtonsL1.add(journal);
panelButtonsL1.add(myPlans);
panelButtonsL1.add(myFavorites);
panelButtonsL1.add(shoppingCart);
panelButtonsL2.add(profile);
panelButtonsL2.add(notifications);
The default layour of JPanel is FlowLayout. Bear in mind that layout is very important to work with swing component disposition.
Define the bottom panel as GridLayout.
JPanel panelButton = new JPanel(new GridLayout(2, 1)); // 2 rows x 1 column
panelButton.add(panelButtonsL1);
panelButton.add(panelButtonsL2);
Details of GridLayout you can find on API.
You can achieve that using a GridLayout: assign a GridLayout to panelButton with two rows and one column, and then add the two panels to it.
According to what you want there is a simpler alternative by continue using the default FlowLayout from the panel. It is more appropriate than using GridLayout since you wanted the last 2 buttons to move to the next row and center itself.
If you use GridLayout, the buttons at the next row are likely going to be directly below one of the buttons above. Here are 2 ways to get what you want.
Method 1. Reduce the width of the main panel holding your buttons:
Dosing so, you will have to add the main panel using BorderLayout.CENTER.
Method 2. Add the buttons to a sub-panel of smaller width and add it to the main panel. All your buttons will be added to the smaller sub-panel:
I'm developing a Java application for homework. This is my code
JLabel queryHandlerL = new JLabel("Create php to handle query results", JLabel.CENTER);
final JCheckBox queryHandlerCB = new JCheckBox();
JPanel checkBoxPanel = new JPanel(new FlowLayout());
checkBoxPanel.add(queryHandlerL);
checkBoxPanel.add(queryHandlerCB);
// Query Panel
// set image
picLabelQuery = new JLabel("",JLabel.LEFT);
picLabelQuery.setIcon(currentPicForm);
JPanel queryPanel = new JPanel();
final JButton queryButton = new JButton("Insert a query");
queryPanel.add(queryButton);
queryPanel.add(picLabelQuery);
// Panel create
final JButton createButton = new JButton("Create");
JPanel createPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
createPanel.add(createButton);
JPanel finalPanel = new JPanel(new GridLayout(0, 1,5,2));
finalPanel.add(queryPanel);
finalPanel.add(checkBoxPanel);
finalPanel.add(createPanel);
finalPanel.setBorder(BorderFactory.createTitledBorder("SQL connection"));
setLayout(new GridBagLayout());
add(finalPanel);
I have a CardLayout and this is a Window inside this CardLayout. The last add(finalPanel) refers to the panel of the CardLayout.
This piece of code works but this is the result
How do I remove the space that is automatically created between the panels?
How do I remove the space that is automatically created between the panels?
Use a different layout manager for the panel. The GridLayout will always resize components to take up all the space in the panel.
Maybe you can use a BoxLayout or a GridBagLayout. You can also nest panels with different layout managers to get your desired effect.
Read the section from the Swing tutorial on Layout Managers for more information and examples.
You should pack() your surrounding panel or set the height to a desired value.
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!