Reputation: 941
I'm creating an application to test system trays, but I don't know what layoutmanager I should use to make it look like this:
The problem I'm having is that I can't align the text in the textField vertically, so I thought something like: "Why not use layoutmanagers and make it scale with the message." What do you think about this?
Upvotes: 0
Views: 97
Reputation: 1458
One of the best Layout Manager that I use 1st GridBagLayout its more flexible to set the location of your Palette (Swing Containers, Controls)
public class GridBagLayoutSample {
JFrame frame = new JFrame("GridBagSample");
JPanel panel = new JPanel();
JButton btn1 = new JButton("One");
JButton btn2 = new JButton("Two");
JButton btn3 = new JButton("Three");
JButton btn4 = new JButton("Four");
JButton btn5 = new JButton("Five");
public GridBagLayoutSample(){
panel.setLayout(new GridBagLayout());
GridBagConstraints a = new GridBagConstraints();
a.fill = GridBagConstraints.HORIZONTAL;
a.insets = new Insets(3,3,3,3);
a.gridx = 0;
a.gridy = 0;
panel.add(btn1, a);
a.gridx = 1;
a.gridy = 0;
panel.add(btn2, a);
a.gridx = 0;
a.gridy = 1;
panel.add(btn3, a);
a.gridx = 1;
a.gridy = 1;
panel.add(btn4, a);
frame.add(panel);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setPreferredSize(new Dimension(500,500));
panel.setSize(300,300);
}
public static void main(String[] args) {
GridBagLayoutSample a = new GridBagLayoutSample();
}
Check this documentation. It explain here how GridBagLayout used. This is the best layout that I used because you can locate any position you want. Use Quadrants as your guide so will not get confused positioning your component hope this helps.
https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
Upvotes: 0
Reputation: 1916
You could use BorderLayout
.
The simplest program I could come up with, to meet your needed positioning requirements is this:
public class PutText extends JFrame {
public PutText() {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
initGUI();
pack();
setLocationRelativeTo(null);
}
private void initGUI() {
Container cp = getContentPane();
cp.setLayout(new BorderLayout(0, 10));
JPanel upper = new JPanel(new BorderLayout());
JPanel lower = new JPanel(new BorderLayout());
cp.add(upper, BorderLayout.PAGE_START);
cp.add(lower, BorderLayout.PAGE_END);
JLabel lbl = new JLabel("Put the text you want the tray to show.");
JTextArea ta = new JTextArea();
ta.setLineWrap(true);
JButton btn = new JButton("Send");
upper.add(lbl, BorderLayout.LINE_START);
cp.add(ta, BorderLayout.CENTER);
lower.add(btn, BorderLayout.LINE_END);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new PutText().setVisible(true);
});
}
}
Note that you would need to add the empty borders to resemble the program you want to create.
Upvotes: 3
Reputation: 285450
Simple: Use a combination of layouts. BorderLayout for the overall, with JLabel in the PAGE_START position and your JScrollPane/JTextArea in the CENTER position. And a FlowLayout.RIGHT using JPanel in the PAGE_END position holding the JButton.
e.g.,
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.*;
@SuppressWarnings("serial")
public class FooPanel extends JPanel {
private static final String PROMPT = "This is prompt text:";
private static final int TA_ROWS = 10;
private static final int TA_COLS = 30;
private static final int GAP = 5;
private JTextArea textArea = new JTextArea(TA_ROWS, TA_COLS);
public FooPanel() {
JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
bottomPanel.add(new JButton("Submit"));
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
setLayout(new BorderLayout(GAP, GAP));
add(new JLabel(PROMPT), BorderLayout.PAGE_START);
add(new JScrollPane(textArea), BorderLayout.CENTER);
add(bottomPanel, BorderLayout.PAGE_END);
}
private static void createAndShowGui() {
FooPanel mainPanel = new FooPanel();
JFrame frame = new JFrame("FooPanel");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGui();
});
}
}
Alternatively, the bottom JPanel could use BoxLayout with horizontal glue pushing the button over:
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));
bottomPanel.add(Box.createHorizontalGlue());
bottomPanel.add(new JButton("Submit"));
An example using the above in a modal dialog, and showing how to wrap lines in your JTextArea:
import java.awt.BorderLayout;
import java.awt.Dialog.ModalityType;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class FooPanel extends JPanel {
private static final int TA_ROWS = 10;
private static final int TA_COLS = 30;
private static final int GAP = 5;
private JTextArea textArea = new JTextArea(TA_ROWS, TA_COLS);
public FooPanel(String prompt) {
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));
bottomPanel.add(Box.createHorizontalGlue());
bottomPanel.add(new JButton(new SendAction("Send", KeyEvent.VK_S)));
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
setLayout(new BorderLayout(GAP, GAP));
add(new JLabel(prompt), BorderLayout.PAGE_START);
add(new JScrollPane(textArea), BorderLayout.CENTER);
add(bottomPanel, BorderLayout.PAGE_END);
}
public String getText() {
return textArea.getText();
}
private class SendAction extends AbstractAction {
public SendAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic); // alt-key shortcut mnemonic
}
@Override
public void actionPerformed(ActionEvent e) {
// simply dispose of this window
Window win = SwingUtilities.getWindowAncestor(FooPanel.this);
win.dispose();
}
}
private static void createAndShowGui() {
String prompt = "Enter the text that is to be displayed in the tray:";
final FooPanel mainPanel = new FooPanel(prompt);
final JFrame frame = new JFrame("FooPanel");
final JTextArea displayArea = new JTextArea(TA_ROWS, TA_COLS);
displayArea.setFocusable(false);
displayArea.setEditable(false);
displayArea.setWrapStyleWord(true);
displayArea.setLineWrap(true);
final JDialog dialog = new JDialog(frame, "Enter Text", ModalityType.APPLICATION_MODAL);
dialog.add(mainPanel);
dialog.pack();
JPanel framePanel = new JPanel(new BorderLayout());
framePanel.add(new JScrollPane(displayArea), BorderLayout.CENTER);
framePanel.add(new JPanel() {
{
add(new JButton(new AbstractAction("Show Dialog") {
{
putValue(MNEMONIC_KEY, KeyEvent.VK_S);
}
public void actionPerformed(ActionEvent e) {
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
String text = mainPanel.getText();
displayArea.setText(text);
};
}));
}
}, BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(framePanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGui();
});
}
}
Upvotes: 4