I am new to Java GUI stuff and currently working on creating a microwave GUI. I am trying to place a panel within a panel but it's not really working as I think it should. I am using BorderLayout and basically putting a panel into BorderLayout.WEST and putting 2 panels within it (NORTH and CENTER). But somehow I can't add a panel to its CENTER, it seems like its going to NORTH all the time. Please see my code below.
private void buildFrame()
{
ovenLabel=new JLabel("Oven Area - Food goes here");
add(ovenLabel, BorderLayout.CENTER);
ovenLabel.setBackground(Color.white);
ovenLabel.setOpaque(true);
ovenLabel.setHorizontalAlignment(JLabel.LEFT);
ovenLabel.setVerticalAlignment(JLabel.TOP);
ovenLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
//sidePanel
side=new JPanel();
add(side, BorderLayout.EAST);
side.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5)); //remove this later
//top panel
sideTop=new JPanel();
sideTop.setBackground(Color.white);
sideTop.setOpaque(true);
sideTop.setLayout(new GridLayout(3, 1));
side.add(sideTop, BorderLayout.NORTH);
displayLabel=new JLabel[3];
for(int i=0;i<displayLabel.length; i++)
{
displayLabel[i]=new JLabel("");
sideTop.add(displayLabel[i]);
}
displayLabel[0].setText("Cooking time is displayed here. . .");
//bottom panel
sideBot=new JPanel();
sideBot.setLayout(new GridLayout(5, 3));
side.add(sideBot, BorderLayout.CENTER);
buttons=new JButton[15];
for(int i=0;i<buttons.length;i++)
{
buttons[i]=new JButton("test");
sideBot.add(buttons[i]);
}
//button
}
If you want to use a BorderLayout , make sure that the container has BorderLayout as its layout manager, or the constraints (NORTH, etc..) will not be understood as they should :
side.setLayout(new BorderLayout());
(do this before adding anything to it)
If you call :
System.out.println(side.getLayout());
before doing the modification, you will see that your panel had a FlowLayout by default.
Related
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);
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 am making a copy of the apple calendar application, and I am having trouble aligning the month name and year name with the center of my screen, while aligning the left and right buttons with the left and right sides of the screen. Here is my code:
final JPanel months = new JPanel();
months.setLayout(new BoxLayout(months,BoxLayout.X_AXIS));
months.add(back, BorderLayout.WEST); //back is a JButton
JLabel monthName = new JLabel(this.monthNames[this.month]+" ", SwingConstants.CENTER);
JLabel year = new JLabel("" + this.year, SwingConstants.CENTER);
monthName.setFont(new Font("Helvetica", 0, 24));
year.setFont(new Font("Helvetica", 0, 24));
monthName.setHorizontalAlignment(JLabel.CENTER);
months.add(monthName, BorderLayout.CENTER);
months.add(year, BorderLayout.CENTER);
months.add(front, BorderLayout.EAST);
add(months);
Yet it shows up like this:
months.setLayout(new BoxLayout(months,BoxLayout.X_AXIS));
You are using a BoxLayout. A BoxLayout just adds the components horizontally to the panel. The WEST, CENTER, EAST constrains are only used by a BorderLayout so they are ignored by the BoxLayout.
months.add(monthName, BorderLayout.CENTER);
months.add(year, BorderLayout.CENTER);
When using a BorderLayout you can only add a single component to a region of the layout. So if you want to add two components to the CENTER you need to first create a panel and add the components to the panel.
So your basic code might be something like:
JPanel centerPanel = new JPanel();
centerPanel.add(month);
centerPanel.add(year);
JPanel mainPanel = new JPanel( new BorderLayout() );
mainPanel.add(westButton, BorderLayout.WEST);
mainPanel.add(centerPanel, BorderLayout.CENTER);
mainPanel.add(eastButton, BorderLayout.EAST);
In jframe, I use miglayout for main jpanel position.
in the left panel, I have 2 jpanel, I use boxlayout.
ComponentPanel is the top left position and PropertyPanel is at the bottom left position.
leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
leftPanel.setMinimumSize(new Dimension(600, 600));
add(leftPanel, BorderLayout.WEST);
componentPanel = new ComponentPanel();
propertyPanel = new PropertyPanel();
in the propertyPanel constructor, i do
setLayout(new MigLayout("debug"));
i get this
why panel is setted to right?
if i add dynamically some space is added.
I tried to use fill to the miglayout constructor without success.
in green is the leftPanel
in red is the componentPanel
http://imagepaste.nullnetwork.net/img/1354548433miglayout3.jpg
Using BorderLayout instead of BoxLayout seem better...
leftPanel.setLayout(new BorderLayout());
leftPanel.add(componentPanel, BorderLayout.NORTH);
leftPanel.add(propertyPanel,BorderLayout.SOUTH);
BoxLayout problem?
As a part for my first GUI App I want to show 2 Tables the following way:
http://i.stack.imgur.com/6mq0m.jpg
I´m unable to print the 2 tables that way!
Here is my code so far:
// Center
JPanel panel_center = new JPanel();
panel_overview.add(panel_center, BorderLayout.CENTER);
panel_center.setLayout(new BorderLayout());
JPanel panel_center_table = new JPanel();
panel_center.add(panel_center_table, BorderLayout.NORTH);
panel_center_table.setLayout(new GridLayout(2, 1));
JPanel panel_table_north = new JPanel();
panel_center_table.add(panel_table_north);
JPanel panel_table_south = new JPanel();
panel_center_table.add(panel_table_south);
JPanel panel_center_combobox = new JPanel();
panel_center.add(panel_center_combobox, BorderLayout.NORTH);
panel_center_combobox.setLayout(new BorderLayout());
panel_center_combobox.add(combobox_table_chooser, BorderLayout.WEST);
Consider using a different layout manager. I'd recommend using a BoxLayout, since you'll be able to easily stack components on top of each other. Here's a How To Use BoxLayout tutorial.
You could also try using a split pane. That way, the user can control the height of each of the tables.