Creating two buttons at bottom left/right corner - java

JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new BorderLayout());
button2.setLayout(new FlowLayout(FlowLayout.RIGHT));
button1.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.getContentPane().add(button1,BorderLayout.SOUTH);
frame.getContentPane().add(button2,BorderLayout.SOUTH);
frame.setSize(500,500);
frame.setVisible(true);
I'm trying to make Button 1 on the bottom left corner and Button 2 on the bottom right corner
__________________________
| |
| |
| |
| |
| |
| |
|Button1 Button2 |
|________________________|

You might want to consider using BoxLayout's horizontalGlue:
import java.awt.BorderLayout;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ButtonsLeftAndRight {
private JFrame frame;
private JPanel pane;
private JButton button1;
private JButton button2;
public static void main(String[] args) {
SwingUtilities.invokeLater(new ButtonsLeftAndRight()::createAndShowGui);
}
public void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());
pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.LINE_AXIS));
button1 = new JButton("Button1");
button2 = new JButton("Button2");
pane.add(button1);
pane.add(Box.createHorizontalGlue());
pane.add(button2);
frame.add(pane, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
This might get you this, before and after resizing:

Create a container for both.
JPanel south = new JPanel(new AxisLayout(AxisLayout.HORIZONTAL));
south.add(button1);
south.add(button2);
frame.getContentPane().add(south, BorderLayout.SOUTH);
Obs: Sorry dont remember exactly Swing layout manangers, but you will find the AxisLayout to solve this

Another alternative is to use the GUI builder, and modify the code accordingly.
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JFrame frame = new JFrame();
GroupLayout layout = new GroupLayout(frame.getContentPane());
frame.getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(button1)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 215, Short.MAX_VALUE)
.addComponent(button2)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(256, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(button1)
.addComponent(button2))
.addGap(25, 25, 25))
);
frame.setSize(500, 500);
frame.setVisible(true);

You can add a jPanel and then add the two buttons to it, and then call setBounds on the buttons and specify the position. Then add the jPanel to the jFrame.
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JFrame frame = new JFrame();
JPanel p = new JPanel();
p.setLayout(null);
button1.setBounds(10, 400, 100, 40);
p.add(button1);
button2.setBounds(375, 400, 100, 40);
p.add(button2);
frame.getContentPane().add(p);
frame.setSize(500, 500);
frame.setVisible(true);
The bounds are set as (x-coord, y-coord, width, height).

Related

Java JPanel GroupLayout formatting problem

I am trying to layout my JPanel such that:
The information label is at the top,
The text area is directly below, this and the info label go across (horizontally) the entire panel,
Below the text from left to right (equally spaced) are the config, save and then clear buttons,
Below those is the success/failure label (central, under the save button) and the home button (right, under the clear button).
I have tried many combinations and used other stack answers but cannot get it right, something about group layout I can't get my head around!
import javax.swing.*;
public class JFrameTest {
private static JFrame mainApp;
private static JPanel mainPanel;
public JFrameTest() {
mainApp = new JFrame("Application");
mainApp.setSize(640, 480);
mainApp.setLocationRelativeTo(null);
mainApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainApp.add(mainPanel());
mainApp.setVisible(true);
}
private JPanel mainPanel() {
JFrame.setDefaultLookAndFeelDecorated(true);
mainPanel= new JPanel();
mainPanel.setSize(600,450);
Container container = mainApp.getContentPane();
JLabel labelInfo = new JLabel("add necessary information here");
JLabel labelSOrF = new JLabel("Success/Failure");
// labelSOrF.setVisible(false);
JTextArea textArea = new JTextArea();
JButton configButton= new JButton("config");
JButton saveButton= new JButton("save");
JButton clearButton= new JButton("clear");
JButton homeButton= new JButton("Home");
GroupLayout layout = new GroupLayout(container);
container.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup()
.addComponent(configButton)
.addGroup(layout.createParallelGroup()
.addComponent(saveButton)
.addComponent(labelSOrF, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup()
.addComponent(clearButton)
.addComponent(homeButton)
.addComponent(labelInfo)
.addComponent(textArea, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
)))
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addComponent(labelInfo)
.addComponent(textArea, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup()
.addGroup(layout.createSequentialGroup()
.addComponent(configButton)
.addComponent(saveButton)
.addComponent(buttonClear))
.addGroup(layout.createParallelGroup()
.addComponent(labelSOrF, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(homeButton)))
);
edit - added code
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.
GroupLayout was designed for GUI builders. No human being will deliberately use a GroupLayout. It's too hard to understand and as you've discovered, maintain.
Here's a GUI that meets your requirements.
Swing was designed to be constructed from the inside out. You layout the Swing components and let the JPanels and the JFrame size themselves. You don't start with the JFrame and fit all the Swing components.
All Swing applications must start with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.
I created a main JPanel with three subordinate JPanels. To construct a complex GUI, you nest simple JPanels.
I added the Swing components to each JPanel in column, row order. That helps me to organize the code and makes it easier for readers of your code to understand the code.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class JFrameTest implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new JFrameTest());
}
#Override
public void run() {
JFrame mainApp = new JFrame("Application");
mainApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainApp.add(createMainPanel(), BorderLayout.CENTER);
mainApp.pack();
mainApp.setLocationRelativeTo(null);
mainApp.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(createTitlePanel());
panel.add(createTextAreaPanel());
panel.add(createButtonPanel());
return panel;
}
private JPanel createTitlePanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
JLabel labelInfo = new JLabel("add necessary information here");
panel.add(labelInfo);
return panel;
}
private JPanel createTextAreaPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
JTextArea textArea = new JTextArea(5, 40);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
panel.add(scrollPane);
return panel;
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel(new GridLayout(0, 3, 20, 5));
panel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
JButton configButton = new JButton("config");
panel.add(configButton);
JButton saveButton = new JButton("save");
panel.add(saveButton);
JButton clearButton = new JButton("clear");
panel.add(clearButton);
JLabel dummy = new JLabel(" ");
panel.add(dummy);
JLabel labelSOrF = new JLabel("Success/Failure");
panel.add(labelSOrF);
JButton homeButton = new JButton("Home");
panel.add(homeButton);
return panel;
}
}

Can't add scroll bar to JTextarea

Hello Everyone I am new to Java so I am sure I am doing something obviously wrong here but I just keep going in circles.
I am trying to add a scroll to a JTextarea. Here is what I have tried but it doesn't show up.
textarea1 = new JTextArea();
textarea1.setBounds(251,26,795,345);
textarea1.setBackground(new Color(255,255,255));
textarea1.setForeground(new Color(0,0,0));
textarea1.setEnabled(true);
textarea1.setFont(new Font("sansserif",0,12));
textarea1.setText("");
textarea1.setBorder(BorderFactory.createBevelBorder(1));
textarea1.setVisible(true);
JScrollPane ScrollPane1 = new JScrollPane(textarea1);
ScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//adding components to contentPane panel
contentPane.add(browseFileOne);
contentPane.add(browseOutput);
contentPane.add(button1);
contentPane.add(button2);
contentPane.add(button4);
contentPane.add(fileOneText);
contentPane.add(fileTwoText);
contentPane.add(label1);
contentPane.add(label2);
contentPane.add(label3);
contentPane.add(outputTextFile);
contentPane.add(textarea1);
see working example:
https://repl.it/repls/DimpledDefensiveSourcecode
Code:
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame {
public static void main(String[] args) {
new Main();
}
public Main() {
this.setSize(400, 400);
this.setLocation(0, 0);
this.setResizable(false);
this.setTitle("Application");
JPanel painel = new JPanel(null);
// Creating the Input
JTextField tf1 = new JTextField("Some random text", 15);
tf1.setBounds(5, 5, this.getWidth() - 120, 20);
tf1.setColumns(10);
tf1.setText("Omg");
// resultsTA,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
painel.add(tf1);
// Creating the button
JButton button1 = new JButton("Send");
button1.setBounds(290, 5, 100, 19);
painel.add(button1);
// Creating the TextArea
JTextArea ta1 = new JTextArea(15, 20);
JScrollPane scr = new JScrollPane(ta1,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);// Add your text area to scroll pane
ta1.setBounds(5, 35, 385, 330);
ta1.setLineWrap(true);
ta1.setWrapStyleWord(true);
scr.setBounds(20, 30, 100, 40);// You have to set bounds for all the controls and containers incas eof null layout
painel.add(scr);// Add you scroll pane to container
this.add(painel);
this.setVisible(true);
}
}

Dock objects in the corner - MigLayout

How to dock three or more components in the corner eg. south-east using MigLayout ?
What I' ve tried:
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;
public class App {
public static void launchView(){
JFrame frame = new JFrame("Foo");
frame.setLayout(new MigLayout());
JButton b1 = new JButton("Sample1");
JButton b2 = new JButton("Sample2");
JButton b3 = new JButton("Sample3");
frame.add(b1, "dock south, east");
frame.add(b2);
frame.add(b3);
frame.setSize(new Dimension(600, 200));
frame.setVisible(true);
}
public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
launchView();
}
});
}
}
In the picture you can see buttons showed in the left corner in the wrong order, but I would like to have them always in the south-west corner, one next to another (Button1 | Button2 | Button3).
Try using SceneBuilder, a plug in available for Eclipse and NetBeans.You can easily construct GUI's using that tool.
A potential solution for yourself is to use this:
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new MigLayout("", "[grow,right]", "[grow,bottom]"));
JButton btnNewButton = new JButton("New button");
frame.getContentPane().add(btnNewButton, "flowx,cell 0 0");
JButton btnNewButton_1 = new JButton("New button");
frame.getContentPane().add(btnNewButton_1, "cell 0 0");
JButton btnNewButton_2 = new JButton("New button");
frame.getContentPane().add(btnNewButton_2, "cell 0 0");

Why JComponents does not appear in the frame?

I'm pretty new to java. And i can't get this to work... I'm trying to add components using this code:
public class Board
{
public static void main(String[] args) {
JFrame window = new JFrame("Tellraw Generator");
window.setVisible(true);
window.setSize(400, 600);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel label = new JLabel();
panel.setLayout(null);
window.add(panel);
//"Generate" Button
JButton button1 = new JButton("Generate");
button1.setBounds(262, 485, 100, 37);
panel.add(button1);
//"Add Text" Button
JButton button2 = new JButton("Add Text");
button2.setBounds(51, 337, 88, 33);
panel.add(button2);
//Title
JLabel txt1 = new JLabel("Tellraw Generator");
txt1.setFont(new Font("Minecrafter Alt Regular", Font.BOLD, 29));
txt1.setBounds(61, 18, 278, 30);
panel.add(txt1);
}
}
But when i'm trying to do it, the components aren't showing up on the screen.
So are there someone who can tell me why it isn't working/Showing up and how i can add it in ?
Thanks
You mean that JLabel and JButton don't appear? right ? not JTextField because there is no JTextField in the code
Anyway just add this line of code to the end:
window.add(panel);
Here you are adding the JPanel that contains all the JComponents to the JFrame
and always: have the setVisible(true) call as last call. Everything that you add to the window/frame must be done before the setVisible call
This is how to declare a JTextField
final JTextField variableName = new JTextField(size);
This is how you get the text from JTextField
variableName.getText()
Hope that helps.
Try this:
public class Board
{
public static void main(String[] args) {
JFrame window = new JFrame("Tellraw Generator");
window.setVisible(true);
window.setSize(400, 600);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel label = new JLabel();
panel.setLayout(null);
window.add(panel);
//"Generate" Button
JButton button1 = new JButton("Generate");
button1.setBounds(262, 485, 100, 37);
panel.add(button1);
//"Add Text" Button
JButton button2 = new JButton("Add Text");
button2.setBounds(51, 337, 88, 33);
panel.add(button2);
//Title
JTextField txt1 = new JTextField ();
txt1.setFont(new Font("Minecrafter Alt Regular", Font.BOLD, 29));
txt1.setBounds(61, 18, 278, 30);
panel.add(txt1);
}
}

write a string on a jpanel centered

I would write on a JPanel a String much bigger respect than other element.which could be the way to paint a string in terms of simply?There is a method to do this?
You could add the text as a JLabel component and change its font size.
public static void main(String[] args) {
NewJFrame1 frame = new NewJFrame1();
frame.setLayout(new GridBagLayout());
JPanel panel = new JPanel();
JLabel jlabel = new JLabel("This is a label");
jlabel.setFont(new Font("Verdana",1,20));
panel.add(jlabel);
panel.setBorder(new LineBorder(Color.BLACK)); // make it easy to see
frame.add(panel, new GridBagConstraints());
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(NewJFrame1.EXIT_ON_CLOSE);
frame.setVisible(true);
}
The code will run and look like this :
see also Java layout manager vertical center for more info
JLabel supports HTML 3.2 formating, so you can use header tags if you don't want to mess with fonts.
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class HtmlHeadersSample extends JFrame {
public HtmlHeadersSample() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,200);
setLocation(100, 100);
JLabel label1 = new JLabel();
label1.setText("simple text");
label1.setBounds(0, 0, 200, 50);
JLabel label2 = new JLabel();
label2.setText("<html><h1>header1 text</h1></html>");
label2.setBounds(0, 20, 200, 50);
JLabel label3 = new JLabel();
label3.setText("<html><h2>header2 text</h2></html>");
label3.setBounds(0, 40, 200, 50);
JLabel label4 = new JLabel();
label4.setText("<html><h3>header3 text</h3></html>");
label4.setBounds(0, 60, 200, 50);
add(label1);
add(label2);
add(label3);
add(label4);
setVisible(true);
}
public static void main(String[] args) {
new HtmlHeadersSample();
}
}
Here's how it looks like:
Just set the size of your font
JLabel bigLabel = new JLabel("Bigger text");
bigLabel.setFont(new Font("Arial", 0, 30));

Categories