Adding buttons below JTextArea - java

I am trying to add two buttons below the JTextArea using the Eclipse WindowBuilder, but I can't. I tried to change the layout, but I couldn't find a way to add buttons where I want and to re-size the JTextArea in an easy way.
public TestScrollPane03() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JTextArea textArea = new JTextArea(100, 50);
JScrollPane scrollPane = new JScrollPane(textArea);
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(scrollPane);
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
How would I go about adding buttons below my original textArea?

You need to have two panels, one for your textArea, and one for your input (in this case buttons). I think something like this is what you are looking for:
public class Test
{
public static void createFrame()
{
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setOpaque(true);
JTextArea textArea = new JTextArea(15, 50);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
textArea.setFont(Font.getFont(Font.SANS_SERIF));
JScrollPane scroller = new JScrollPane(textArea);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
JPanel inputpanel = new JPanel();
inputpanel.setLayout(new FlowLayout());
JTextField input = new JTextField(20);
JButton button = new JButton("Enter");
DefaultCaret caret = (DefaultCaret) textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
panel.add(scroller);
inputpanel.add(input);
inputpanel.add(button);
panel.add(inputpanel);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
frame.setResizable(false);
input.requestFocus();
}
});
}
public static void main(String... args)
{
createFrame();
}
}
If you want your frame to look more like those of the OS you are running on, you can add .setLookAndFeel() before you make the frame visible:
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
What adding the UIManager looks like (notably a bit smaller):

Related

JScrollPane component doesn't appear

I am trying to add a JList to my JPanel as it is stated by the following source code but it's not working :
public class Game {
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
DefaultListModel<String> defListPlayers = new DefaultListModel<String>();
defListPlayers.addElement("Player 1");
defListPlayers.addElement("Player 2");
JList<String> listPlayers = new JList<String>(defListPlayers);
listPlayers.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
listPlayers.setLayoutOrientation(JList.VERTICAL);
listPlayers.setVisibleRowCount(10);
JScrollPane scroller = new JScrollPane(listPlayers);
scroller.setViewportBorder(new LineBorder(Color.RED));
scroller.revalidate();
JPanel panel = new JPanel();
panel.add(scroller);
JFrame frame = new JFrame;
frame.setContentPane(panel);
frame.pack();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().repaint();
}
}
Any idea please why and what to do?

ScrollPane not showing on JLabel

When I run this program, I don't see a scrollbar on the Label. What am I missing?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Util1
{
public static void main(String[] args)
{
new Util1();
}
public Util1()
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ExamplePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class ExamplePane extends JPanel
{
public ExamplePane()
{
final JPanel panel = new JPanel(new GridBagLayout());
final JLabel message = new JLabel("<html>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello</html>");
message.setPreferredSize(new Dimension(500, 50));
JScrollPane scroller = new JScrollPane( message, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroller.setViewportView(message);
panel.add(scroller);
add(panel);
}
}
}
To see a scrollbar wrap the "message" JLabel into JPanel and then add this JPanel to JScrollPane like bellow:
public ExamplePane() {
final JPanel panel = new JPanel(new GridBagLayout());
final JLabel message = new JLabel("<html>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello</html>");
message.setPreferredSize(new Dimension(500, 50));
final JPanel messagePanel = new JPanel();
messagePanel.add(message);
JScrollPane scroller = new JScrollPane(messagePanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroller.setPreferredSize(new Dimension(100, 50));
panel.add(scroller);
add(panel);
}

Set JPanel Size to No Larger Than Element it Contains

I’m trying to get a JPanel to be no taller than the text in the single JLabel it contains. Here’s some simple example code stripped down to just the essential issue:
import java.awt.*;
import javax.swing.*;
public class MinimumSpacing {
private static void createAndShowGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 250);
JPanel pane0 = new JPanel(new GridLayout(0,1,0,0));
JPanel pane1 = new JPanel(new GridLayout(0,1,0,0));
JPanel pane2 = new JPanel(new GridLayout(0,1,0,0));
pane1.add(new JLabel("This is line #1.", JLabel.CENTER));
pane2.add(new JLabel("This is line #2.", JLabel.CENTER));
pane2.add(new JLabel("This is line #3.", JLabel.CENTER));
pane2.add(new JLabel("This is line #4.", JLabel.CENTER));
pane2.add(new JLabel("This is line #5.", JLabel.CENTER));
pane0.add(pane1);
pane0.add(pane2);
frame.add(pane0);
frame.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
);
} catch (Exception exc) {
//
}
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}
This is the result I get:
What I want is for “line #1” to be at the top of the frame with the other 4 lines immediately under it. What’s the best way to shrink the size of pane1 to make this happen?
Any help would be appreciated.
Don't use a GridLayout. A GridLayout will always resize each component to be of equal size.
Maybe you should use a vertical BoxLayout.
Read the section from the Swing tutorial on Layout Managers for more information and working examples.
As already indicated, you should consider to use a BoxLayout. Here is how to do this:
private static void createAndShowGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 250);
JPanel pane0 = new JPanel();
pane0.setLayout(new BoxLayout(pane0, BoxLayout.Y_AXIS));
pane0.add(new JLabel("This is line #1.", JLabel.CENTER));
pane0.add(new JLabel("This is line #2.", JLabel.CENTER));
pane0.add(new JLabel("This is line #3.", JLabel.CENTER));
pane0.add(new JLabel("This is line #4.", JLabel.CENTER));
pane0.add(new JLabel("This is line #5.", JLabel.CENTER));
frame.add(pane0);
frame.setVisible(true);
}

Java panel alignment

class CipherGUIFrame extends JFrame {
public CipherGUIFrame() {
super("Caesar Cipher GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 600);
JTextArea area1 = new JTextArea();
JTextArea area2 = new JTextArea();
JSpinner myspinner=new JSpinner();
JPanel mainframe = new JPanel();
mainframe.setLayout(new BoxLayout(mainframe, BoxLayout.Y_AXIS));
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));
p2.setLayout(new BoxLayout(p2, BoxLayout.Y_AXIS));
p1.setBorder(BorderFactory.createTitledBorder("Cleartext"));
p2.setBorder(BorderFactory.createTitledBorder("Spinner"));
p3.setLayout(new BoxLayout(p3, BoxLayout.Y_AXIS));
p3.setBorder(BorderFactory.createTitledBorder("Ciphertext"));
p1.add(area1);
p2.add(myspinner);
p3.add(area2);
mainframe.add(p1);
mainframe.add(p2);
mainframe.add(p3);
this.add(mainframe);
}
}
It seems that this code produces something which looks similar to this:
I am trying to tidy this up so it looks cleaner; is there a way to shrink the middle panel or to make the others bigger to make it look nicer?
Don't set the sizes of anything, but instead set the columns and rows of your JTextAreas. Don't use BoxLayout when you don't want its behaviors. Put your JTextAreas in JScrollPanes instead. And don't forget to pack() your JFrame.
import java.awt.BorderLayout;
import javax.swing.*;
public class Cipher2 extends JPanel {
public static final int ROWS = 12;
public static final int COLS = 30;
private JTextArea textArea1 = new JTextArea(ROWS, COLS);
private JTextArea textArea2 = new JTextArea(ROWS, COLS);
public Cipher2() {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); // Box OK here
JScrollPane scroll1 = new JScrollPane(textArea1);
add(wrapComponentWithTitle(scroll1, "Fubar"), BorderLayout.PAGE_START);
add(wrapComponentWithTitle(new JSpinner(), "Spinner"), BorderLayout.CENTER);
scroll1 = new JScrollPane(textArea2);
add(wrapComponentWithTitle(scroll1, "Snafu"), BorderLayout.PAGE_END);
}
private JPanel wrapComponentWithTitle(JComponent component, String title) {
// BoxLayout NOT OK here. Use BorderLayout instead
JPanel wrapPanel = new JPanel(new BorderLayout());
wrapPanel.add(component);
wrapPanel.setBorder(BorderFactory.createTitledBorder(title));
return wrapPanel;
}
private static void createAndShowGui() {
Cipher2 mainPanel = new Cipher2();
JFrame frame = new JFrame("Foo");
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(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
I figured out the answer: change Y_AXIS to X_AXIS.
<3

How to get Current JPanel from Button Click

On the event of clicking a button how would I get the JPanel it is in currently?
I know how to make a button and add an actionlistener and do event handling. I don't know how to select the current panel.
The code from the post above modified to avoid using final variables
public void buildUI() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton button = new JButton("Button");
panel.add(button);
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("The current panel is " + ((JButton)e.getComponent()).getParent());
}
});
frame.add(panel);
frame.pack();
frame.setDefaultCloseBehavior(JFrame.CLOSE_ON_EXIT);
frame.setVisible(true);
}
public void buildUI() {
JFrame frame = new JFrame();
final JPanel panel = new JPanel();
JButton button = new JButton("Button");
panel.add(button);
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("The current panel is " + panel);
}
});
frame.add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
EDIT: Adding example where listener code is not in the same class as GUI code.
//PanelPrintingListener.java
public class PanelPrintingListener implements ActionListener {
private JPanel panel;
public PanelPrintingListener(JPanel panel) {
this.panel = panel;
}
public void actionPerformed(ActionEvent e) {
System.out.println("The current panel is " + panel);
}
}
//OtherFoo.java
public void buildUI() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton button = new JButton("Button");
panel.add(button);
button.addActionListener( new PanelPrintingListener(panel) );
frame.add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
If you are looking for the panel that is currently active (meaning that it is the top one in the stack), then you can use the getComponent() method.
Component active = parentPanel.getComponent(0);
Now the "active" object will have the currently active or displayed panel.Note that "parentPanel" is the panel on which the required sub-panels are placed.

Categories