Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #35087
| From | Knute Johnson <nospam@rabbitbrush.frazmtn.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: What do you think of my CropImage class |
| Date | 2016-07-22 07:47 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <nmtbmv$47g$1@dont-email.me> (permalink) |
| References | (8 earlier) <nmrpp8$lfv$1@dont-email.me> <87mvla4dlt.fsf@Equus.decebal.nl> <nms1aq$6jj$1@dont-email.me> <38012169-26ac-47b4-91c9-cfbb84d8c042@googlegroups.com> <dvenqbFouupU1@mid.individual.net> |
On 7/22/2016 07:10, Nigel Wade wrote:
> Gridbag and Border are the main layouts I use. There's pretty much
> nothing I've ever constructed in a GUI which couldn't be managed by a
> suitable combination of those two layouts. The key to any GUI is not to
> try to force every component into the same container, and then try to
> micro-manage the layout. Instead use multiple containers, containers
> within containers, each using their own layout, until you get the
> desired effect.
I do almost everything with GridBagLayout and a couple of things with
BorderLayout and FlowLayout. Years ago I wrote a program to demonstrate
how GridBagLayout works. Code is below. I haven't used it in a long
time but it might be really handy for Cecil.
Cecil there are some sample layouts but you can create your own
components and constraints to try a piece of a layout to see how the
components interact. In the lower right corner is a box that you can
drag to make the layout area smaller and see what happens when your
components don't have enough room.
//package com.knutejohnson.demos;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class GridBagTester extends JPanel implements ActionListener {
private static final String[] colorNames = { "Black","Blue","Cyan",
"Green","Gray","Magenta","Orange","Pink","Red","Yellow" };
private static final Color[] colors = {
Color.BLACK,Color.BLUE,Color.CYAN,
Color.GREEN,Color.GRAY,Color.MAGENTA,Color.ORANGE,Color.PINK,
Color.RED,Color.YELLOW };
private static final String instructionStr =
"<html><ul>" +
"<li>Use the 'Edit' menu to select a component by color." +
"<li>Use the sub-menus to 'Add', 'Remove', set 'Constraints'
on,<br>" +
"or set the 'Sizes' of a component." +
"<li>Right click on a component to popup its menu." +
"<li>The 'View' menu (not available with JApplet) has options
to<br>" +
"pack the frame or set its size." +
"<li>Click and drag the small box in the lower right corner to<br>" +
"resize the panel." +
"</ul></html>";
private static final String aboutStr =
"GridBagTester\nWritten by: Knute Johnson\nUpdated 4 Aug 2008";
private final JPanel[] panels = new JPanel[colors.length];
private final GridBagLayout gbl;
private final GridBagConstraints defaultConstraints;
public final JMenu file,edit,view,samples,help;
private final JMenu[] panelMenus;
private final JMenuItem quit,pack,six40x480,clear,instructions,about;
private final JMenuItem layout1, layout2, layout3;
private boolean mouseFlag;
public GridBagTester() {
setBorder(BorderFactory.createLineBorder(Color.BLACK));
// create menus for use by top level container
file = new JMenu("File");
file.setMnemonic(KeyEvent.VK_F);
edit = new JMenu("Edit");
edit.setMnemonic(KeyEvent.VK_E);
view = new JMenu("View");
view.setMnemonic(KeyEvent.VK_V);
samples = new JMenu("Samples");
samples.setMnemonic(KeyEvent.VK_S);
help = new JMenu("Help");
help.setMnemonic(KeyEvent.VK_H);
quit = new JMenuItem("Quit");
quit.addActionListener(this);
file.add(quit);
pack = new JMenuItem("Pack");
pack.addActionListener(this);
view.add(pack);
six40x480 = new JMenuItem("640x480");
six40x480.addActionListener(this);
view.add(six40x480);
layout1 = new JMenuItem("Layout 1");
layout1.addActionListener(this);
samples.add(layout1);
layout2 = new JMenuItem("Layout 2");
layout2.addActionListener(this);
samples.add(layout2);
layout3 = new JMenuItem("Layout 3");
layout3.addActionListener(this);
samples.add(layout3);
instructions = new JMenuItem("Instructions");
instructions.addActionListener(this);
help.add(instructions);
about = new JMenuItem("About");
about.addActionListener(this);
help.add(about);
panelMenus = new JMenu[colors.length];
for (int i=0; i<colors.length; i++) {
panelMenus[i] = new JMenu(colorNames[i]);
edit.add(panelMenus[i]);
IndexedJMenuItem mi = new IndexedJMenuItem("Add",i);
mi.addActionListener(this);
panelMenus[i].add(mi);
mi = new IndexedJMenuItem("Remove",i);
mi.addActionListener(this);
panelMenus[i].add(mi);
mi = new IndexedJMenuItem("Constraints",i);
mi.addActionListener(this);
panelMenus[i].add(mi);
mi = new IndexedJMenuItem("Sizes",i);
mi.addActionListener(this);
panelMenus[i].add(mi);
}
edit.add(new JSeparator());
clear = new JMenuItem("Clear");
clear.addActionListener(this);
edit.add(clear);
gbl = new GridBagLayout();
setLayout(gbl);
defaultConstraints = new GridBagConstraints();
for (int i=0; i<colors.length; i++) {
panels[i] = new JPanel(new BorderLayout());
JLabel l = new JLabel(colorNames[i],JLabel.CENTER);
l.setForeground(new Color(colors[i].getRGB() ^ 0xafafaf));
panels[i].add(l,BorderLayout.CENTER);
panels[i].setBackground(colors[i]);
panels[i].setPreferredSize(new Dimension(80,60));
JPopupMenu popup = new JPopupMenu();
IndexedJMenuItem mi = new IndexedJMenuItem("Remove",i);
mi.addActionListener(this);
popup.add(mi);
mi = new IndexedJMenuItem("Constraints",i);
mi.addActionListener(this);
popup.add(mi);
mi = new IndexedJMenuItem("Sizes",i);
mi.addActionListener(this);
popup.add(mi);
panels[i].addMouseListener(new PopupListener(popup));
}
MouseAdapter ma = new MouseAdapter() {
public void mousePressed(MouseEvent me) {
Rectangle r = new
Rectangle(getWidth()-8,getHeight()-8,8,8);
if (r.contains(me.getPoint())) {
mouseFlag = true;
// this causes cursor to move to center of drag box
when
// mouse is clicked if the box can move
MouseEvent event = new MouseEvent(
(Component)me.getSource(),
MouseEvent.MOUSE_DRAGGED,
me.getWhen(),
me.getModifiersEx(),
me.getX(),
me.getY(),
me.getClickCount(),
me.isPopupTrigger());
processMouseMotionEvent(event);
}
}
public void mouseReleased(MouseEvent me) {
mouseFlag = false;
}
};
MouseMotionAdapter mma = new MouseMotionAdapter() {
public void mouseMoved(MouseEvent me) {
Rectangle r = new
Rectangle(getWidth()-8,getHeight()-8,8,8);
if (r.contains(me.getPoint()))
setCursor(Cursor.getPredefinedCursor(
Cursor.CROSSHAIR_CURSOR));
else
setCursor(Cursor.getDefaultCursor());
}
public void mouseDragged(MouseEvent me) {
if (mouseFlag) {
Container cont = getTopLevelAncestor();
if (cont instanceof JApplet)
cont = ((JApplet)cont).getContentPane();
else if (cont instanceof JFrame)
cont = ((JFrame)cont).getContentPane();
else
return;
int x = me.getX();
if (x < 4)
x = 4;
int y = me.getY();
if (y < 4)
y = 4;
if (x > cont.getWidth() - 5)
x = cont.getWidth() - 5;
if (y > cont.getHeight() - 5)
y = cont.getHeight() - 5;
setSize(x + 5,y + 5);
validate();
}
}
};
addMouseListener(ma);
addMouseMotionListener(mma);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(getWidth()-9,getHeight()-9,8,8);
}
public void actionPerformed(ActionEvent ae) {
String ac = ae.getActionCommand();
if (ac.equals("Quit"))
System.exit(0);
else if (ac.equals("Pack")) {
Container cont = getTopLevelAncestor();
if (cont instanceof JFrame)
((JFrame)cont).pack();
} else if (ac.equals("640x480")) {
Container cont = getTopLevelAncestor();
if (cont instanceof JFrame)
((JFrame)cont).setSize(640,480);
} else if (ac.equals("Clear")) {
removeAll();
validate();
repaint();
} else if (ac.equals("Instructions")) {
JOptionPane.showMessageDialog(this,instructionStr);
} else if (ac.equals("About")) {
JOptionPane.showMessageDialog(this,aboutStr);
} else if (ac.equals("Layout 1")) {
removeAll();
// reset preferred size
for (int i=0; i<4; i++)
panels[i].setPreferredSize(new Dimension(80,60));
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
add(panels[0],c);
c.gridx = 1;
add(panels[1],c);
c.gridx = 2;
add(panels[2],c);
c.gridx = 0; c.gridwidth = 3; c.ipady = 40;
c.fill = GridBagConstraints.HORIZONTAL;
add(panels[3],c);
c.gridx = 1; c.gridwidth = 2; c.ipady = -20;
c.insets.top = 10;
add(panels[4],c);
validate();
repaint();
} else if (ac.equals("Layout 2")) {
removeAll();
// reset preferred size
for (int i=0; i<4; i++)
panels[i].setPreferredSize(new Dimension(80,60));
GridBagConstraints c = new GridBagConstraints();
c.gridx = c.gridy = 0; c.weightx = c.weighty = 1.0;
c.anchor = GridBagConstraints.NORTHWEST;
add(panels[0],c);
c.gridx = 2; c.anchor = GridBagConstraints.NORTHEAST;
add(panels[1],c);
c.gridx = c.gridy = 1; c.anchor = GridBagConstraints.CENTER;
add(panels[2],c);
c.gridx = 0; c.gridy = 2; c.anchor =
GridBagConstraints.SOUTHWEST;
add(panels[3],c);
c.gridx = 2; c.anchor = GridBagConstraints.SOUTHEAST;
add(panels[4],c);
validate();
repaint();
} else if (ac.equals("Layout 3")) {
removeAll();
// reset preferred size
for (int i=0; i<4; i++)
panels[i].setPreferredSize(new Dimension(80,60));
GridBagConstraints c = new GridBagConstraints();
c.gridx =0; c.gridy = GridBagConstraints.RELATIVE;
c.weightx = c.weighty = 1.0;
c.anchor = GridBagConstraints.NORTH;
add(panels[0],c);
c.anchor = GridBagConstraints.SOUTH;
add(panels[1],c);
c.anchor = GridBagConstraints.NORTH;
add(panels[2],c);
c.anchor = GridBagConstraints.SOUTH;
add(panels[3],c);
validate();
repaint();
}
if (ae.getSource() instanceof IndexedJMenuItem) {
int menuIndex = ((IndexedJMenuItem)ae.getSource()).getIndex();
if (ac.equals("Add")) {
if (!panels[menuIndex].isDisplayable()) {
add(panels[menuIndex],defaultConstraints);
validate();
repaint();
}
} else if (ac.equals("Remove")) {
if (panels[menuIndex].isDisplayable()) {
remove(panels[menuIndex]);
validate();
repaint();
}
} else if (ac.equals("Constraints")) {
GridBagConstraints c = ConstraintsDialog.show(
panels[menuIndex],colorNames[menuIndex],
gbl.getConstraints(panels[menuIndex]));
if (c != null) {
gbl.setConstraints(panels[menuIndex],c);
panels[menuIndex].revalidate();
}
} else if (ac.equals("Sizes")) {
Dimension[] dim = SizesDialog.show(panels[menuIndex],
colorNames[menuIndex],panels[menuIndex].getMinimumSize(),
panels[menuIndex].getPreferredSize(),
panels[menuIndex].getMaximumSize());
if (dim != null) {
panels[menuIndex].setMinimumSize(dim[0]);
panels[menuIndex].setPreferredSize(dim[1]);
panels[menuIndex].setMaximumSize(dim[2]);
panels[menuIndex].revalidate();
}
}
}
}
static class IndexedJMenuItem extends JMenuItem {
private final int index;
public IndexedJMenuItem(String text, int index) {
super(text);
this.index = index;
}
public int getIndex() {
return index;
}
}
static class PopupListener extends MouseAdapter {
private final JPopupMenu popup;
public PopupListener(JPopupMenu popup) {
this.popup = popup;
}
public void mousePressed(MouseEvent me) {
if (me.isPopupTrigger())
popup.show(me.getComponent(),me.getX(),me.getY());
}
public void mouseReleased(MouseEvent me) {
if (me.isPopupTrigger())
popup.show(me.getComponent(),me.getX(),me.getY());
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame("GridBagTester");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagTester gbt = new GridBagTester();
f.getContentPane().add(gbt);
JMenuBar mb = new JMenuBar();
f.setJMenuBar(mb);
mb.add(gbt.file);
mb.add(gbt.edit);
mb.add(gbt.view);
mb.add(gbt.samples);
mb.add(gbt.help);
f.setSize(640,480);
f.setVisible(true);
}
});
}
}
//package com.knutejohnson.demos;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SizesDialog extends JDialog implements ActionListener {
private JTextField minimumWidth, minimumHeight;
private JTextField preferredWidth, preferredHeight;
private JTextField maximumWidth, maximumHeight;
private JButton updateButton, cancelButton;
private boolean flag;
public SizesDialog(String title) {
super((Dialog)null,title,true);
Container cp = getContentPane();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
setVisible(false);
}
});
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(2,2,2,2);
c.gridx = 1; c.gridy = 0;
cp.add(new JLabel("Width"),c);
++c.gridx;
cp.add(new JLabel("Height"),c);
c.gridx = 0; ++c.gridy;
cp.add(new JLabel("minimumSize"),c);
++c.gridx;
minimumWidth = new JTextField(5);
cp.add(minimumWidth,c);
++c.gridx;
minimumHeight = new JTextField(5);
cp.add(minimumHeight,c);
c.gridx = 0; ++c.gridy;
cp.add(new JLabel("preferredSize"),c);
++c.gridx;
preferredWidth = new JTextField(5);
cp.add(preferredWidth,c);
++c.gridx;
preferredHeight = new JTextField(5);
cp.add(preferredHeight,c);
c.gridx = 0; ++c.gridy;
cp.add(new JLabel("maximumSize"),c);
++c.gridx;
maximumWidth = new JTextField(5);
cp.add(maximumWidth,c);
++c.gridx;
maximumHeight = new JTextField(5);
cp.add(maximumHeight,c);
c.gridx = 0; ++c.gridy; c.gridwidth = 3;
JPanel p = new JPanel(new FlowLayout());
updateButton = new JButton("Update");
getRootPane().setDefaultButton(updateButton);
updateButton.addActionListener(this);
p.add(updateButton);
cancelButton = new JButton("Cancel");
cancelButton.addActionListener(this);
p.add(cancelButton);
cp.add(p,c);
}
public void actionPerformed(ActionEvent ae) {
String ac = ae.getActionCommand();
if (ac.equals("Update")) {
if (validateFields()) {
flag = true;
setVisible(false);
}
} else if (ac.equals("Cancel")) {
setVisible(false);
}
}
public boolean validateFields() {
boolean retcod = true;
try {
int n = 0;
try {
minimumWidth.requestFocusInWindow();
n = Integer.parseInt(minimumWidth.getText());
} catch (NumberFormatException nfe) {
throw new NumberFormatException("minimumWidth");
}
if (n < 0)
throw new NumberFormatException("minimumWidth < 0");
try {
minimumHeight.requestFocusInWindow();
n = Integer.parseInt(minimumHeight.getText());
} catch (NumberFormatException nfe) {
throw new NumberFormatException("minimumHeight");
}
if (n < 0)
throw new NumberFormatException("minimumHeight < 0");
try {
preferredWidth.requestFocusInWindow();
n = Integer.parseInt(preferredWidth.getText());
} catch (NumberFormatException nfe) {
throw new NumberFormatException("preferredWidth");
}
if (n < 0)
throw new NumberFormatException("preferredWidth < 0");
try {
preferredHeight.requestFocusInWindow();
n = Integer.parseInt(preferredHeight.getText());
} catch (NumberFormatException nfe) {
throw new NumberFormatException("preferredHeight");
}
if (n < 0)
throw new NumberFormatException("preferredHeight < 0");
try {
maximumWidth.requestFocusInWindow();
n = Integer.parseInt(maximumWidth.getText());
} catch (NumberFormatException nfe) {
throw new NumberFormatException("maximumWidth");
}
if (n < 0)
throw new NumberFormatException("maximumWidth < 0");
try {
maximumHeight.requestFocusInWindow();
n = Integer.parseInt(maximumHeight.getText());
} catch (NumberFormatException nfe) {
throw new NumberFormatException("maximumHeight");
}
if (n < 0)
throw new NumberFormatException("maximumHeight < 0");
} catch (NumberFormatException nfe) {
retcod = false;
JOptionPane.showMessageDialog(SizesDialog.this,nfe,
"Invalid Data Error!",JOptionPane.ERROR_MESSAGE);
}
return retcod;
}
public void fillFields(Dimension min, Dimension pref, Dimension max) {
minimumWidth.setText(Integer.toString(min.width));
minimumHeight.setText(Integer.toString(min.height));
preferredWidth.setText(Integer.toString(pref.width));
preferredHeight.setText(Integer.toString(pref.height));
maximumWidth.setText(Integer.toString(max.width));
maximumHeight.setText(Integer.toString(max.height));
}
public Dimension[] emptyFields() {
Dimension[] dim = new Dimension[3];
dim[0] = new Dimension();
dim[1] = new Dimension();
dim[2] = new Dimension();
try {
dim[0].width = Integer.parseInt(minimumWidth.getText());
dim[0].height = Integer.parseInt(minimumHeight.getText());
dim[1].width = Integer.parseInt(preferredWidth.getText());
dim[1].height = Integer.parseInt(preferredHeight.getText());
dim[2].width = Integer.parseInt(maximumWidth.getText());
dim[2].height = Integer.parseInt(maximumHeight.getText());
} catch (NumberFormatException nfe) {
flag = false;
}
return dim;
}
public static Dimension[] show(Component comp, String title,
Dimension min,
Dimension pref, Dimension max) {
SizesDialog dialog = new SizesDialog(title);
dialog.fillFields(min,pref,max);
dialog.pack();
dialog.setLocationRelativeTo(comp);
dialog.setVisible(true);
dialog.dispose();
return dialog.flag ? dialog.emptyFields() : null;
}
}
//package com.knutejohnson.demos;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ConstraintsDialog extends JDialog implements ActionListener {
final String[] gridXYChoices =
{ "RELATIVE","0","1","2","3","4","5","6","7","8","9","10" };
final String[] gridWHChoices =
{ "REMAINDER","RELATIVE","1","2","3","4","5","6","7","8","9","10" };
final String[] anchorChoices = { "CENTER","NORTH","NORTHEAST","EAST",
"SOUTHEAST","SOUTH","SOUTHWEST","WEST","NORTHWEST" };
final String[] fillChoices = { "NONE","HORIZONTAL","VERTICAL","BOTH" };
final String[] insetsTip = { "Top","Left","Bottom","Right" } ;
final JComboBox gridx, gridy, gridwidth, gridheight, fill, anchor;
final JTextField weightx, weighty, insets[], ipadx, ipady;
final JButton updateButton, cancelButton;
private boolean flag;
public ConstraintsDialog(String title) {
super((Dialog)null,title,true);
Container cp = getContentPane();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
setVisible(false);
}
});
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(2,2,2,2);
c.weightx = 1.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0; c.gridy = 0;
cp.add(new JLabel("gridx"),c);
++c.gridx;
gridx = new JComboBox(gridXYChoices);
cp.add(gridx,c);
c.gridx = 0; ++c.gridy;
cp.add(new JLabel("gridy"),c);
++c.gridx;
gridy = new JComboBox(gridXYChoices);
cp.add(gridy,c);
c.gridx = 0; ++c.gridy;
cp.add(new JLabel("gridwidth"),c);
++c.gridx;
gridwidth = new JComboBox(gridWHChoices);
cp.add(gridwidth,c);
c.gridx = 0; ++c.gridy;
cp.add(new JLabel("gridheight"),c);
++c.gridx;
gridheight = new JComboBox(gridWHChoices);
cp.add(gridheight,c);
c.gridx = 0; ++c.gridy;
cp.add(new JLabel("weightx"),c);
++c.gridx;
weightx = new JTextField(4);
cp.add(weightx,c);
c.gridx = 0; ++c.gridy;
cp.add(new JLabel("weighty"),c);
++c.gridx;
weighty = new JTextField(4);
cp.add(weighty,c);
c.gridx = 2; c.gridy = 0;
cp.add(new JLabel("anchor"),c);
++c.gridx;
anchor = new JComboBox(anchorChoices);
cp.add(anchor,c);
c.gridx = 2; ++c.gridy;
cp.add(new JLabel("fill"),c);
++c.gridx;
fill = new JComboBox(fillChoices);
cp.add(fill,c);
c.gridx = 2; ++c.gridy;
cp.add(new JLabel("insets"),c);
++c.gridx;
JPanel p = new JPanel(new FlowLayout(FlowLayout.CENTER,0,0));
insets = new JTextField[4];
for (int i=0; i<insets.length; i++) {
insets[i] = new JTextField(2);
insets[i].setToolTipText(insetsTip[i]);
p.add(insets[i]);
}
cp.add(p,c);
c.gridx = 2; ++c.gridy;
cp.add(new JLabel("ipadx"),c);
++c.gridx;
ipadx = new JTextField(4);
cp.add(ipadx,c);
c.gridx = 2; ++c.gridy;
cp.add(new JLabel("ipady"),c);
++c.gridx;
ipady = new JTextField(4);
cp.add(ipady,c);
c.gridx = 0; c.gridy += 2; c.gridwidth = 2;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.EAST;
updateButton = new JButton("Update");
updateButton.addActionListener(this);
getRootPane().setDefaultButton(updateButton);
cp.add(updateButton,c);
c.gridx = 2;
c.anchor = GridBagConstraints.WEST;
cancelButton = new JButton("Cancel");
cancelButton.addActionListener(this);
cp.add(cancelButton,c);
}
public void actionPerformed(ActionEvent ae) {
String ac = ae.getActionCommand();
if (ac.equals("Update")) {
if (validateFields()) {
flag = true;
setVisible(false);
}
} else if (ac.equals("Cancel"))
setVisible(false);
}
public boolean validateFields() {
boolean retcod = true;
try {
double w = 0.0;
try {
weightx.requestFocusInWindow();
w = Double.parseDouble(weightx.getText());
} catch (NumberFormatException nfe) {
throw new NumberFormatException("weightx");
}
if (w < 0.0)
throw new NumberFormatException("weightx < 0.0");
try {
weighty.requestFocusInWindow();
w = Double.parseDouble(weighty.getText());
} catch (NumberFormatException nfe) {
throw new NumberFormatException("weighty");
}
if (w < 0.0)
throw new NumberFormatException("weighty < 0.0");
int n[] = new int[insets.length];
try {
for (int i=0; i<insets.length; i++) {
insets[i].requestFocusInWindow();
n[i] = Integer.parseInt(insets[i].getText());
}
} catch (NumberFormatException nfe) {
throw new NumberFormatException("insets");
}
for (int i=0; i<insets.length; i++) {
if (n[i] < 0) {
insets[i].requestFocusInWindow();
throw new NumberFormatException("inset < 0");
}
}
int i = 0;
try {
ipadx.requestFocusInWindow();
i = Integer.parseInt(ipadx.getText());
} catch (NumberFormatException nfe) {
throw new NumberFormatException("ipadx");
}
try {
ipady.requestFocusInWindow();
i = Integer.parseInt(ipady.getText());
} catch (NumberFormatException nfe) {
throw new NumberFormatException("ipady");
}
} catch (NumberFormatException nfe) {
retcod = false;
JOptionPane.showMessageDialog(ConstraintsDialog.this,nfe,
"Invalid Data Error!",JOptionPane.ERROR_MESSAGE);
}
return retcod;
}
public void fillFields(GridBagConstraints constraints) {
if (constraints.gridx == GridBagConstraints.RELATIVE)
gridx.setSelectedItem("RELATIVE");
else
gridx.setSelectedItem(Integer.toString(constraints.gridx));
if (constraints.gridy == GridBagConstraints.RELATIVE)
gridy.setSelectedItem("RELATIVE");
else
gridy.setSelectedItem(Integer.toString(constraints.gridy));
if (constraints.gridwidth == GridBagConstraints.RELATIVE)
gridwidth.setSelectedItem("RELATIVE");
else if (constraints.gridwidth == GridBagConstraints.REMAINDER)
gridwidth.setSelectedItem("REMAINDER");
else
gridwidth.setSelectedItem(Integer.toString(constraints.gridwidth));
if (constraints.gridheight == GridBagConstraints.RELATIVE)
gridheight.setSelectedItem("RELATIVE");
else if (constraints.gridheight == GridBagConstraints.REMAINDER)
gridheight.setSelectedItem("REMAINDER");
else
gridheight.setSelectedItem(
Integer.toString(constraints.gridheight));
weightx.setText(Double.toString(constraints.weightx));
weighty.setText(Double.toString(constraints.weighty));
switch (constraints.anchor) {
case GridBagConstraints.CENTER:
anchor.setSelectedItem("CENTER"); break;
case GridBagConstraints.NORTH:
anchor.setSelectedItem("NORTH"); break;
case GridBagConstraints.NORTHEAST:
anchor.setSelectedItem("NORTHEAST"); break;
case GridBagConstraints.EAST:
anchor.setSelectedItem("EAST"); break;
case GridBagConstraints.SOUTHEAST:
anchor.setSelectedItem("SOUTHEAST"); break;
case GridBagConstraints.SOUTH:
anchor.setSelectedItem("SOUTH"); break;
case GridBagConstraints.SOUTHWEST:
anchor.setSelectedItem("SOUTHWEST"); break;
case GridBagConstraints.WEST:
anchor.setSelectedItem("WEST"); break;
case GridBagConstraints.NORTHWEST:
anchor.setSelectedItem("NORTHWEST"); break;
default: break;
}
switch (constraints.fill) {
case GridBagConstraints.NONE:
fill.setSelectedItem("NONE"); break;
case GridBagConstraints.HORIZONTAL:
fill.setSelectedItem("HORIZONTAL"); break;
case GridBagConstraints.VERTICAL:
fill.setSelectedItem("VERTICAL"); break;
case GridBagConstraints.BOTH:
fill.setSelectedItem("BOTH"); break;
default: break;
}
insets[0].setText(Integer.toString(constraints.insets.top));
insets[1].setText(Integer.toString(constraints.insets.left));
insets[2].setText(Integer.toString(constraints.insets.bottom));
insets[3].setText(Integer.toString(constraints.insets.right));
ipadx.setText(Integer.toString(constraints.ipadx));
ipady.setText(Integer.toString(constraints.ipady));
}
public GridBagConstraints emptyFields() {
GridBagConstraints constraints = new GridBagConstraints();
try {
String str = (String)gridx.getSelectedItem();
if (str.equals("RELATIVE"))
constraints.gridx = GridBagConstraints.RELATIVE;
else
constraints.gridx = Integer.parseInt(str);
str = (String)gridy.getSelectedItem();
if (str.equals("RELATIVE"))
constraints.gridy = GridBagConstraints.RELATIVE;
else
constraints.gridy = Integer.parseInt(str);
str = (String)gridwidth.getSelectedItem();
if (str.equals("REMAINDER"))
constraints.gridwidth = GridBagConstraints.REMAINDER;
else if (str.equals("RELATIVE"))
constraints.gridwidth = GridBagConstraints.RELATIVE;
else
constraints.gridwidth = Integer.parseInt(str);
str = (String)gridheight.getSelectedItem();
if (str.equals("REMAINDER"))
constraints.gridheight = GridBagConstraints.REMAINDER;
else if (str.equals("RELATIVE"))
constraints.gridheight = GridBagConstraints.RELATIVE;
else
constraints.gridheight = Integer.parseInt(str);
constraints.weightx =
Double.parseDouble((String)weightx.getText());
constraints.weighty =
Double.parseDouble((String)weighty.getText());
str = (String)anchor.getSelectedItem();
if (str.equals("CENTER"))
constraints.anchor = GridBagConstraints.CENTER;
else if (str.equals("NORTH"))
constraints.anchor = GridBagConstraints.NORTH;
else if (str.equals("NORTHEAST"))
constraints.anchor = GridBagConstraints.NORTHEAST;
else if (str.equals("EAST"))
constraints.anchor = GridBagConstraints.EAST;
else if (str.equals("SOUTHEAST"))
constraints.anchor = GridBagConstraints.SOUTHEAST;
else if (str.equals("SOUTH"))
constraints.anchor = GridBagConstraints.SOUTH;
else if (str.equals("SOUTHWEST"))
constraints.anchor = GridBagConstraints.SOUTHWEST;
else if (str.equals("WEST"))
constraints.anchor = GridBagConstraints.WEST;
else if (str.equals("NORTHWEST"))
constraints.anchor = GridBagConstraints.NORTHWEST;
str = (String)fill.getSelectedItem();
if (str.equals("NONE"))
constraints.fill = GridBagConstraints.NONE;
else if (str.equals("HORIZONTAL"))
constraints.fill = GridBagConstraints.HORIZONTAL;
else if (str.equals("VERTICAL"))
constraints.fill = GridBagConstraints.VERTICAL;
else if (str.equals("BOTH"))
constraints.fill = GridBagConstraints.BOTH;
int[] in = new int[insets.length];
for (int i=0; i<insets.length; i++)
in[i] = Integer.parseInt(insets[i].getText());
constraints.insets = new Insets(in[0],in[1],in[2],in[3]);
constraints.ipadx = Integer.parseInt((String)ipadx.getText());
constraints.ipady = Integer.parseInt((String)ipady.getText());
} catch (NumberFormatException nfe) {
flag = false;
}
return constraints;
}
public static GridBagConstraints show(Component comp, String title,
GridBagConstraints constraints) {
ConstraintsDialog dialog = new ConstraintsDialog(title);
dialog.fillFields(constraints);
dialog.pack();
dialog.setLocationRelativeTo(comp);
dialog.setVisible(true);
dialog.dispose();
return dialog.flag ? dialog.emptyFields() : null;
}
}
--
Knute Johnson
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-20 09:15 +0200
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-20 11:12 +0200
Re: What do you think of my CropImage class Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-07-20 10:02 -0700
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-20 21:14 +0200
Re: What do you think of my CropImage class Eric Douglas <e.d.programmer@gmail.com> - 2016-07-20 12:50 -0700
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-21 12:11 +0200
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-21 14:19 +0200
Re: What do you think of my CropImage class Qu0ll <qu0ll64@gmail.com> - 2016-07-21 05:53 +1000
Re: What do you think of my CropImage class Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-07-20 13:46 -0700
Re: What do you think of my CropImage class Eric Douglas <e.d.programmer@gmail.com> - 2016-07-20 13:59 -0700
Re: What do you think of my CropImage class Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-07-20 15:40 -0700
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-21 12:27 +0200
Re: What do you think of my CropImage class Eric Douglas <e.d.programmer@gmail.com> - 2016-07-21 05:17 -0700
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-21 17:43 +0200
Re: What do you think of my CropImage class Eric Douglas <e.d.programmer@gmail.com> - 2016-07-21 08:59 -0700
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-21 19:10 +0200
Re: What do you think of my CropImage class Arne Vajhøj <arne@vajhoej.dk> - 2016-07-23 21:47 -0400
Re: What do you think of my CropImage class Eric Douglas <e.d.programmer@gmail.com> - 2016-07-25 04:51 -0700
Re: What do you think of my CropImage class Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-07-25 15:01 -0700
Re: What do you think of my CropImage class Arne Vajhøj <arne@vajhoej.dk> - 2016-07-25 21:06 -0400
Re: What do you think of my CropImage class Arne Vajhøj <arne@vajhoej.dk> - 2016-07-25 21:09 -0400
Re: What do you think of my CropImage class Nigel Wade <nmw@local.domain> - 2016-07-21 15:44 +0100
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-21 17:46 +0200
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-21 12:25 +0200
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-21 14:16 +0200
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-21 12:05 +0200
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-21 13:23 +0200
Re: What do you think of my CropImage class Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-07-21 07:58 -0700
Re: What do you think of my CropImage class Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-07-21 11:13 -0400
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-21 18:12 +0200
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-21 17:49 +0200
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-21 11:58 +0200
Re: What do you think of my CropImage class Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-07-21 09:35 -0400
Re: What do you think of my CropImage class Eric Douglas <e.d.programmer@gmail.com> - 2016-07-21 09:03 -0700
Re: What do you think of my CropImage class Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-07-21 12:39 -0400
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-21 18:07 +0200
Re: What do you think of my CropImage class Nigel Wade <nmw@local.domain> - 2016-07-21 17:23 +0100
Re: What do you think of my CropImage class Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-07-21 12:56 -0400
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-21 19:03 +0200
Re: What do you think of my CropImage class Eric Douglas <e.d.programmer@gmail.com> - 2016-07-21 10:25 -0700
Re: What do you think of my CropImage class Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-07-21 14:24 -0400
Re: What do you think of my CropImage class Eric Douglas <e.d.programmer@gmail.com> - 2016-07-21 11:54 -0700
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-21 20:25 +0200
Re: What do you think of my CropImage class Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-07-21 15:37 -0400
Re: What do you think of my CropImage class Martin Gregorie <martin@address-in-sig.invalid> - 2016-07-21 21:30 +0000
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-21 23:52 +0200
Re: What do you think of my CropImage class Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-07-21 20:35 -0400
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-22 04:21 +0200
Re: What do you think of my CropImage class Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-07-21 22:44 -0400
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-22 10:27 +0200
Re: What do you think of my CropImage class Martin Gregorie <martin@address-in-sig.invalid> - 2016-07-22 10:47 +0000
Re: What do you think of my CropImage class Eric Douglas <e.d.programmer@gmail.com> - 2016-07-22 05:16 -0700
Re: What do you think of my CropImage class Nigel Wade <nmw@local.domain> - 2016-07-22 15:10 +0100
Re: What do you think of my CropImage class Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-07-22 07:47 -0700
Re: What do you think of my CropImage class Eric Douglas <e.d.programmer@gmail.com> - 2016-07-22 10:21 -0700
Re: What do you think of my CropImage class Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-07-21 08:30 -0700
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-21 18:19 +0200
Re: What do you think of my CropImage class Eric Douglas <e.d.programmer@gmail.com> - 2016-07-21 10:18 -0700
Re: What do you think of my CropImage class Cecil Westerhof <Cecil@decebal.nl> - 2016-07-21 20:12 +0200
csiph-web