I am trying to put into my JPanel 6 JButtons in such a way that there will be 3 JButtons in one line and beneath another 3 JButtons. Since I know that explicitly JPanel works with Flow Layout Manager, I got such an idea:
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.getContentPane().add(panel);
JButton button1 = new JButton();
JButton button2 = new JButton();
JButton button3 = new JButton();
JButton button4 = new JButton();
JButton button5 = new JButton();
JButton button6 = new JButton();
button1.setSize((panel.WIDTH)/3,(panel.HEIGHT)/2);
button2.setSize((panel.WIDTH)/3,(panel.HEIGHT)/2);
button3.setSize((panel.WIDTH)/3,(panel.HEIGHT)/2);
button4.setSize((panel.WIDTH)/3,(panel.HEIGHT)/2);
button5.setSize((panel.WIDTH)/3,(panel.HEIGHT)/2);
button6.setSize((panel.WIDTH)/3,(panel.HEIGHT)/2);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
panel.add(button6);
Unfortunately, this does not work, I can not succeed in changing the size of the button. Does anybody have an idea? Thank you so much.
this is job for GridLayout
override getPreferredSize for JPanel
call JFrame.pack(); before JFrame.setVisible(true);
see Initial Thread
You should use a GridLayout for that, if you want more control you can also use GridBagLayout.
If you do not want to use any layout manager, then first make frame.setLayout(null) and also use button1.setBounds(x,y,width,height) for every button instead of setSize(). Lastly frame.setVisible(true).
You should check out some other Layouts, such as the GridLayout, or the GridBagLayout.
This tutorial, from Oracle, might prove useful.
However, when working with Layouts, use setPreferredSize(Dimension size), instead of setSize.
GridLayout is a good idea but if you don't plan to add more components to the panel or only in group of 3, SpringLayout with its makeGrid methods would do the trick too.
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 really new to GUI programming in Java so please forgive me if this code is really basic. In short, I want to have 2 panels that are the same design. After I press the "A" button on the panel 1, I want to make panel 2 appear with the same design. Making the GUI efficient or pretty doesn't currently matter to me. I just want it to work. I have parts of the code listed below.
JButton buttonA = new JButton("a");
JButton buttonB = new JButton("b");
JButton buttonC = new JButton("c");
JButton buttonD = new JButton("d");
JPanel pan1 = new JPanel();
JPanel pan2 = new JPanel();
setTitle ("Test");
setSize (640, 640);
setResizable(false);
GridLayout grid1 = new GridLayout();
setLayout (grid1);
FlowLayout flow1 = new FlowLayout();
pan1.setLayout (flow1);
pan1.add(buttonA);
pan1.add(buttonB);
pan1.add(buttonC);
pan1.add(buttonD);
buttonA.addActionListener(this);
buttonB.addActionListener(this);
buttonC.addActionListener(this);
buttonD.addActionListener(this);
FlowLayout flow2 = new FlowLayout();
pan2.setLayout (flow2);
pan2.add(buttonA);
pan2.add(buttonB);
pan2.add(buttonC);
pan2.add(buttonD);
add(pan1);
add(pan2);
pan1.setVisible(true);
pan2.setVisible(false);
setVisible(true);
public void actionPerformed(ActionEvent event) {
if (command.equals("a")){//i want to show the panel 2 after button a is pressed
System.out.println("HelloA");
pan1.setVisible(false);
pan2.setVisible(true);
}
Currently, it just shows nothing in the window. Any help guys?
Short answer is, you can't.
Long answer is, a component can only reside on a single parent. Adding a component to a second container will automatically remove it from the first container before its added to the new one.
Instead, you will need to create individual buttons for both containers.
Also, understand that BorderLayout can't support what you're trying to do, it will only manage one component at a time (in each of the 5 available positions)
A better solution would be to make use of the CardLayout which is designed to facilitate the action you are trying to achieve
I'm kind of new to the whole "how to arrange your components" thing in JAVA and I couldn't figure out how to realise the following JFrame (I can't post images so I just put the link)
I tried to be as precise as possible about what I already did.
I would like your advice about how to arrange the green part.
Thanks!
EDIT: as some people rightfully said, I didn't put the code of what I did. Here it is:
public Frame(){
this.setTitle("Small application");
this.setSize(445, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
JPanel container = new JPanel();
container.setLayout(new BorderLayout());
//Title
JLabel title = new JLabel("Welcome to this application");
title.setHorizontalAlignment(JLabel.CENTER);
title.setPreferredSize(new Dimension(200,50));
title.setFont(new Font("Courrier",Font.BOLD,20));
container.add(title, BorderLayout.NORTH);
//Center part
JPanel centerPart = new JPanel();
JLabel cell1 = new JLabel("Enter all measurements:");
cell1.setPreferredSize(new Dimension(150,20));
JLabel cell2 = new JLabel("Please, select the files...");
cell2.setPreferredSize(new Dimension(150,20));
cell2.setBackground(Color.white);
cell2.setBorder(BorderFactory.createLineBorder(Color.black));
cell2.setOpaque(true);
JButton cell3 = new JButton("Browse");
cell3.setPreferredSize(new Dimension(100,20));
centerPart.add(cell1);
centerPart.add(cell2);
centerPart.add(cell3);
container.add(centerPart, BorderLayout.CENTER);
/*
* I need your help here :)
* I can't figure out how to put the image and the text next to it
*/
//Bottom part
JPanel bottom = new JPanel();
JButton graph = new JButton("Graph");
JButton exit = new JButton("Exit");
bottom.add(graph);
bottom.add(exit);
container.add(bottom, BorderLayout.SOUTH);
this.setContentPane(container);
}
For most practical cases, you use multiple, nested containers, with a LayoutManager suited to the layout within each container.
Each LayoutManager does one specific job, in practice you often want differnt regions of a UI layouted in different ways. So for each region use a separate Container (e.g. JPanel) and set a LayoutManager that suits your layout requirements.
The big hurdle for beginners seems to be to get the point that LayoutManagers can (and often must) be used with nested containers.
Try using a JPanel
Create the JPanel
Place your JPanel into your JFrame
Position the labels, button, textfield onto the newly created JPanel.
It should do the trick. and it is pretty basic. You should be able to do the code on your own!
I have an applet and I want to add a jbutton. The problem is the button is too big, I already used the setSize() method but still it doesn't work. Perhaps the setting of setSize could might be wrong.
could someone got an idea about this problem?
Thanks...
private JButton newGame = new JButton("New Game");
private JButton players = new JButton("Players");
private JButton quit = new JButton("Quit");
public void init()
{
Container content = getContentPane();
content.setLayout(new BorderLayout());
mainPanel = new JPanel();
getContentPane().add(mainPanel);
setVisible(true);
setSize(400, 400);
content.add(newGame);
content.add(players);
content.add(quit);
}
Please check the layout manager you are using for your container (panel, frame, applet..). This plays a major role in defining the size & position of components (like JButton).
See also in the Java Tutorial for more details:
Using Layout Managers.
The Laying Out Components Within a Container lesson.
Add JPanel into your applet and then add JButton to it
The tree code conception is
JApplet (GridLayout) <- JPanel (FlowLayout) <- JButton (setSize(new Dimension(x,y)))
Good Luck
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()