Hey, this is the first program I've tried writing using any GUI and I have gotten everything so far, except every time I click my button to try and run my program it just freezes and does not do anything.

import javax.swing.*;
import BreezySwing.*;
public class piGUI extends GBFrame{

private JLabel iterationLabel;
private JLabel numberLabel;
private DoubleField iterationField;
private DoubleField numberField;
private JButton iterationButton;
private double iteration;
private double number;



public piGUI(){

iterationLabel = addLabel ("Iterations" ,1,1,1,1);
numberLabel = addLabel ("Pi" ,1,2,1,1);
iterationField = addDoubleField (0.0 ,2,1,1,1);
numberField = addDoubleField (0.0 ,2,2,1,1);
iterationButton = addButton (">>>>>" ,3,1,1,1);}

public void buttonClicked (JButton buttonObj){

 double number;
 double iteration;
 number = (numberField.getNumber());
 iteration = (iterationField.getNumber());


  for (int i=1; i < iteration; i++) {

  if(number % 2 == 1)
  {
  number=number-1.0/(2*i-1);
  i=i+1;
  }
  else
  {
  number=number+1.0/(2*i-1);
  i=i+1;
  number = number * 4;
  }
  }
  numberField.setNumber(number);
  }

public static void main (String[] args){
piGUI theGUI = new piGUI();
theGUI.setSize (250, 100);
theGUI.setVisible(true);
}
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Swing is strictly single threaded, and all code must be done on EDT and your JComponents always wait for long iterations, GUI's freeeze during this long operation

you have to move your iterations to the backGround Task

1/ using SwingWorker (this section contains Concurency, EDT a SwingWorker)
http://download.oracle.com/javase/tutorial/uiswing/concurrency/index.html
be sure that you are check deamon bug for java version < 1.6.22

2/ using Runnable#run, but with Threads you must calculate with Concurency and you must publish any output on EDT

please this code contains one unsolved issue with Concurency and EDT

TableHeader repaint (only mouseClick repaints value), read carrefully sections about EDT and then you'll able to ...

hint: separate void and the code must be packed into SwingUtilities.invokeLater(new Runnable() {

import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Random;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.table.*;

public class TableWithTimer extends JFrame implements ActionListener, Runnable {

    private static final long serialVersionUID = 1L;
    private JScrollPane scroll = new JScrollPane();
    private JTable myTable;
    private JButton startButton = new JButton("Start Thread to Update Table");
    private JButton stopButton = new JButton("Stop Thread for Update Table");
    private int count = 0;
    private int delay = 3;
    private javax.swing.Timer timer = null;
    private boolean runProcess;
    private int row = 0;
    private int column = 0;
    private String value = "Amnd";
    private int amndValue = 0;
    private String valueAt = "";
    private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

    public TableWithTimer() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String[] head = {"One", "Two", "Three", "Four", "Five", "Six"};
        String[][] data = new String[25][6];
        myTable = new TableBackroundPaint0(data, head);
        myTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        myTable.setGridColor(Color.gray);
        myTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        final TableCellRenderer cellRendener = myTable.getTableHeader().getDefaultRenderer();
        myTable.getTableHeader().setDefaultRenderer(new TableCellRenderer() {

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value,
                    boolean isSelected, boolean hasFocus, int row, int column) {
                JLabel label = (JLabel) cellRendener.getTableCellRendererComponent(
                        table, value, isSelected, hasFocus, row, column);
                label.setBackground(Color.orange);
                label.setForeground(Color.darkGray);
                label.setFont(new Font("SansSerif", Font.BOLD, 12));
                label.setBorder(BorderFactory.createCompoundBorder(label.getBorder(),
                        BorderFactory.createEmptyBorder(0, 5, 0, 0)));
                label.setHorizontalAlignment(SwingConstants.LEFT);
                label.setHorizontalAlignment(SwingConstants.CENTER);
                if ((label.getText().equals("First")) || (label.getText().equals("Second"))) {
                    label.setForeground(Color.red);
                }
                if ((label.getText().equals("Day")) || (label.getText().equals("Month")) || (label.getText().equals("Year"))) {
                    label.setForeground(Color.blue);
                }
                if ((label.getText().equals("Time"))) {
                    label.setForeground(Color.green);
                }
                return label;
            }
        });
        TableColumnModel cm = myTable.getColumnModel();
        for (int column1 = 0; column1 < cm.getColumnCount(); column1++) {
            TableColumn colLeft1 = cm.getColumn(column1);
            cm.getColumn(column1).setWidth(140);
            cm.getColumn(column1).setPreferredWidth(140);
        }
        //myTable.setFillsViewportHeight(true); // apply paintComponent for whole Viewport
        JButton cornerButtonTop = new JButton();
        cornerButtonTop.setBackground(scroll.getViewport().getBackground());
        JButton cornerButtonBottom = new JButton();
        cornerButtonBottom.setOpaque(false);
        scroll.setCorner(JScrollPane.UPPER_RIGHT_CORNER, cornerButtonTop);
        scroll.setCorner(JScrollPane.LOWER_RIGHT_CORNER, cornerButtonBottom);
        scroll.setViewportView(myTable);
        scroll.setMinimumSize(new Dimension(600, 400));
        scroll.setMaximumSize(new Dimension(900, 600));
        scroll.setPreferredSize(new Dimension(850, 430));
        startButton.addActionListener(this);
        add(startButton, BorderLayout.NORTH);
        startButton.setEnabled(false);
        add(scroll, BorderLayout.CENTER);
        stopButton.addActionListener(this);
        add(stopButton, BorderLayout.SOUTH);
        stopButton.setEnabled(false);
        setLocation(100, 100);
        pack();
        setVisible(true);
        start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == startButton) {
            runProcess = true;
            new Thread(this).start();
            myTable.requestFocus();
            startButton.setEnabled(false);
            stopButton.setEnabled(true);
        } else if (e.getSource() == stopButton) {
            runProcess = false;
            startButton.setEnabled(true);
            stopButton.setEnabled(false);
        }
    }

    @Override
    public void run() {
        count = 1;
        Random random = new Random();
        while (runProcess) {
            row = random.nextInt(myTable.getRowCount());
            column = random.nextInt(myTable.getColumnCount());
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    try {
                        amndValue++;
                        valueAt = ((myTable.getValueAt(row, column)).toString());
                        if (!(valueAt.startsWith("A"))) {
                            java.util.Date date = new java.util.Date();
                            String dateTime = sdf.format(date.getTime());
                            myTable.setValueAt((value + " " + String.valueOf(amndValue) + " at: " + dateTime), row, column);
                            //myTable.setValueAt(new Integer(1), row, column); // please uncoment for generate misstype error on EDT
                            myTable.changeSelection(row, column, false, false);
                            System.out.println("update cycle with value :"
                                    + (value + " " + String.valueOf(amndValue) + " at: " + dateTime) + ", table row :" + row
                                    + ", table column " + column);
                        }
                    } catch (Exception e) {
                        runProcess = false;
                        System.out.println("Error for update JTable cell");
                        e.printStackTrace();
                    }
                }
            });
            try {
                Thread.sleep(500);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public Action updateCol() {
        return new AbstractAction("text load action") {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("updating row " + (count + 1));
                TableModel model = myTable.getModel();
                int cols = model.getColumnCount();
                int row = 0;
                for (int j = 0; j < cols; j++) {
                    row = count;
                    myTable.changeSelection(row, 0, false, false);
                    timer.setDelay(200);
                    Object value = "row " + (count + 1) + " item " + (j + 1);
                    model.setValueAt(value, count, j);
                }
                count++;
                if (count >= myTable.getRowCount()) {
                    myTable.changeSelection(0, 0, false, false);
                    timer.stop();
                    for (int j = 0; j < cols; j++) {
                        String colName = model.getColumnName(j);
                        if (colName.equals("One")) {
                            myTable.getColumnModel().getColumn(j).setHeaderValue("First");
                        }
                        if (colName.equals("Two")) {
                            myTable.getColumnModel().getColumn(j).setHeaderValue("Second");
                        }
                        if (colName.equals("Three")) {
                            myTable.getColumnModel().getColumn(j).setHeaderValue("Day");
                        }
                        if (colName.equals("Four")) {
                            myTable.getColumnModel().getColumn(j).setHeaderValue("Month");
                        }
                        if (colName.equals("Five")) {
                            myTable.getColumnModel().getColumn(j).setHeaderValue("Year");
                        }
                        if (colName.equals("Six")) {
                            myTable.getColumnModel().getColumn(j).setHeaderValue("Time");
                        }
                    }
                    System.out.println("update cycle completed");
                    myTable.clearSelection();
                    startButton.setEnabled(true);
                }
            }
        };
    }

    private void start() {
        timer = new javax.swing.Timer(delay * 100, updateCol());
        timer.start();
    }

    public static void main(String args[]) {
        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                System.out.println(info.getName());
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (UnsupportedLookAndFeelException e) {
            // handle exception
        } catch (ClassNotFoundException e) {
            // handle exception
        } catch (InstantiationException e) {
            // handle exception
        } catch (IllegalAccessException e) {
            // handle exception
        }
        TableWithTimer tableWithTimer = new TableWithTimer();
    }
}

class TableBackroundPaint0 extends JTable {

    private static final long serialVersionUID = 1L;

    TableBackroundPaint0(Object[][] data, Object[] head) {
        super(data, head);
        setOpaque(false);
        ((JComponent) getDefaultRenderer(Object.class)).setOpaque(false);
    }

    @Override
    public void paintComponent(Graphics g) {
        Color background = new Color(168, 210, 241);
        Color controlColor = new Color(230, 240, 230);
        int width = getWidth();
        int height = getHeight();
        Graphics2D g2 = (Graphics2D) g;
        Paint oldPaint = g2.getPaint();
        g2.setPaint(new GradientPaint(0, 0, background, width, 0, controlColor));
        g2.fillRect(0, 0, width, height);
        g2.setPaint(oldPaint);
        for (int row : getSelectedRows()) {
            Rectangle start = getCellRect(row, 0, true);
            Rectangle end = getCellRect(row, getColumnCount() - 1, true);
            g2.setPaint(new GradientPaint(start.x, 0, controlColor, (int) ((end.x + end.width - start.x) * 1.25), 0, Color.orange));
            g2.fillRect(start.x, start.y, end.x + end.width - start.x, start.height);
        }
        super.paintComponent(g);
    }
}

much luck

Are you required to use BreezySwing? If not I'd forget about it and move on the "real" Swing without wasting any more time. BreezySwing just seems to be a very old set of "training wheels" from when people were still struggling with AWT before/just after Swing was released.
I think its a safe bet you won't find it in use in any commercial environment today, so it's bit of a dead end.
But then if it's a requirement of a course you are on, there's not a lot you can do...

This article has been dead for over six months. Start a new discussion instead.