How to store a color of a JButton in a BD SQLite

1

My program can dynamically create JButtons, Text is added to it and the value they will return to me when I click:

String name = nameField.getText();
    String identification = idField.getText();
    Person person = new Person(name, identification);
    insertarVendedores(name, identification);

    JButton personButton = new JButton(new PersonAction(person));
    pnlVen.add(personButton);
    pnlVen.revalidate();
    pnlVen.repaint();
    nameField.setText("");
    idField.setText("");
    CargarTablaVendedores("");

Afterwards, it loads them from a SQLite database:

try{
        Connection miConexion = DriverManager.getConnection("jdbc:sqlite:vendedores2Claro.db");
    Statement miStatement = miConexion.createStatement();
    ResultSet miResultSet = miStatement.executeQuery("SELECT id, name, identification FROM vendedores2Claro");

    while(miResultSet.next()){
            pnlVen.add(new JButton(new PersonAction(new Person(miResultSet.getString("name"), miResultSet.getString("identification")))));
            pnlVen.revalidate();
            pnlVen.repaint();
        }
    }catch(Exception e){
        System.out.println(e);
    }

But the buttons become indistinguishable because they are all of a color, and I need help to be told how I can do so that at the time of filling out the form for the user to create the JButton select from a box combo the color of that new JButton and that when loaded it also loads with that color. A thousand apologies if I give too many turns in my question, And I thank you for your attention.

    
asked by Extibax 27.08.2018 в 02:47
source

1 answer

0

Hello I assemble this simple program that generates buttons and assigned the color based on a comboBox, I hope you an idea to implement the solution.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {

        Window window = new Window();
        window.start();

    }
}


class Window extends JFrame implements ActionListener {


    final String[] COLORS = {"Azul", "Rojo", "Verde"};

    private JButton generateButton = new JButton("Generar botones");
    private JComboBox colorBox = new JComboBox(COLORS);
    private List<JButton> buttons = new ArrayList<>();


    public Window() {

        setBounds(0, 0, 300, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        generateButton.addActionListener(this);
        add(generateButton);
        add(colorBox);
    }

    public void start() {
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {


        //Create a new button with the selected color
        JButton newButton = new JButton("Button" + (buttons.size() + 1));
        newButton.setBackground(getColor(colorBox.getSelectedItem() + ""));
        buttons.add(newButton);
        add(newButton);

        //Update change on the JFrame
        revalidate();
        repaint();
    }

    private Color getColor(final String stringColor) {
        switch (stringColor) {
            case "Azul":
                return Color.blue;
            case "Rojo":
                return Color.red;
            case "Verde":
                return Color.green;
            default:
                return Color.blue;
        }
    }
}
    
answered by 27.08.2018 / 04:12
source