Java Swing Borders to textfields and buttons - java

I have a Swing UI which contains 6 text fields and labels for the input and 1 button and texfield to show the output. now I want to make a border around these two.
I have read some materials regarding Titled borders but I think its only for single elements. Please suggest.

You could make a JPanel with a titled border, then put however many components you wanted in the JPanel using the content manager of your choice.
An example:
JPanel myPanel = new JPanel();
myPanel.setBorder(new TitledBorder(null, "My Title", TitledBorder.LEADING, TitledBorder.TOP, null, null));
myPanel.setLayout(new GridLayout(1, 0, 0, 0));
JButton button = new JButton("New button");
myPanel.add(button);
JLabel label = new JLabel("New label");
myPanel.add(label);

You can add that last 2 components to a JPanel and then add that panel to main frame. Now you can give border to JPanel and it will around 2 components inside it.
To give border to jPanel you can use following:
JPanel pane = new JPanel();
pane.setBorder(BorderFactory.createLineBorder(Color.black));
If you want titled border then you can use following:
pane.setBorder(BorderFactory.createTitledBorder(BorderFactory
.createMatteBorder(5, 5, 5, 5, Color.blue), "Title",
TitledBorder.LEFT, TitledBorder.TOP));
Reference: http://download.oracle.com/javase/tutorial/uiswing/components/border.html

Related

Java Swing - MigLayout: Docking a component in center isn't fully centering

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.

how to stop JButton from changing position when a Jlabel is added before it

I have a JLabel (seriesInformationLabel) that is initially blank. There is a Jbutton (copyButton) next to it. Problem is everytime the JLabel loads a text value, the JButton moves to the right. How do I stop this JButton from moving?
private void init() {
super.initializeLayout();
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
Box buttonPanel = Box.createHorizontalBox();
buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
buttonPanel.setLayout(null);
copyButton = new JButton(Utilities.getString("COPY"));
copyButton.setActionCommand(COPY);
copyButton.setEnabled(false);
seriesInformationLabel = new JLabel();
seriesInformationLabel.setAlignmentY(CENTER_ALIGNMENT);
seriesInformationLabel.setName(this.getClass().getSimpleName() + "_seriesInformationLabel");
buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
buttonPanel.add(seriesInformationLabel);
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(copyButton);
buttonPanel.add(Box.createHorizontalGlue());
}
buttonPanel.add(seriesInformationLabel);
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(copyButton);
buttonPanel.add(Box.createHorizontalGlue());
Well the free space changes as the text of the label changes.
This means that the space available for the "glue" will change equally between the two glue components causing the button to shift.
If you don't want the button to move then you need to get rid of the second "glue" component:
buttonPanel.add(seriesInformationLabel);
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(copyButton);
//buttonPanel.add(Box.createHorizontalGlue());
Maybe instead add another rigid area if you don't want the button completely at the right edge of the panel.

Java Swing: How to remove the default-spacing for JButtons in a JPanel

I'm busy writing a button menu for a Java Swing application and I am wondering if it is possible to remove the padding between JButtons that are added to a JPanel.
The JPanel uses a FlowLayout that is aligned left.
JPanel panelMenu = new JPanel(new FlowLayout(FlowLayout.LEFT));
The buttons are standard JButtons
JButton buttOne = new JButton("One");
JButton buttTwo = new JButton("Two");
I added the JButtons to the panel as normal
add(panelMenu, BorderLayout.NORTH);
panelMenu.add(buttOne);
panelMenu.add(buttTwo);
Everything works as expected but what do I need to do to remove the default spacing between the buttons?
I found a suggested solution online which is the following
buttOne.setBorder(null);
buttOne.setBorderPainted(false);
buttOne.setMargin(new Insets(0,0,0,0));
buttTwo.setBorder(null);
buttTwo.setBorderPainted(false);
buttTwo.setMargin(new Insets(0,0,0,0));
However this seems to remove the spacing inside of the button and not the spacing between each button.
Is this spacing produced by the FlowLayout? If so, how can I remove it?
// 0, 0 equates to horizontal and vertical offsets, the default is 5.
JPanel panelMenu = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
Should sort it!
The FlowLayout controls the spacing, the default is 5.
Use new FlowLayout(FlowLayout.LEFT, 0) to remove the spacing.

Custom layout in frame

When i add another JPanel into frame previous will dissapper (or probably overlaps the previous one). How can i stop this overlapping?
public Attack() {
JFrame frame = new JFrame("Oracle Padding Attack");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.setLayout(null);
JLabel label1 = new JLabel("Inicialization vector:");
createTextField(10, 40, arrayIV, panel1, "HH", true, false);
label1.setBounds(10, 0, 120, 50);
panel1.add(label1);
panel2.setLayout(null);
JLabel label2 = new JLabel("Encrypted text:");
createTextField(400, 40, encryptedTextArray, panel2, "00", true, false);
label2.setBounds(400, 0, 120, 50);
panel2.add(label2);
frame.add(panel1);
frame.add(panel2);
frame.setSize(900, 400);
frame.setVisible(true);
}
It depends on how you want your two panels placed inside your frame. You will need a layout for your frame.
If you want to be able to "jump" from one to the other, cardLayout is your answer.
Otherwise if you want both side to side for example, you will have to use a layout inside your frame. I like the MigLayout, but GridLayout will do the job just fine.
http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html
All build in layout handlers can be found here: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Also it's not recommended to use null layout at all since it will break the look if the window is resized.

Organizing JPanels and Layouts

So I have the following screen:
And this is my code:
setLayout(new BorderLayout());
JLabel lblTitulo = new JLabel("Sistema Generador de Examenes");
lblTitulo.setFont(new Font("Tahoma", Font.BOLD, 18));
JPanel panel1 = new JPanel();
panel1.setBackground(Color.white);
panel1.add(lblTitulo);
add(panel1, BorderLayout.NORTH);
JButton btnCrear = new JButton("Crear Examen");
JButton btnRendir = new JButton("Rendir Examen");
JButton btnCorregir = new JButton("Corregir Examen");
JButton btnVerCorrecciones = new JButton("Ver Correcciones");
btnCrear.setBounds(15, 100, 450, 35);
btnRendir.setBounds(15, 150, 450, 35);
btnCorregir.setBounds(15, 200, 450, 35);
btnVerCorrecciones.setBounds(15, 250, 450, 35);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.white);
panel2.setLayout(null);
panel2.add(btnCrear);
panel2.add(btnRendir);
panel2.add(btnCorregir);
panel2.add(btnVerCorrecciones);
add(panel2, BorderLayout.CENTER);
1 - I'm using the BorderLayout. Do I need to have 2 JPanels to separate components (JLabel and JButtons) if I want to have the JLabel in the North and the JButtons in the Center? Or is there any way to use just one JPanel?
2 - I want to take out the setBounds used in my JButtons and use some Layout in order to have my JButtons like that in the middle of the screen. How could I do that?
I'm using the BorderLayout. Do I need to have 2 JPanels to separate components (JLabel and JButtons) if I want to have the JLabel in the North and the JButtons in the Center? Or is there any way to use just one JPanel?
Yes, you could use one JPanel and a GridBagLayout with a single column and some Insets to space the buttons from the label.
However, the nested layouts will keep the buttons in the center no matter how you resize the JFrame.
I want to take out the setBounds used in my JButtons and use some Layout in order to have my JButtons like that in the middle of the screen. How could I do that?
The GridBagLayout will space out the buttons with insets.
See this article, Sudoku Solver Swing GUI, for a couple of examples of dialogs that use the GridbagLayout.

Categories

Resources