2

I'm kinda new to Java (I only have experience with Processing) and I have a question.

I'm building a small task planning application in Eclipse. I'm using the MigLayout. Every task has its own row (see image). I use three classes, TaskRowDone, TaskRowBusy and TaskRowToDo.

The app

public HoofdScherm() {
    initialize();
    drawDone();
    drawBusy();
    drawToDo();
    drawNewBtn();
}

The code above draws the main screen. The code below draws the button labeled "Nieuwe taak" (New task). I want to be able to click this button to make a new task row. I've tried putting the lines TaskRowToDo trtd3 = new TaskRowToDo(5, true, "test", 3); and trtd3.draw() inside the actionPerformed function of the button (as a way of testing), but this doesn't seem to do anything. Does the program only run the functions inside 'HoofdScherm' once and then stop drawing? If this is the case, how do I structure the program so the layout can always be changed? I tried a while(true) loop inside 'HoofdScherm' so it kept looping but of course this crashed the computer.

private void drawNewBtn(){
    JButton btnNew = new JButton("Nieuwe taak");
    btnNew.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("New task added");   
        }
    });
    frmPlanner.getContentPane().add(btnNew, "cell 3 7");
}
Jonah
  • 1,013
  • 15
  • 25
kajdehoop
  • 517
  • 8
  • 22
  • I haven't done GUI programming in Java in some time, but I remember needing to implement a `redraw()` or `refresh()` function – Kevin Mee Dec 15 '15 at 16:20
  • 1
    Thank you! By searching for the redraw() function I found out about the revalidate() and repaint() functions :) – kajdehoop Dec 15 '15 at 17:21

2 Answers2

2

I've found out I needed to use revalidate() and repaint() to redraw the frame. Source: Java Swing revalidate() vs repaint()

Community
  • 1
  • 1
kajdehoop
  • 517
  • 8
  • 22
0

This works:

panel.invalidate();
//call the method u desire
panel.revalidate();
krpa
  • 84
  • 1
  • 13