eclipse-windowbuilder / windowbuilder

Eclipse Windowbuilder
https://projects.eclipse.org/projects/tools.windowbuilder
Eclipse Public License 1.0
78 stars 30 forks source link

disappering butttons #463

Closed billykd4dpb closed 1 year ago

billykd4dpb commented 1 year ago

Here is my issue. I have a frame with label, jtextfield, and 2 buttons when you run the program the label and the textfield show but the butttons dont until you mouse over them. i was told i needed to post here from eclipse forum even though this is cut and paist from a you tube video but didnt run using winbuilder. When i try to run the Scientific Caalculator it has the messages the"cannot refer to nonfinal verible in scope. i am running ubuntu linux and openjdk 8.0 here is my code import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

//import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField;

public class SC4 { static JFrame frame; static JTextField textField;

double first;
double second;
double result;
String operation;
String answer;
//private final ButtonGroup buttonGroup = new ButtonGroup();

public static void main(String[] args) {
    // TODO Auto-generated method stub

    //private void initialize() {
        JFrame frame = new JFrame();
        frame.setForeground(Color.WHITE);
        frame.getContentPane().setForeground(Color.BLACK);
        frame.setBounds(100, 100, 357, 539);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setVisible(true);

        JLabel lblNewLabel = new JLabel("SCIENTFIC CALCULATOR");
        lblNewLabel.setBounds(10, 11, 320, 27);
        lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 23));
        frame.getContentPane().add(lblNewLabel);

        JTextField textField = new JTextField();
        textField.setFont(new Font("Tahoma", Font.BOLD, 15));
        textField.setBounds(10, 39, 320, 63);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        JButton btn2 = new JButton("2");
        btn2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String number = textField.getText() + btn2.getText();
                textField.setText(number);
            }
        });
        btn2.setFont(new Font("Tahoma", Font.BOLD, 23));
        btn2.setBounds(139, 398, 64, 50);
        frame.getContentPane().add(btn2);

        JButton btn5 = new JButton("5");
        btn5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String number = textField.getText() + btn5.getText();
                textField.setText(number);
            }
        });
        btn5.setFont(new Font("Tahoma", Font.BOLD, 23));
        btn5.setBounds(139, 347, 64, 50);
        frame.getContentPane().add(btn5);
ptziegler commented 1 year ago

Here is my issue. I have a frame with label, jtextfield, and 2 buttons when you run the program the label and the textfield show but the butttons dont until you mouse over them.

I don't think that's necessarily a WindowBuilder bug but just how Swing behaves. The primary issues seems to be that you call frame.setVisible(true), before adding the buttons. Because you also don't use a layout, there is no incentive for Swing to redraw the button area. That is until you hover over them.

In short, there are two ways to fix this:

The latter one is the more elegant and also more stable solution.

When i try to run the Scientific Caalculator it has the messages the"cannot refer to nonfinal verible in scope.

That's sadly not something I could reproduce with the code snippet you provided. Which version of WindowBuilder are you using? Almost any newer version should require at least Java 11, so I find it odd that you mention Java 8.

merks commented 1 year ago

@ptziegler

Note that this above is hand written code and is not using WindowBuilder as also appears in this thread:

https://www.eclipse.org/forums/index.php?t=msg&th=1113069&goto=1859241&#msg_1859241

wimjongman commented 1 year ago

This should be a discussion but I cannot convert it.