i am trying to set the text box and label in panel but the text box is getting inappropriate size and the layout is also not setting properly
public class FrameLayout1 implements ActionListener {
JTextField[] txtName;
JLabel[] lblname;
JCheckBox keyButton;
JCheckBox cascadeButton;
JComboBox SetList;
JComboBox ClassList;
JLabel[] lblType;
JTextField txtJoinColumn;
JLabel[] lblAssoType;
JTextField[] txtJoinColumnAssoc;
FrameLayout1(){
}
public void setDetailsPanel()
{
txtName = new JTextField[10];
txtJoinColumnAssoc = new JTextField[10];
lblname=new JLabel[10];
lblType=new JLabel[10];
lblAssoType=new JLabel[20];
JFrame ColumnFrame=new JFrame("Enter the Column Values");
int i=0;
JPanel panel = new JPanel(new GridLayout(0,1,5,10));
for (i=0;i<5;i++)
{
JPanel panelTxtLbl = new JPanel(new GridLayout(0,2));
lblname[i] = new JLabel("label"+i+":", JLabel.LEFT);
panelTxtLbl.add(lblname[i]);
txtName[i] = new JTextField(15);
panelTxtLbl.add(txtName[i]);
panel.add(panelTxtLbl);
lblType[i] = new JLabel("labeldata", JLabel.LEFT);
panel.add(lblType[i]);
String[] SetStrings = { "One to Many","Many to Many" };
SetList = new JComboBox(SetStrings);
SetList.setSelectedIndex(1);
panel.add(SetList);
cascadeButton = new JCheckBox("cascade");
cascadeButton.setSelected(true);
panel.add(cascadeButton);
JPanel panelSetClass = new JPanel(new GridLayout(0,2));
for(int j=0;j<3;j++)
{
lblAssoType[j]=new JLabel("Label Inner"+j+":", JLabel.LEFT);
panelSetClass.add(lblAssoType[j]);
txtJoinColumnAssoc[j] = new JTextField(15);
panelSetClass.add(txtJoinColumnAssoc[j]);
}
panel.add(panelSetClass);
panel.add(createHorizontalSeparator());
}
//detailsPanel.add(panel, BorderLayout.CENTER);
//detailsPanel.setAutoscrolls(true);
panel.setAutoscrolls(true);
JButton button = new JButton("Submit");
button.addActionListener(this);
//detailsPanel.add(button,BorderLayout.PAGE_END);
panel.add(button,BorderLayout.PAGE_END);
Color c=new Color(205, 222, 216);
ColumnFrame.setLayout(new BorderLayout());
ColumnFrame.add(panel,BorderLayout.CENTER);
//ColumnFrame.setSize(700,600);
ColumnFrame.setBackground(c);
ColumnFrame.pack();
ColumnFrame.setVisible(true);
ColumnFrame.setLocation(200, 200);
}
/*
* to add a horizontal line in the panel
*/
static JComponent createHorizontalSeparator() {
JSeparator x = new JSeparator(SwingConstants.HORIZONTAL);
x.setPreferredSize(new Dimension(3,2));
return x;
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
createHorizontalSeparator function helps you add a horizontal line in the panel, as i was unable to segregate the fields .
current output
desired output
Today, I answered a similar question where GridLayout caused much the same
confusion. Do yourself a service and use a flexible layout manager
(if it is possible) like MigLayout to create your layouts. These
simple built-in layout managers either have very limited application
(GridLayout, FlowLayout, BorderLayout) or it can become challenging
to create sofisticated layouts with them (BoxLayout).
GridLayout and BorderLayout stretch their components and do not
honour the size bounds of their children. This is the reason why you
have those unnecessary spaces and why the button is expanded horizontally.
The following is an example that mimics your layout. It is created with
the powerful MigLayout manager.
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class DetailsPanel extends JFrame {
public DetailsPanel() {
initUI();
setTitle("Details");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
setLayout(new MigLayout("ins 10, wrap 2", "[][grow]"));
add(new JLabel("Label 1:"));
add(new JTextField(), "w 200, growx");
add(new JLabel("Label 2:"));
add(new JTextField(), "growx");
add(new JLabel("Label 3:"));
add(new JTextField(), "gaptop 30, growx");
add(new JLabel("Label 4:"));
add(new JTextField(), "growx");
add(new JLabel("Label 5:"));
add(new JTextField(), "gaptop 30, growx");
add(new JLabel("Label 6:"));
add(new JTextField(), "growx");
add(new JButton("Submit"), "gaptop 30, skip, right");
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
DetailsPanel ex = new DetailsPanel();
ex.setVisible(true);
}
});
}
}
You need to dedicate some time to learn a more complex layout manager, but
it pays off. Especially, if you often build layouts in Swing.
Related
How to set Jbuttons to a specific place when you have a background in JLabel : code below
i can't get the jlabel to stay at the top and the buttons to stay south(bottom) ??
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonsClass extends JFrame
implements ActionListener {
JButton b1 = new JButton("button1");
JButton b2 = new JButton("button2");
JButton b3 = new JButton("button3");
JButton b4 = new JButton("button4");
JLabel label = new JLabel("buttons:");
public static void main(String[] args) {
new ButtonsClass();
}
public Jukebox() {
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("image.png")));
setLayout(new FlowLayout());
setSize(500,150);
setTitle("Backgroundwithbuttons");
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
JPanel top = new JPanel();
top.add(label);
add("North", top);
JPanel bottom = new JPanel();
bottom.add(b1);
bottom.add(b2);
bottom.add(b3);
bottom.add(b4);
add("South", bottom);
setVisible(true);
}
}
" i can't get the jlabel to stay at the top and the buttons to stay south(bottom)"
That's because you set the layout the BorderLayout, then immediately set it to FlowLayout. With FlowLayout, your BorderLayout positioning will do nothing.
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("image.png")));
setLayout(new FlowLayout());
Just get rid of the setLayout(new FlowLayout());
Also your constructor is wrong
public Jukebox() {
-Should be-
public ButtonClass() {
Also you need to set the layout of the JLabel that you set as the content pane. Yout constructor should look like this
public ButtonClass() {
JLabel background = new JLabel(new ImageIcon("image.png"));
background.setLayout(new BorderLayout());
setContentPane(background);
setTitle("Background with buttons");
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
JPanel top = new JPanel();
top.add(label);
add(top, BorderLayout.NORTH);
JPanel bottom = new JPanel();
bottom.add(b1);
bottom.add(b2);
bottom.add(b3);
bottom.add(b4);
add(bottom, BorderLayout.SOUTH);
//pack();
setVisible(true);
}
Also, add("North", top); is a deprecated method. Instead use add(top, BorderLayout.NORTH) and same for add(bottom, BorderLayout.SOUTH)
Also, Swing apps should be run on the Event Dispatch Thread. You can do so by wrapping the code in your main with a SwingUtilities.invokeLater...
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ButtonClass();
}
});
}
Also, you should set the panel's opaque property to false, if you want the image to show behind them.
top.setOpaque(false);
bottom.setOpaque(false);
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonClass extends JFrame
implements ActionListener {
JButton b1 = new JButton("button1");
JButton b2 = new JButton("button2");
JButton b3 = new JButton("button3");
JButton b4 = new JButton("button4");
JLabel label = new JLabel("buttons:");
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ButtonClass();
}
});
}
public ButtonClass() {
label.setForeground(Color.WHITE);
JLabel background = new JLabel(new ImageIcon(getClass().getResource("/resources/space.png")));
background.setLayout(new BorderLayout());
setContentPane(background);
setTitle("Background with buttons");
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
JPanel top = new JPanel();
top.setOpaque(false);
top.add(label);
add(top, BorderLayout.NORTH);
JPanel bottom = new JPanel();
bottom.setOpaque(false);
bottom.add(b1);
bottom.add(b2);
bottom.add(b3);
bottom.add(b4);
add(bottom, BorderLayout.SOUTH);
setSize(400, 300);
setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {}
}
Try using:
add(bottom, BorderLayout.SOUTH);
instead of:
add("South", bottom);
BorderLayout tutorial
hello goodevening to all i have a problem on my program with the ScrollPane in my JList i cant put an JScrollPane in my list because i am using a panel instead of Container this is my code so far its all runnable the problem is if you enter a high number in the number of times the some output will not be able to see because of the size of my list . so this is the code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MultCen extends JFrame implements ActionListener
{
public static void main(String args [])
{
MultCen e = new MultCen();
e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
e.setVisible(true);
e.setSize(300,450);
}
JTextField t1 = new JTextField();
JTextField t2 = new JTextField();
JButton b = new JButton("Okay");
JButton c = new JButton("Clear");
JList list = new JList();
JLabel lab = new JLabel();
DefaultListModel m = new DefaultListModel();
public MultCen()
{
JPanel panel = new JPanel();
panel.setLayout(null);
JLabel l = new JLabel("Enter a number :");
JLabel l1 = new JLabel("How many times :");
l.setBounds(10,10,130,30);
l1.setBounds(10,40,130,30);
t1.setBounds(140,10,130,25);
t2.setBounds(140,40,130,25);
b.setBounds(60,90,75,30);
c.setBounds(150,90,75,30);
list.setBounds(30,140,220,220);
panel.add(t1);
panel.add(t2);
panel.add(l);
panel.add(l1);
panel.add(list);
panel.add(b);
panel.add(c);
getContentPane().add(panel);
b.addActionListener(this);
c.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b)
{
int t3 = Integer.parseInt(t1.getText());
int t4 = Integer.parseInt(t2.getText());
m.addElement("The multiplication Table of "+t3);
for (int cc =1 ; cc <=t4; cc++ )
{
lab.setText(t3+"*"+cc+" = "+(t3*cc));
m.addElement(lab.getText());
list.setModel(m);
}
}
if(e.getSource() == c)
{
t1.setText("");
t2.setText("");
m.removeAllElements();
}
}
}
JScrollPane does not work with null Layout. Use BoxLayout or any other resizeable layout instead. This is the limitation of setLayout(null).
Use Layout managers. You've asked a lot of questions here and I'm sure a few you have been advised not to use null layout. Again here is the tutorial Laying out components Within a container. Learn to use them so you don't run into the million possible problems on the road ahead. This kind of problem being one of them.
here's an example of how you could achieve the same thing with layout managers, and some empty borders for white space.
With Layout Manager
Without Layout Manger
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class MultCen extends JFrame {
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MultCen e = new MultCen();
e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
e.setVisible(true);
e.pack();
}
});
}
JTextField t1 = new JTextField(10);
JTextField t2 = new JTextField(10);
JButton b = new JButton("Okay");
JButton c = new JButton("Clear");
JLabel lab = new JLabel();
DefaultListModel m = new DefaultListModel();
public MultCen() {
JPanel topPanel = new JPanel(new GridLayout(2, 2, 0, 5));
JLabel l = new JLabel("Enter a number :");
JLabel l1 = new JLabel("How many times :");
topPanel.add(l);
topPanel.add(t1);
topPanel.add(l1);
topPanel.add(t2);
JPanel buttonPanel = new JPanel();
buttonPanel.add(b);
buttonPanel.add(c);
buttonPanel.setBorder(new EmptyBorder(10, 0, 10, 0));
JList list = new JList();
JScrollPane scroll = new JScrollPane(list);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setPreferredSize(new Dimension(300, 300));
JPanel panel = new JPanel(new BorderLayout());
panel.add(topPanel, BorderLayout.NORTH);
panel.add(buttonPanel, BorderLayout.CENTER);
panel.add(scroll, BorderLayout.SOUTH);
panel.setBorder(new EmptyBorder(10, 15, 10, 15));
getContentPane().add(panel);
}
}
Side Notes
Run Swing apps from the Event Dispatch Thread. See Initial Threads
When you do decide to use layout managers, just pack() your frame instead of setSize()
Use better variable names.
See Extends JFrame vs. creating it inside the the program
How can I do this with GridLayout or another Gridxxx?
enter link description here
I want add to panel JLabel and JTextField with different size.
Is it possible?
I have this code.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
class TEST_rozlozenia {
TEST_rozlozenia() {
JPanel panel = new JPanel();
JPanel lFields = new JPanel(new BorderLayout(2,2));
JPanel labels = new JPanel(new GridLayout(0,1));
labels.setBorder(new TitledBorder("Labels"));
JPanel fields = new JPanel(new GridLayout(0,1));
fields.setBorder(new TitledBorder("TextFields"));
labels.add(new JLabel("Label"));
labels.add(new JLabel("Label1"));
fields.add(new JTextField(55));
fields.add(new JTextField(10));
lFields.add(labels, BorderLayout.WEST);
lFields.add(fields, BorderLayout.EAST);
panel.add(lFields, BorderLayout.NORTH);
JOptionPane.showMessageDialog(null, panel);
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
new TEST_rozlozenia();
}
});
}
}
Whats wrong?
I don't have a different textfields size.
All examples has JTextField the same size
Then create you text fields with different sizes:
JTextField textField1 = new JTextField(10);
JTextField textField2 = new JTextField(20);
The parameter in the constructor will make a suggestion as to how big the text field should be. So any layout manager that respects the preferred size of the component will use this information.
I made a small program with two buttons. I label
the buttons one exiting the program and the second importing files.
I actually let them both to exit the program when ever someone pressed on it
the problem is the buttons taking all the window, why?
I tried GridBagConstraints to resize the buttons some how but no luck anyway here's the full class without imports..
public class Window2 extends JFrame{
private static final long serialVersionUID = 1L;
public Window2(){
super ("ALANAZ imagtor");
setSize(600,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel pnl1 = new JPanel(new GridLayout());
JPanel pnl2 = new JPanel();
//button
JButton butn1 = new JButton("EXIT");
JButton butn2 =new JButton("IMPORT");
butn1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "exiting ... bye...");
System.exit(0);
}
});
butn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
JOptionPane.showMessageDialog(null, "can't import now exiting");
System.exit(0);
}
});
GridBagConstraints gb1 = new GridBagConstraints();
gb1.insets = new Insets(15,15,15,15);
//Jlabel
JLabel lbl1 = new JLabel("exit or import an image");
pnl1.add(butn1);
pnl1.add(butn2);
pnl2.add(lbl1);
add(pnl2, BorderLayout.SOUTH);
add(pnl1, BorderLayout.CENTER);
}}
You are misusing your layout managers. Your pnl1 JPanel uses GridLayout (without any row or column constants?!), and if you only add one component to it, it will take up the entire JPanel. You seem to have GridBagConstraints in your code, but no GridBagLayout, which is confusing to me.
The solution is to read up on and understand how to use layout managers. Please have a look at the tutorial link: Laying Out Components Within a Container.
Key is to keep remembering that you can nest JPanels within JPanels. For example:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
public class Window2 extends JFrame {
private static final long serialVersionUID = 1L;
private static final int PREF_W = 600;
private static final int PREF_H = 400;
public Window2() {
super("ALANAZ imagtor");
setDefaultCloseOperation(EXIT_ON_CLOSE);
int gap = 3;
JPanel buttonPanel = new JPanel(new GridLayout(1, 0, gap, 0));
buttonPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
JPanel pnl2 = new JPanel();
JButton butn1 = new JButton("EXIT");
JButton butn2 = new JButton("IMPORT");
butn1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "exiting ... bye...");
System.exit(0);
}
});
butn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(null, "can't import now exiting");
System.exit(0);
}
});
JLabel lbl1 = new JLabel("exit or import an image");
buttonPanel.add(butn1);
buttonPanel.add(butn2);
JPanel centerPanel = new JPanel(new BorderLayout());
centerPanel.add(buttonPanel, BorderLayout.SOUTH);
pnl2.add(lbl1);
add(pnl2, BorderLayout.SOUTH);
add(centerPanel, BorderLayout.CENTER);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
public static void main(String[] args) {
Window2 win2 = new Window2();
win2.pack();
win2.setLocationRelativeTo(null);
win2.setVisible(true);
}
}
If you initialize your panel with a BorderLayout instead, and add your buttons using EAST, NORTH, WEST, SOUTH, you will have a quick fix - however I do also recommend reading up on layout managers
JPanel pnl1 = new JPanel(new BorderLayout());
pnl1.add(new JButton(), BorderLayout.SOUTH);
This is a MigLayout-newbie question. I'd like to know how to draw a separator from the end of a label across the width of a panel.
Here's my example code:
package com.ndh.swingjunk;
import java.awt.*;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;
class MyJFrame extends JFrame {
public MyJFrame() {
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("foo");
label.setFont(new Font("Tahoma", Font.BOLD, 11));
JSeparator separator = new JSeparator();
JLabel anotherLabel = new JLabel("some other label");
anotherLabel.setFont(new Font("Tahoma", Font.PLAIN, 11));
JButton button1 = new JButton("button 1");
JButton button2 = new JButton("button 2");
JPanel panel = new JPanel(new MigLayout());
panel.add(label);
panel.add(separator, "growx, wrap");
panel.add(anotherLabel);
panel.add(button1);
panel.add(button2);
getContentPane().add(panel);
pack();
}
}
public class SeparatorLayoutQuestion {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override public void run() {new MyJFrame().setVisible(true);}});}}
and here's the result:
which is terribly lame. I'd like the separator to stretch from the end of "Foo" to the end of the panel, all the way across button 2.
Here's what worked for me, I had to use "split" so the label and separator could share the same cell:
JPanel panel = new JPanel(new MigLayout());
panel.add(label, "split 2, span");
panel.add(separator, "growx, wrap");
panel.add(anotherLabel);
panel.add(button1);
panel.add(button2);
try adding
"span 3"
to the first label