1

I've only been studying Java for a short while, and for some reason this small thing has me stuck on my current project. I have the jframe complete with a change/delete button, but I need to figure out how to make them work. I need it to actually change it in the coding, and delete it or change it permanently in the actual code.

package admin;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import java.awt.event.*;

public class Project3 extends JFrame {

    private JPanel contentPane;
    private JTable table_1;
    private JScrollPane scrollPane;
    String[] columnNames = {"Restaurant", "Dish", "Type", "Price", "Rating"};
    Object[][] data = {
        {"Nemo", "Vesuvio", "Pizza", new String("65kr"), new Integer(7)},
        {"John", "Doe", "Rowing", new Integer(3), new Boolean(true)},
        {"Sue", "Black", "Knitting", new Integer(2), new Boolean(false)},
        {"Jane", "White", "Speed reading", new Integer(20),
            new Boolean(true)},
        {"Joe", "Brown", "Pool", new Integer(10), new Boolean(false)}};

    /**
    * Launch the application.
    */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    Project3 frame = new Project3();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
    * Create the frame.
    */
    public Project3() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 800, 600);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.CENTER);
        panel.setLayout(null);
        scrollPane = new JScrollPane();
        scrollPane.setBounds(139, 162, 469, 420);
        panel.add(scrollPane);
        table_1 = new JTable(data, columnNames);
        scrollPane.setViewportView(table_1);
        table_1.setFillsViewportHeight(true);
        table_1.setModel(new DefaultTableModel(new Object[][]{
                {"Nemo", "blah", "Pizza", "65kr", new Integer(7)},
                {"Nemo", "blah", "Rowing", "1 000 000kr", Boolean.TRUE},
                {"Sue", "blah", "Knitting", new Integer(2), Boolean.FALSE},
                {"Jane", "blah", "Speed reading", new Integer(20), Boolean.TRUE},
                {"Joe", "Brown", "Pool", new Integer(10), Boolean.FALSE},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},},
            new String[]{
                "Restaurant", "Dish", "Type", "Price", "Rating"}));
        JButton btnChangeadd = new JButton("Change/Add");
        btnChangeadd.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // CALL CHANGE METHOD HERE
            }
        });
        btnChangeadd.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
        btnChangeadd.setBounds(143, 104, 175, 42);
        panel.add(btnChangeadd);
        JLabel lblNewLabel = new JLabel("Welcome RestaurantID");
        lblNewLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 24));
        lblNewLabel.setBounds(264, 56, 260, 36);
        panel.add(lblNewLabel);
        JButton button = new JButton("+");
        button.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // CALL SOMETHING HERE IF YOU WANT
            }
        });
        button.setBounds(66, 248, 40, 29);
        panel.add(button);
        JButton button_1 = new JButton("-");
        button_1.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
            }
        });
        button_1.setBounds(66, 289, 40, 29);
        panel.add(button_1);
        JButton btnDelete = new JButton("Delete");
        btnDelete.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                //DELETE METHOD
            }
        });
        btnDelete.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
        btnDelete.setBounds(344, 104, 191, 42);
        panel.add(btnDelete);
        JButton btnLogOut = new JButton("Log out");
        btnLogOut.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
            }
        });
        btnLogOut.setBounds(667, 6, 117, 29);
        panel.add(btnLogOut);
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Cameronaut
  • 25
  • 2
  • 7
  • 1) `button_1.setBounds(66, 289, 40, 29);` Use layouts. 2) Delete what? Change what? For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Nov 24 '12 at 12:56
  • You wouldn't be developing in Notepad, would you? Cause that would explain the formatting. – Marko Topolnik Nov 24 '12 at 13:15
  • There's a complete example [here](http://stackoverflow.com/a/11241218/230513) that uses `removeRow()` in a `MouseListener`. – trashgod Nov 24 '12 at 14:13

2 Answers2

2

I think you're looking for an ActionListener instead of a MouseListener. You can add an actionlistener to a JButton in the same way that you add the MouseListener.

button.addActionListener(new ActionListener(){  
    public void actionPerformed(ActionEvent e){  
        //code you want to run when the button gets pressed  
    }  
 }  

The actionPerformed method will get run when the button is clicked. Here is a good tutorial for writing actionlisteners: http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

In that tutorial they do things a little different. They have a class that extends ActionListener and implements the actionPerformed method. If it is done this way then you add the whole class as the actionlistener to the button. So if your button was getting created inside the class that extends the actionlistener you would do:
button.addActionListener(this);
This allows you to have one actionPerformed method for multiple buttons. To tell which button gets clicked within the actionPerformed method you would call the getSource method on the ActionEvent object like so:
if(e.getSource() == button)
If you need more explanation on having the class extend ActionListener let me know.

Edit:
Definitely misunderstood what you were asking. I still recommend the actionlisteners over the way you are handling button presses with the mouselistener though. If you clarify on how you want to change the data I might be able to help.

Geren White
  • 270
  • 1
  • 2
  • 11
0

If you want to delete the btn which was clicked then do:

panel.remove();
panel.validate();
panel.repaint();
Moreover, you can access clicked source via e.getSource() inside action performed method.
sol4me
  • 15,233
  • 5
  • 34
  • 34