Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java App - Add, Delete, Reorder elements of the JButtons

I'm developping a Java application.

I created this interface with MockupScreens. Please look at these pictures.

enter image description here

enter image description here

At first time, there's only one element, the user have to enter informations (title and description) then he starts adding elements as he needs. He can edit elemnt infomrations at any time. He can too delete or change the order of this elements ...

How can I do to create something like the pictures up?????

Thanks in advance. Best regards,

Ali.

I know these parts in Java Swing. My problem is how to insert this block of buttons dynamically. enter image description here

I get an idea, I must put JButtons on a JPanel then manipulate the JPanel by adding, removing and reodering... So a Grid Layout will be efficient to add each panel after each one, but thinking on reordering the order will be so hard ...

Any suggestions please. :)


After searching, I get an idea:

enter image description here

Let us put these JButtons in a JPanel called btnsUnit, then manipulate it by adding, removing and reodering... So a GridLayout will be efficient to add each JPanel after each one ..

Thats why I created a new JPanel which will contain an unknown number of ListbtnsUnit JPanel, I fixed 10 as the max number.

I'm just doing these steps when you reply me. I didn't arrived to add btnsUnit JPanel in ListbtnsUnit JPanel.

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;


public class setupDeviceList extends JFrame {

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                setupDeviceList frame = new setupDeviceList();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public setupDeviceList() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 742, 335);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    final JPanel ListbtnsUnit = new JPanel();
    ListbtnsUnit.setBackground(Color.RED);
    ListbtnsUnit.setBounds(55, 56, 243, 191);
    contentPane.add(ListbtnsUnit);
    ListbtnsUnit.setLayout(new GridLayout(10, 0));

    final JButton btnAdd = new JButton("Add");
    btnAdd.setBounds(161, 11, 56, 23);
    btnAdd.setVisible(true);

    btnAdd.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            final JPanel btnsUnit = new JPanel();
            btnsUnit.setBounds(343, 71, 243, 147);
            contentPane.add(btnsUnit);
            btnsUnit.setBackground(Color.ORANGE);
            btnsUnit.setLayout(null);
            btnsUnit.add(btnAdd);

            ListbtnsUnit.add(btnsUnit);
            ListbtnsUnit.revalidate();
            ListbtnsUnit.repaint();
        }
    });
}
}

enter image description here

Please can you help me in this code. I need just the first push to go on.

like image 764
Ali Ben Messaoud Avatar asked Mar 04 '11 23:03

Ali Ben Messaoud


3 Answers

but thinking on reordering the order will be so hard ...

In your ActionListener for the button:

  1. use getSource() method to get the button that was clicked
  2. use the getParent() method of the button to find the panel the button belongs to
  3. use the getParent() method of the buttons panel to find its parent panel
  4. use the getComponents method to get an Array of all the button panels. Then loop through the Array to find the index of the panel you are search for.
  5. once you find the index you increase of decrease the index, then you can add the panel back to its parent panel at the new index.
  6. then you revalidate() and repaint() the parent panel to display the panel in its new location.
like image 87
camickr Avatar answered Nov 18 '22 04:11

camickr


Nice images :-)

You need these parts, if you want to do this in Swing:

  • javax.swing.JButton (the several buttons)
  • javax.swing.JTextField (the one line text input component)
  • javax.swing.JTextArea (the multi-line text input component)
  • javax.swing.JLabel (labels for your input fields)
  • javax.swing.JPanel (a container to group several components together, like your lines of buttons, or the editing components at the right)
  • javax.swing.JFrame (the window for all together)
  • javax.swing.Action (actions to be performed when a button is clicked - they also can contain an icon and/or text for the button)

Look at these, start to code, and then come back if you have concrete problems. Good luck!

like image 43
Paŭlo Ebermann Avatar answered Nov 18 '22 03:11

Paŭlo Ebermann


I think you are using a wrong idiom here. What you are trying to accomplish is usually done using JList (scrollable list of items). This way you need just one set of operations such as reorder, add and delete, next to the JList. Operations should operate on selected item in JList (otherwise they should be disabled)

Your interface may look sort of like

enter image description here

The idiom you're currently using is mostly done on web pages. Desktop UIs, (especially Swing) are much richer.

You can find more about how to use JList at http://download.oracle.com/javase/tutorial/uiswing/components/list.html

like image 33
Eugene Ryzhikov Avatar answered Nov 18 '22 03:11

Eugene Ryzhikov