ArrayList<JTextField> arrayFinal; /*
LIKE global variable
Have initialize part, for alloc memory for arayFinal.
*/
也可以从JTextFields
添加到面板arrayFinal
。现在在此功能中,无法从面板中删除元素......
public void arrayFinalErase(JPanel panel){
for ( JTextField text : arrayFinal){
panel.remove(text);
}
panel.revalidate();
panel.repaint();
/*
* Also tried
* panel.remove(arrayFinal.get(......));
* but also doesn't work
*/
}
编辑,插入完整代码:
JButton dodajButton = new JButton("Dodaj");
dodajButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("--------\n TEXT: " + arrayFinal.get(0).getText() );
panel.remove(arrayFinal.get(0));
panel.revalidate();
panel.repaint();
}
});
dodajButton.setBounds(851, 35, 89, 23);
panel.add(dodajButton);
JButton sacuvajButton = new JButton("Sacuvaj");
sacuvajButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
sacuvajArray();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
sacuvajButton.setBounds(851, 7, 89, 23);
panel.add(sacuvajButton);
scrollPane.setBounds(10, 11, 969, 400 );
scrollPane.setBackground(Color.green);
contentPane.add(scrollPane);
countRows = count(ID);
if ( countRows!= 0){
setBeginning();
for( ArrayList<JTextField> tmp : list){
textDrawWithArray(tmp, panel);
}
}
else{
System.out.println("dadada");
textDrawDefault(panel);
finalDraw(panel);
}
}
public int count(int ID) throws SQLException{
String sql = "SELECT COUNT(ID) AS total FROM tabelle_1 WHERE ID = " + ID + ";";
ResultSet rs = stmt.executeQuery(sql);
return rs.getInt(1);
}
public void setBeginning() throws SQLException{
String sql = "SELECT * FROM tabelle_1 WHERE ID =" + ID + ";";
ResultSet rs = stmt.executeQuery(sql);
if ( rs.next()){
ArrayList<JTextField> tmp;
do{
tmp = arrayAllocJTextField();
tmp.add(textAlloc(rs.getString("datum")));
tmp.add(textAlloc(Double.toString(rs.getDouble("cena"))));
tmp.add(textAlloc(Double.toString(rs.getDouble("brojLekcija"))));
tmp.add(textAlloc(Double.toString(rs.getDouble("total"))));
tmp.add(textAlloc(rs.getString("opisRadnje")));
tmp.add(textAlloc(rs.getString("cena_1")));
tmp.add(textAlloc(rs.getString("brojLekcija_1")));
tmp.add(textAlloc(rs.getString("total_1")));
list.add(tmp);
}while(rs.next());
}
rs.close();
}
public ArrayList<String> arrayAllocString(){
return new ArrayList<String>();
}
public ArrayList<JTextField> arrayAllocJTextField(){
return new ArrayList<JTextField>();
}
public JTextField textAlloc(String text){
return new JTextField(text);
}
public void textDrawWithArray(ArrayList<JTextField> array, JPanel panel){
JTextField textTemp;
int i = 1;
for( JTextField text : array){
if ( i == 1){
textTemp = textAlloc(Integer.toString(redniBroj));
textTemp.setColumns(10);
textTemp.setBounds(x_pos[0], y_pos, x_duz[0], y_duz);
textTemp.setEditable(false);
panel.add(textTemp);
redniBroj++;
}
textTemp = textAlloc(text.getText());
textTemp.setColumns(10);
textTemp.setBounds(x_pos[i], y_pos, x_duz[i], y_duz);
panel.add(textTemp);
i++;
}
panel.revalidate();
panel.repaint();
y_pos += 30;
}
public void textDrawDefault(JPanel panel){
JTextField textTemp;
ArrayList<JTextField> tmp = arrayAllocJTextField();
for ( int i = 0; i < 9; i++){
if ( i == 0){
textTemp = textAlloc(Integer.toString(redniBroj));
textTemp.setColumns(10);
textTemp.setBounds(x_pos[i], y_pos, x_duz[i], y_duz);
textTemp.setEditable(false);
panel.add(textTemp);
redniBroj++;
continue;
}else if ( i == 4 || i == 8){
textTemp = textAlloc("");
textTemp.setEditable(false);
textTemp.setColumns(10);
textTemp.setBounds(x_pos[i], y_pos, x_duz[i], y_duz);
tmp.add(textTemp);
if ( i == 4){
textTemp.addFocusListener(new FocusListener(){
@Override
public void focusGained(FocusEvent arg0) {
if ( !tmp.get(1).getText().equals("") && !tmp.get(2).getText().equals("")){
double a = Double.parseDouble(tmp.get(1).getText());
double b = Double.parseDouble(tmp.get(2).getText());
tmp.get(3).setText("" + (a*b));
}
}
@Override
public void focusLost(FocusEvent arg0) {
// TODO Auto-generated method stub
}
});
}else{
textTemp.addFocusListener(new FocusListener(){
@Override
public void focusGained(FocusEvent arg0) {
if ( !tmp.get(5).getText().equals("") && !tmp.get(6).getText().equals("")){
double a = Double.parseDouble(tmp.get(5).getText());
double b = Double.parseDouble(tmp.get(6).getText());
tmp.get(7).setText("" + (a*b));
}
}
@Override
public void focusLost(FocusEvent arg0) {
// TODO Auto-generated method stub
}
});
}
panel.add(textTemp);
continue;
}
textTemp = textAlloc("");
textTemp.setColumns(10);
textTemp.setBounds(x_pos[i], y_pos, x_duz[i], y_duz);
textTemp.setText("");
panel.add(textTemp);
tmp.add(textTemp);
}
list.add(tmp);
panel.revalidate();
panel.repaint();
y_pos += 30;
System.out.println("cechk default");
}
public void sacuvajArray() throws SQLException{
String[] textBase = new String[8];
String sqlDelete = "DELETE FROM tabelle_1 WHERE ID = "+ ID + ";";
stmt.executeUpdate(sqlDelete);
String sqlInsert;
int i = 0;
for(ArrayList<JTextField> arrayChild : list){
i= 0;
for( JTextField text : arrayChild){
textBase[i++] = text.getText();
}
sqlInsert = "INSERT INTO tabelle_1 (ID, datum, cena, brojLekcija, total, opisRadnje, cena_1, brojLekcija_1, total_1) VALUES("
+ ID + ","
+ "'" + textBase[0] + "',"
+ "'" + textBase[1] + "',"
+ "'" + textBase[2] + "',"
+ "'" + textBase[3] + "',"
+ "'" + textBase[4] + "',"
+ "'" + textBase[5] + "',"
+ "'" + textBase[6] + "',"
+ "'" + textBase[7] + "');";
stmt.executeUpdate(sqlInsert);
}
}
public void finalDraw(JPanel panel){
double brojLekcija=0.0;
double total= 0.0;
double cena_1=0.0;
double brojLekcija_1=0.0;
double total_1= 0.0;
int i = 0, y=0;
for( ArrayList<JTextField> tmp : list){
i=0;
for( JTextField textField : tmp){
if ( i == 0 || i == 1 || i == 4){
i++;
continue;
}
String text = textField.getText();
if ( text.equals("")){
i++;
continue;
}
switch(i){
case 2:
brojLekcija += Double.parseDouble(text);
break;
case 3:
total += Double.parseDouble(text);
break;
case 5:
cena_1 += Double.parseDouble(text);
break;
case 6:
brojLekcija_1 += Double.parseDouble(text);
break;
case 7:
total_1 += Double.parseDouble(text);
break;
default:
System.out.println("ERROR!!");
break;
}
i++;
}
System.out.println("-----------------");
}
arrayFinal.add(textAlloc("Total"));
arrayFinal.add(textAlloc(""));
arrayFinal.add(textAlloc(Double.toString(brojLekcija)));
arrayFinal.add(textAlloc(Double.toString(total)));
arrayFinal.add(textAlloc(""));
arrayFinal.add(textAlloc(Double.toString(cena_1)));
arrayFinal.add(textAlloc(Double.toString(brojLekcija_1)));
arrayFinal.add(textAlloc(Double.toString(total_1)));
textDrawWithArray(arrayFinal, panel);
System.out.println("OKE JE");
}
public void arrayFinalErase(JPanel panel){
for ( JTextField text : arrayFinal){
panel.remove(text);
}
panel.revalidate();
panel.repaint();
/*
* Also tried
* panel.remove(arrayFinal.get(......));
* but also doesn't work
*/
y_pos -= 30;
}
}
答案 0 :(得分:0)
它适用于我:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class FrameExample extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private List<JTextField> textfieldGroup;
public FrameExample() {
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
textfieldGroup = new ArrayList<>();
init();
pack();
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
arrayFinalErase();
}
private void init() {
for (int index = 0; index < 10; index++) {
textfieldGroup.add(new JTextField(index));
add(textfieldGroup.get(index));
}
JButton removeAllTextfields = new JButton("remove");
add(removeAllTextfields);
removeAllTextfields.addActionListener(this);
}
private void arrayFinalErase() {
for (JTextField text : textfieldGroup) {
remove(text);
}
revalidate();
repaint();
}
public static void main(String[] args) {
new FrameExample();
}
}
TextFields列表可能是空的吗?