Re: Extending JPanel - solved it


2005-09-02 07:16:34 AM
jbuilder19
It was as simple as take this line out!
// ohmsLawPanel.setLayout(gridLayout1);
Magnus wrote:
Quote
Hi,

I want to build an application for set of calculators, about 20, (i.e.
formulas where the user enters value, click calculate, and get the answer).

My plan is to create a frame with a selection list to the left (in a
JPanel) and a JPanel (cardlayout) to the right showing the formula
selected in the list.

Having worked with JB and cardlayout, Design mode gets really slow when
a lot of labels and fields are entered. So my thought was to create a
class per formula and extend JPanel and the only code in "main" would be
to show the right panel with

cardLayout1.show(jPanel2,"formula1Panel"); // jPanel2 is my cardlayout
panel

It works to a part. I create my formula1 class and set the Layout to
GridBagLayout. It all looks good in design.
In main (to test before I create my showRightFormula method) I try to
just display the right panel with code as above, right after call to
jbInit(). The panel shows empty.

When I in design mode for "main" set different Layout managers, they
override my formula1-class layout and displays as it feels like.

What to do?

--- Code ---

// main
JPanel jPanel1 = new JPanel(); // where to put list
JScrollPane jScrollPane1 = new JScrollPane();
JList calcSelectjList = new JList();
GridBagLayout gridBagLayout1 = new GridBagLayout();
JPanel jPanel2 = new JPanel(); // cardlayout panel
CardLayout cardLayout1 = new CardLayout();
formula1 ohmsLawPanel = new formula1();
JPanel emptyPanel = new JPanel(); // to show when no selection

//cosntructor
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
calcSelectjList.setListData(calculators);
cardLayout1.show(jPanel2,"ohmsLawPanel");
} catch (Exception exception) {
exception.printStackTrace();
}

// my extended class

class fomula1 extends JPanel {

JTextField jTextField1 = new JTextField();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JButton jButton1 = new JButton();
GridBagLayout gridBagLayout1 = new GridBagLayout();

public formula1() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
}

private void jbInit() throws Exception {
...
jLabel1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 20));
jLabel1.setText("=");
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel3.setText("*");

jButton1.setMaximumSize(new Dimension(80, 30));
jButton1.setMinimumSize(new Dimension(80, 30));
jButton1.setPreferredSize(new Dimension(80, 30));

jButton1.setText("Reset");
...
this.add(jButton1, new GridBagConstraints(3, 2, 2, 1, 0.0, 0.0
, GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(13, 10, 18, 23), 20, 0));
}