Sorry I don’t have much knowledge about Java or swing applications. I created a dialog called DlgShape and in it i have 2 text fields, 2 buttons and 2 labels. I tried creating DlgRectangle and instead of it inheriting from JDialog I inherited from DlgShape. The design of the parent and child class are identical but in the DlgRectangle I don’t have the labels, buttons and text fields listed out as properties, I have public get and set methods and getContentPane. Suddenly my layout of the DlgRectangle changed from GridBagLayout to BorderLayout, why did it do that? I have the default DlgShape constructor set the layout to GridBagLayout and my DlgRectangle constructor also calls the super constructor.
I tried manually typing the code to create a label and put it in the content panel but it doesn’t appear. I had a thought that maybe I need to get the content panel to be a property of DlgShape and maybe make it protected.
Here’s my code for DlgShape: package drawing;
public class DlgShape extends JDialog { private final JPanel contentPanel = new JPanel(); private boolean validInput; private JTextField txtX; private JTextField txtY; private JButton btnOuterColor = new JButton("outer color"); private JButton btnInnerColor = new JButton("inner color"); JLabel lblX = new JLabel("X"); private Shape shape; private void setShape(Shape shape) { this.shape=shape; } private Shape getShape(Shape shape) { return shape; } public void setValidInput(boolean validInput) { this.validInput=validInput; } public boolean isValidInput() { return this.validInput; } public JTextField getTxtX() { return txtX; } public JTextField getTxtY() { return txtY; } /** * Launch the application. */ public static void main(String[] args) { try { DlgShape dialog = new DlgShape(); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } /** * Create the dialog. */ public DlgShape() { setModal(true); setBounds(100, 100, 233, 300); //Auto generated getContentPane().setLayout(new BorderLayout()); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); getContentPane().add(contentPanel, BorderLayout.CENTER); GridBagLayout gbl_contentPanel = new GridBagLayout(); gbl_contentPanel.columnWidths = new int[]{0, 0, 0}; gbl_contentPanel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0}; gbl_contentPanel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE}; gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; contentPanel.setLayout(gbl_contentPanel); { GridBagConstraints gbc_lblX = new GridBagConstraints(); gbc_lblX.anchor = GridBagConstraints.EAST; gbc_lblX.insets = new Insets(0, 0, 5, 5); gbc_lblX.gridx = 0; gbc_lblX.gridy = 0; contentPanel.add(lblX, gbc_lblX); } { txtX = new JTextField(); GridBagConstraints gbc_txtX = new GridBagConstraints(); gbc_txtX.insets = new Insets(0, 0, 5, 0); gbc_txtX.fill = GridBagConstraints.HORIZONTAL; gbc_txtX.gridx = 1; gbc_txtX.gridy = 0; contentPanel.add(txtX, gbc_txtX); txtX.setColumns(10); } { JLabel lblY = new JLabel("Y"); GridBagConstraints gbc_lblY = new GridBagConstraints(); gbc_lblY.anchor = GridBagConstraints.EAST; gbc_lblY.insets = new Insets(0, 0, 5, 5); gbc_lblY.gridx = 0; gbc_lblY.gridy = 1; contentPanel.add(lblY, gbc_lblY); } { txtY = new JTextField(); GridBagConstraints gbc_txtY = new GridBagConstraints(); gbc_txtY.insets = new Insets(0, 0, 5, 0); gbc_txtY.fill = GridBagConstraints.HORIZONTAL; gbc_txtY.gridx = 1; gbc_txtY.gridy = 1; contentPanel.add(txtY, gbc_txtY); txtY.setColumns(10); } { Component verticalStrut = Box.createVerticalStrut(20); GridBagConstraints gbc_verticalStrut = new GridBagConstraints(); gbc_verticalStrut.insets = new Insets(0, 0, 5, 0); gbc_verticalStrut.gridx = 1; gbc_verticalStrut.gridy = 5; contentPanel.add(verticalStrut, gbc_verticalStrut); } { GridBagConstraints gbc_btnOuterColor = new GridBagConstraints(); gbc_btnOuterColor.insets = new Insets(0, 0, 5, 0); gbc_btnOuterColor.gridx = 1; gbc_btnOuterColor.gridy = 6; contentPanel.add(btnOuterColor, gbc_btnOuterColor); } { GridBagConstraints gbc_btnInnerColor = new GridBagConstraints(); gbc_btnInnerColor.gridx = 1; gbc_btnInnerColor.gridy = 7; contentPanel.add(btnInnerColor, gbc_btnInnerColor); } { JPanel buttonPane = new JPanel(); buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); getContentPane().add(buttonPane, BorderLayout.SOUTH); { JButton okButton = new JButton("OK"); okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { int x=Integer.parseInt(txtX.getText()); int y=Integer.parseInt(txtY.getText()); validInput=true; }catch(Exception exc){ JOptionPane.showMessageDialog(null, "Invalid values"); } } }); okButton.setActionCommand("OK"); buttonPane.add(okButton); getRootPane().setDefaultButton(okButton); } { JButton cancelButton = new JButton("Cancel"); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { validInput=false; setVisible(false); } }); cancelButton.setActionCommand("Cancel"); buttonPane.add(cancelButton); } } } public DlgShape(String title,Shape shape) { this(); setTitle(title); setShape(shape); if(shape instanceof Point) { txtX.setText(String.valueOf(((Point)shape).getX())); txtY.setText(String.valueOf(((Point)shape).getY())); btnInnerColor.setVisible(false); btnOuterColor.setText("color"); }else if(shape instanceof Line) { txtX.setText(String.valueOf(((Line)shape).getStartPoint().getX())); txtY.setText(String.valueOf(((Line)shape).getStartPoint().getY())); btnInnerColor.setVisible(false); btnOuterColor.setText("color"); }else if(shape instanceof Rectangle) { txtX.setText(String.valueOf(((Rectangle)shape).getUpperLeftPoint().getX())); txtY.setText(String.valueOf(((Rectangle)shape).getUpperLeftPoint().getY())); }else if(shape instanceof Circle || shape instanceof Donut) { txtX.setText(String.valueOf(((Circle)shape).getCenter().getX())); txtY.setText(String.valueOf(((Circle)shape).getCenter().getY())); } } }
and code for DlgRectangle, well DlgRectangle1 but lets call it DlgRectangle:
public class DlgRectangle1 extends DlgShape { private final JPanel contentPanel = new JPanel(); /** * Launch the application. */ public static void main(String[] args) { try { DlgRectangle1 dialog = new DlgRectangle1(); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } /** * Create the dialog. */ public DlgRectangle1() { super(); //this.getContentPane().add(new JLabel("widht")); JLabel lblWidth = new JLabel("width"); GridBagConstraints gbc_lblWidth = new GridBagConstraints(); gbc_lblWidth.anchor = GridBagConstraints.EAST; gbc_lblWidth.insets = new Insets(0, 0, 5, 5); gbc_lblWidth.gridx = 0; gbc_lblWidth.gridy = 2; contentPanel.add(lblWidth, gbc_lblWidth); } public DlgRectangle1(String title,Rectangle rectangle) { super(title,rectangle); } }
Advertisement
Answer
I just needed to switch my properties in DlgShape from private to public or protected.
So
private JTextField txtX; private JTextField txtY; private JButton btnOuterColor = new JButton("outer color"); private JButton btnInnerColor = new JButton("inner color");
should be
public JTextField txtX; public JTextField txtY; public JButton btnOuterColor = new JButton("outer color"); public JButton btnInnerColor = new JButton("inner color");
Credits go to the comment by @Bohemian