The search bar is 'search_bar', I'm trying to move it to the left hand side of the box with 'j_panel.add(search_bar, BorderLayout.WEST);' but it doesn't move from the middle. Any ideas on how to do this? Thanks
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
public class Main {
public static void main(String args[]) {
JFrame jfrm = new JFrame("Test");
jfrm.setSize(1024, 600);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setResizable(false);
JTextField search_bar = new JTextField("Search...", 25);
search_bar.setPreferredSize(new Dimension(1,35));
JTextArea body = new JTextArea(32,83);
body.setPreferredSize(new Dimension());
body.setEditable(false);
JButton search_button = new JButton("Search");
JPanel j_panel = new JPanel();
j_panel.setLayout(new FlowLayout());
j_panel.add(search_bar, BorderLayout.WEST);
j_panel.add(search_button, null);
j_panel.add(body, BorderLayout.EAST);
j_panel.setBackground(Color.gray);
search_button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
final String text = search_bar.getText();
body.setText(text);
}
});
jfrm.setContentPane(j_panel);
jfrm.setVisible(true);
}
}
j_panel.setLayout(new FlowLayout());
j_panel.add(search_bar, BorderLayout.WEST);
BorderLayout.WEST is not applicable for FlowLayout. If you want to use a BorderLayout, use one instead of FlowLayout.
You cannot position components exactly the way you'd like when using FlowLayout.
By using BorderLayout you are bound to have only 5 components, because BorderLayout can handle 5 components max.
Related
My question is about layout in Java Swing.
I want to make a screen like shown below. I saw this video on youtube and made a gif of the part I want.
I want 2 panels and a button like this:
When i clicked the button the JPanel will be hidden and JTable's width will be 100% like html/css like this; (And when button clicked again JPanel will be shown etc..)
How can I do this? Which layout should I use?
There is more than one way to do it, but here's an example that uses BorderLayout as the main layout, and places the button in a left aligning FlowLayout:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
public class LayoutDemo {
private LayoutDemo() {
JFrame frame = new JFrame("Demo");
frame.setLocationByPlatform(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel buttonHolder = new JPanel(new FlowLayout(FlowLayout.LEADING));
frame.add(buttonHolder, BorderLayout.NORTH);
JButton button = new JButton("Toggle visibility");
buttonHolder.add(button);
final JPanel left = new JPanel();
left.setPreferredSize(new Dimension(100, 200));
left.setBackground(Color.BLUE);
frame.add(left, BorderLayout.LINE_START);
JLabel table = new JLabel("This pretends to be a table", SwingConstants.CENTER);
table.setPreferredSize(new Dimension(400, 200));
frame.add(table);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
left.setVisible(!left.isVisible());
}
});
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new LayoutDemo();
}
});
}
}
I used setPreferredSize() to give the components some reasonable default size, but usually it should be automatically calculated by the layout manager from the sizes of the child components, or in case of a custom component, you should override getPreferredSize() return what is appropriate for the component.
The result looks like:
I have a jsplitPane and 2 component. I want to not let the user click the divider to maximize the top component.
Here is my code.
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
public class MovingJSplitPaneDivider {
public static void main(String[] a) {
JFrame horizontalFrame = new JFrame();
horizontalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent topButton = new JButton("Left");
JComponent bottomButton = new JButton("Right");
final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setTopComponent(topButton);
splitPane.setBottomComponent(bottomButton);
horizontalFrame.add(splitPane, BorderLayout.CENTER);
horizontalFrame.setSize(150, 150);
horizontalFrame.setVisible(true);
splitPane.setDividerLocation(0.5);
}
}
What I want exactly is that to block the user when he wants to move down the divider.
I am trying to add a JPanel to a JFrame with FlowLayout, but continue to get this exception: "Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal component position". I would like to be able to implement a JPanel with a few buttons in the near future, so please let me know what I can do to allow that.
package textadv;
import java.awt.FlowLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TextAdv {
private JFrame frame = new JFrame("Text Adventure");
private JPanel mainPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
private JButton b1 = new JButton("Left");
public TextAdv() {
mainPanel.setBackground(Color.RED);
int FRAME_WIDTH = 400;
int FRAME_HEIGHT = 400;
b1.setEnabled(true);
b1.setVisible(true);
b1.setPreferredSize(new Dimension(40, 40));
mainPanel.setVisible(true);
mainPanel.setPreferredSize(new Dimension (50, 50));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setVisible(true);
mainPanel.add(b1, FlowLayout.CENTER);
frame.add(mainPanel);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
TextAdv fS = new TextAdv();
}
});
}
You cant specify FlowLayout options to the add method of Container. This resolves to the add() overload which accepts Component, and an int, index. Other layouts let you use the add(Component, Object) overload, like the GridBagLayout and its GridBagConstraints. For the FlowLayout you specify the options to the constructor of the layout only.
Replace this
mainPanel.add(b1, FlowLayout.CENTER);
with this
mainPanel.add(b1);
Consider revisiting the tutorial here: http://docs.oracle.com/javase/tutorial/uiswing/layout/flow.html
See Container here : http://docs.oracle.com/javase/7/docs/api/java/awt/Container.html#add%28java.awt.Component,%20int%29
I'm working on a program in which I'd used a JTextArea, JButton, JLabel and JPanel.
The logic I'd to implement is: user types a text in the given textArea and then click on the button. On button click I'd to retrieve the text from the textArea and create a label with the written text(as in textArea) and show it on the panel.
Everything I'd done previously is correct but the problem is with the label and panel. The label is not visible on the panel.
The code snippets is:
import java.awt.BorderLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
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.ScrollPaneConstants;
import javax.swing.border.BevelBorder;
/**
*
* #author mohammadfaisal
* http://ermohammadfaisal.blogspot.com
* http://facebook.com/m.faisal6621
*
*/
public class CodeMagnets extends JFrame{
private JTextArea area4Label;
private JLabel codeLabel;
private JButton createButton;
private JPanel magnet;
public CodeMagnets(String title) throws HeadlessException {
super(title);
magnet=new JPanel(null);
JScrollPane magnetScroller=new JScrollPane(magnet);
magnetScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
magnetScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(BorderLayout.CENTER, magnetScroller);
JPanel inputPanel=new JPanel();
area4Label=new JTextArea(5, 30);
area4Label.setTabSize(4);
JScrollPane textScroller=new JScrollPane(area4Label);
inputPanel.add(textScroller);
createButton=new JButton("Create code magnet");
createButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//codeLabel=new JLabel(area4Label.getText());
codeLabel=new MyLabel(area4Label.getText());//this is for my new question
codeLabel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
codeLabel.setLocation(50, 20);
codeLabel.setVisible(true);
magnet.add(codeLabel);
area4Label.setText("");
//pack();
}
});
inputPanel.add(createButton);
add(BorderLayout.SOUTH, inputPanel);
//pack();
setSize(640, 480);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new CodeMagnets("Code Magnets");
}
}
You need to repaint()/validate() your panle after adding new components in it dynamically. So after this:
magnet.add(codeLabel);
add this:
magnet.validate();
or
magnet.repaint();
Also one thing you are using null layout for magnet panel. So must have to setBounds() of jLable before adding it to magnet panel. So it becomes
public void actionPerformed(ActionEvent e) {
codeLabel=new JLabel(area4Label.getText());
codeLabel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
codeLabel.setBounds(50, 20, 100, 100);
magnet.add(codeLabel);
magnet.repaint();
area4Label.setText("");
}
It is not recommended to use null as layout, you should use proper layout like BorderLayout or GridLayout or even simpler FlowLayout based on your requirement.
As said by #Andrew use something like:
codeLabel.setSize(codeLabel.getPreferredSize());
codeLabel.setLocation(50, 20);
instead of
codeLabel.setBounds(50, 20, 100, 100);
This will solve the size issue of jLabel.
public void actionPerformed(ActionEvent e) {
codeLabel=new JLabel(area4Label.getText());
codeLabel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
codeLabel.setBounds(50, 20, 100, 100);
codeLabel.setOpaque(True);
magnet.add(codeLabel);
magnet.repaint();
area4Label.setText("");
}
If I have code like so:
class X extends JFrame
{
X()
{
setLayout(new GridLayout(3,3));
JButton b = new JButton("A-ha");
/*I would like to add this button in the center of this grid (2,2)*/
//How can I do it?
}
};
As what I know, you have to fill all the previous cells before. So you need to add 4 components before you can add the center component. Hmm, it could be a better layoutmanager.
What are you trying to do? Maybe BorderLayout is what you are looking for?
You might want to take a look at the GridBag Layout
This centres vertically and horizontally
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class centerbutton extends JFrame{
public centerbutton(){
setLayout(new BorderLayout());
JPanel panel = new JPanel();
JButton button = new JButton("A-ha!");
button.setAlignmentX(
Component.CENTER_ALIGNMENT);
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(Box.createVerticalGlue());
panel.add(button);
panel.add(Box.createVerticalGlue());
getContentPane().add(panel);
setVisible(true);
}
public static void main(String[] args) {
new centerbutton();
}
}