How to make two checkboxes sit next to each other in Swing? - java

I have two JCheckBox's and one JEditorPane. I am looking for an output as under
But my current code is somewhat messy for which I am not able to
private void createContents()
{
JEditorPane license;
JCheckBox confirmBox;
JCheckBox declineBox;
license = new JEditorPane("text/html", "");
license.setText (buildEulaText());
license.setEditable(false);
confirmBox = new JCheckBox("I accept.", false);
declineBox = new JCheckBox("I decline.", false);
add(license, BorderLayout.CENTER);
add(confirmBox, BorderLayout.SOUTH);
add(declineBox, BorderLayout.NORTH); //I know this is wrong
}

A simple solution is to compose the layouts using a new JPanel with a FlowLayout.
add(license, BorderLayout.CENTER);
JPanel boxes = new JPanel(new FlowLayout());
// FlowLayout is the JPanel default layout manager, so
// boxes = new JPanel(); works too :)
boxes.add(confirmBox);
boxes.add(declineBox);
add(boxes, BorderLayout.SOUTH);
But you can also take a look at the GridBagLayout.

Create a new JPanel to hold all the checkboxes, then add this to your panel/frame.
JPanel checkBoxesPane = new Panel();
checkBoxesPane.add( confirmBox );
checkBoxesPane.add( declineBox );
add( checkBoxes, BorderLayout.SOUTH );

First of all I would highly recommend to do it in javafx rather than in Java swing. It is the later Technology and much better according to my opinion.
If you still want to do it in Java swing, here is the code:
JPanel panel = new Panel();
panel.add(confirmBox);
panel.add(declineBox);
add(panel, BorderLayout.SOUTH);
I didn't test the code, but it should work with this code.

Related

Create a blue colored rectangular java bean [duplicate]

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()

How to create a panel with an organized "list" of information in Swing

I'm trying to create a panel that has information on it.
I've tried using the setBorderLayout to add the labels with the information to certain areas of the panel, but I know this doesn't work as it just overwrites what I already had before.
Here is an example from my code:
final JFrame fr = new JFrame("ATCGUI");
fr.setLayout(new BorderLayout());
JTabbedPane tabbedPane = new JTabbedPane();
JPanel tab1 = new JPanel();
tab1.setLayout(new BorderLayout());
tabbedPane.addTab("Airport", tab1);
JLabel labelAirportName = new JLabel(airportInfo[0]);
tab1.add(labelAirportName, BorderLayout.NORTH);
JLabel labelAirportCodeStatic = new JLabel("Code:");
JLabel labelAirportLocationStatic = new JLabel("Location:");
JLabel labelAirportCoordinatesStatic = new JLabel("Coordinates:");
JLabel labelAirportAltitudeStatic = new JLabel("Altitude:");
JLabel labelAirportTimezoneStatic = new JLabel("Timezone:");
JLabel labelAirportICAOStatic = new JLabel("ICAO:");
tab1.add(labelAirportCodeStatic, BorderLayout.WEST);
tab1.add(labelAirportLocationStatic, BorderLayout.WEST);
tab1.add(labelAirportCoordinatesStatic, BorderLayout.WEST);
tab1.add(labelAirportAltitudeStatic, BorderLayout.WEST);
tab1.add(labelAirportTimezoneStatic, BorderLayout.WEST);
tab1.add(labelAirportICAOStatic, BorderLayout.WEST);
This ends up creating something like this:
When I am aiming for something that looks more like this:
I created this with the SwingUI designer but due to compatibility I am switching over to just Swing
Following the Oracle Swing tutorial linked by maloomeister https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
I was able to create the layout I wanted by mixing BorderLayout and GridLayout.

Which Layout should be used?

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!

Centering a JLabel on a JPanel

I'm using the NetBeans GUI builder to handle my layout (I'm terrible with LayoutManagers) and am trying to place a simple JLabel so that it is always centered (horizontally) inside its parent JPanel. Ideally, this would maintain true even if the JPanel was resized, but if that's a crazy amount of coding than it is sufficient to just be centered when the JPanel is first created.
I'm bad enough trying to handle layouts myself, but since the NetBeans GUI Builder autogenerates immutable code, it's been impossible for me to figure out how to do this centering, and I haven't been able to find anything online to help me.
Thanks to anybody who can steer me in the right direction!
Here are four ways to center a component:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class CenterComponent {
public static JLabel getLabel(String text) {
return getLabel(text, SwingConstants.LEFT);
}
public static JLabel getLabel(String text, int alignment) {
JLabel l = new JLabel(text, alignment);
l.setBorder(new LineBorder(Color.RED, 2));
return l;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JPanel p = new JPanel(new GridLayout(2,2,4,4));
p.setBackground(Color.black);
p.setBorder(new EmptyBorder(4,4,4,4));
JPanel border = new JPanel(new BorderLayout());
border.add(getLabel(
"Border", SwingConstants.CENTER), BorderLayout.CENTER);
p.add(border);
JPanel gridbag = new JPanel(new GridBagLayout());
gridbag.add(getLabel("GridBag"));
p.add(gridbag);
JPanel grid = new JPanel(new GridLayout());
grid.add(getLabel("Grid", SwingConstants.CENTER));
p.add(grid);
// from #0verbose
JPanel box = new JPanel();
box.setLayout(new BoxLayout(box, BoxLayout.X_AXIS ));
box.add(Box.createHorizontalGlue());
box.add(getLabel("Box"));
box.add(Box.createHorizontalGlue());
p.add(box);
JFrame f = new JFrame("Streeeetch me..");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(p);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
});
}
}
By using Borderlayout, you can put any of JComponents to the CENTER area. For an example, see an answer to Stack Overflow question Get rid of the gap between JPanels. This should work.
Even with BoxLayout you can achieve that:
JPanel listPane = new JPanel();
listPane.setLayout(new BoxLayout(listPane, BoxLayout.X_AXIS ));
JLabel label = new JLabel();
listPane.add(Box.createHorizontalGlue());
listPane.add(label);
listPane.add(Box.createHorizontalGlue());
mKorbel's solution is perfect for your goal. Anyway I always like to suggest BoxLayout because it's very flexible.
Mara: "thanks for your response, however the NetBeans GUI Build uses GroupLayout and this is not overridable."
Not true! Right click anywhere inside JFrame (or any other GUI container) in NetBeans GUI builder and select "Set Layout". By default is selected "Free Design", which is Group layout, but you can select any other layout including Border layout as advised by mKorbel.
There's many ways to do this, depending on the layout manager(s) you use. I suggest you read the Laying Out Components Within a Container tutorial.
I believe the following will work, regardless of layout manager:
JLabel.setHorizontalAlignment(SwingConstants.CENTER)

set size wont work in java

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()

Categories