Hi everybody!
Help me plz to figure out obe little thing. so imagine little window with 4 buttons and a JLable. I want to ask how to add JLable to the panel and not give it a Grid Layout. I mean that I want my JLable be under 4 buttons. But it should not be in the table.

******************************
* Button 1 * Button 2 *
* * *
******************************
* Button 3 * Button 4 *
* * *
******************************
* Here should be my JLable *
* *
******************************

public class ButtonPanel extends JPanel{
	
	private ButtonsModel bm;
	private JLabel label;
	
	public ButtonPanel() {
		GridLayout gl = new GridLayout(2,2);
		this.setLayout(gl);
		bm = new ButtonsModel();
		
		JButton button_1 = new JButton("1");
		JButton button_2 = new JButton("2");
		JButton button_3 = new JButton("3");
		JButton button_4 = new JButton("4");
		label = new JLabel("" + bm.getCurrentButton());
		
		this.add(button_1);
		this.add(button_2);
		this.add(button_3);
		this.add(button_4);
		this.add(label);
		
		Listener listener = new Listener();
		button_1.addActionListener(listener);
		button_2.addActionListener(listener);
		button_3.addActionListener(listener);
		button_4.addActionListener(listener);
	}

Recommended Answers

All 3 Replies

Create two panels, one with the four buttons and a separate panel with the label. Then add both panels to your main panel. This allows you to use different layout managers in each panel to get the exact result you want.

Alternatively, you could use a GridBagLayout to do the whole thing, but the learning curve is quite steep.

commented: great minds thinks alike !!!, I didn't saw your answer :-) +11

1) basic stuff (valid for part of programing languages) is use nested layout

- put 4 JButtons to the JPanel layed by GridLayout,

- put this JPanel put to the JFrame myFrame.add(myFirstPanel, BorderLayout.CENTER)

- put JLabel to the another JPanel (without declaring for LayourManager is there FlowLayout and JLabel will be centered in the JPanel)

- put this second JPanel to the JFrame myFrame.add(my2ndPanel, BorderLayout.SOUTH)

2) by using GridBagLayout or MigLayout

thank you very muuch guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.