0

I have a Swing Form (Java). In this form I have field, for example Name1. I initialize it so:

private JTextField Name1;

In this code i'm adding JTextField Name1 into my Form:

tabbedPane.addTab("T6", null, panel, "T6");
panel.setLayout(null);
Name1.setBounds(73, 11, 674, 20);
panel.add(Name1);

Additionaly I have Button1 on my form. The event in this button is changing the value of Name1. Its work normaly.

Moreover I have a Button 2 that hiding the Tab with Name1:

tabbedPane.remove(1);
tabbedPane.repaint();
tabbedPane.revalidate();
frame.repaint();
frame.revalidate();

(And, of course, I turn on my tabpane again after this)

After all that, by the pressing the Button 4 I want to change the vlue of Name1 to some text.

But it doesn't work!!!!!! SetTex doesnt work. The field is empty.

So, if I change the Name1 declaration from

private JTextField Name1;

to

static JTextField Name1;

Yes, it works. BUT! Then I can't change the value of Name1 by using

Name1.Settext("Example");

What i have to do to make Name1 available after Button 4 pressed and changable ????

The all code is:

public class GUI {


    public JTextField Name_textField;



    public static void main(String[] args) {

        DB_Initialize();

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GUI window = new GUI();
                    window.frame.setVisible(true);
                    window.frame.setResizable(false);
                    FirstConnect FC = window.new FirstConnect();
                    ConnectStatus = true;
                    FC.FirstEntry();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    public GUI() {

        CurrentEnty_textField = new JTextField();
        Name_textField = new JTextField();

        EntriesCountlbl = new JLabel("New label");
        initialize();
        EntriesCountlbl.setText(Integer.toString(EC1));

        if (LoginStatus == true) {

            System.out.println("LoginStatus == true");
            AdminPartOn();
            btnNewButton.setEnabled(false);

        } else {

            btnNewButton_3.setEnabled(false);

            tabbedPane.setEnabledAt(1, false);
            // UnableForm();
            AdminPartOff();

        }

    }



    public static void DB_Initialize() {

        conn3 = con3.DoConnect(); 

        int ID;

        ArrayList<String> list = new ArrayList<String>();
        String l;
        String p;

        list = con3.LoginFileRead();

        if (list.size() == 2) {

            l = list.get(0);
            p = list.get(1);

            System.out.println("Логин из файла = " + l);
            System.out.println("Пароль из файла = " + p);

            ID = con3.CRMUserRequest(conn3, l, p);
            AdminPanelData = con3.CRMUserFullData(conn3, l, p);

            if (ID != 0) {

                System.out.println("ID Юзера = " + ID);
                LoginStatus = true;

            }
        }

        EC1 = con3.CRMQuery_EntriesCount(conn3); // запрашиваем кол-во записей
        StatusTableEntriesCount = con3.CRMQueryStatus_EntriesCount(conn3);

        StatusTableFromCount = con3.CRMQueryFRom_EntriesCount(conn3);

        System.out.println("Entries count(Из модуля GYU): " + EC1);

        if (EC1 > 0) {
            CurrentEntry = 1;
            System.out.println("Все ОК, текущая запись - " + CurrentEntry);

        } else {

            System.out.println("Выскакивает обработчик ошибок");

        }

        con3.Ini();
        con3.CRMQuery2(conn3, EC1 + 1);

        StatusColumn = con3.CRMQueryStatus(conn3, StatusTableEntriesCount);
        FromColumn = con3.CRMQueryFrom(conn3, StatusTableFromCount);

    }

    public class FirstConnect {
        public void FirstEntry() {

            int CurStatus = F.GetStatus(CurrentEntry - 1);

            int CurFrom = F.GetFrom(CurrentEntry - 1);

            if (LoginStatus != false) {

                Name_textField.setText(F.GetName(CurrentEntry - 1));

            } else {

                Name_textField.setText("-");

            }

        }
    }


    private void initialize() {

        frame = new JFrame();
        panel = new JPanel();

        frame.setBounds(100, 100, 816, 649);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        tabbedPane.setBounds(10, 250, 780, 361);
        frame.getContentPane().add(tabbedPane);

        JPanel panel_1 = new JPanel();
        tabbedPane.addTab("\u0412\u0445\u043E\u0434", null, panel_1, null);

        btnNewButton = new JButton("\u0412\u0445\u043E\u0434");
        btnNewButton.setBounds(263, 285, 226, 37);
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                int ID;

                ID = con3.CRMUserRequest(conn3, LoginField.getText(), PasswordField.getText());
                if (ID == 0) {


                } else {


                    MainTab();
                    FirstEntry3();

                }

            }
        });
        panel_1.setLayout(null);
        panel_1.add(btnNewButton);


tabbedPane.addTab("\u041A\u043B\u0438\u0435\u043D\u0442", null, panel,
                "\u041A\u043E\u043D\u0442\u0430\u043A\u0442\u043D\u044B\u0435 \u0434\u0430\u043D\u043D\u044B\u0435 \u043A\u043B\u0438\u0435\u043D\u0442\u0430");
        panel.setLayout(null);

        // Name_textField = new JTextField();
        Name_textField.setBounds(73, 11, 674, 20);
        panel.add(Name_textField);


        Name_textField.setHorizontalAlignment(SwingConstants.CENTER);
        Name_textField.setColumns(10);


        NextEntryButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                if (NextEntryButton.isEnabled() != false) {



                    if (CurrentEntry < EC1) {


                        CurrentEntry = CurrentEntry + 1;


                        int CurStatus = F.GetStatus(CurrentEntry - 1);

                        int CurFrom = F.GetFrom(CurrentEntry - 1);


                        Name_textField.setText(F.GetName(CurrentEntry - 1));



                    } else {

                    }


                }

            }
        });

    }

    public void MainTab() {

        tabbedPane.addTab("1", null, panel,
                "1");
        tabbedPane.setEnabledAt(1, true);
        panel.setLayout(null);
    }

    public void FirstEntry3() {
        Name_textField.setText(F.GetName(CurrentEntry - 1));

    }
}
Anton Romanov
  • 315
  • 2
  • 5
  • 12
  • 1
    Can you please share the code here – Rishabh Kumar Sep 27 '16 at 09:04
  • All code? But its too big !!!! – Anton Romanov Sep 27 '16 at 09:06
  • The event code where you tried to change the text – Rishabh Kumar Sep 27 '16 at 09:07
  • 1
    It's bad practice to define UI widgets as static. Try to learn and understand the basics of OOP and Design Patterns. – Sergiy Medvynskyy Sep 27 '16 at 09:18
  • 3
    And if you want to get a useful answer, you should create a [SSCCE](http://sscce.org) – Sergiy Medvynskyy Sep 27 '16 at 09:20
  • I have put the all code above. But event code is just: ` Name_textField.setText("Some text"); ` Its all – Anton Romanov Sep 27 '16 at 09:37
  • Ok, if its the bad practice, How I can update value of this field after i kill the tabpane and create it again? – Anton Romanov Sep 27 '16 at 09:46
  • 2
    The code above is not a [SSCCE](http://sscce.org). To retain field value use either the [Memento-Pattern](https://en.wikipedia.org/wiki/Memento_pattern) or [Model-View-Controller](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) pattern – Sergiy Medvynskyy Sep 27 '16 at 09:59
  • 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) Agree with comments warning against using `static` for GUI components, causes more problems than solves. – Andrew Thompson Sep 27 '16 at 13:11
  • 4) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Sep 27 '16 at 13:12

0 Answers0