1

I want to make an Java-Aplication with Swing Forms. But I have a Problem. I can see all Textboxes, all Buttons an Labels. But there's no way to see the JComboBox. Same with JCheckBox. Where is the Problem?

import javax.swing.*;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.text.DefaultCaret;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.font.TextAttribute;
import java.io.File;
import java.util.Map;
import java.util.Vector;


public class Testtool{
    private JFrame frame;
    private JTextArea taAusgabe;
    private JFileChooser file1;
    private JFileChooser file2;
    private JComboBox cbFormate;
    private DefaultComboBoxModel cbmFormate;

    public JButton btn1;
    public JButton btn2;
    public JButton btn3;
    public JButton btnClear;
    public JLabel laLoading;

    private boolean debug = false;

    private static Testtool window;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    window = new Testtool();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Testtool() throws InterruptedException {
        Vector<String> formate = new Vector<String>();
        formate.add("Format");
        formate.add("1");
        formate.add("2");

        frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.setBounds(100, 100, 720, 510);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setResizable(false);            

        Container container = frame.getContentPane();

        Font header = new Font("Serif", Font.BOLD, 22);
        Map attributes = header.getAttributes();
        attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);

        Font normal = new Font("Serif", Font.BOLD, 16);

        JLabel laHeader = new JLabel();
        laHeader.setHorizontalAlignment(SwingConstants.CENTER);
        laHeader.setText("Testtool für Transformationsskripte");
        laHeader.setFont(header.deriveFont(attributes));
        laHeader.setSize(740, 40);
        laHeader.setLocation(0, 10);
        container.add(laHeader);

        JLabel laFormat = new JLabel();
        laFormat.setText("Format:");
        laFormat.setFont(normal);
        laFormat.setSize(160, 20);
        laFormat.setLocation(10, 80);
        container.add(laFormat);

        cbFormate = new JComboBox();
        cbmFormate = new DefaultComboBoxModel(formate);
        cbFormate.setModel(cbmFormate);
        cbFormate.setLocation(60, 80);
        container.add(cbFormate);

        JLabel la1= new JLabel();
        la1.setText("Messwertdatei:");
        la1.setFont(normal);
        la1.setSize(160, 20);
        la1.setLocation(10, 110);
        container.add(la1);


        final JLabel la2 = new JLabel();
        la2 .setSize(410, 20);
        la2 .setLocation(300, 110);
        container.add(la2 );

        btn1= new JButton();
        btn1.setText("Auswählen");
        btn1.setSize(100, 20);
        btn1.setLocation(180, 110);
        btn1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                file1= new JFileChooser();
                int returnVal1 = file1.showOpenDialog(null);
                if (returnVal1 == JFileChooser.APPROVE_OPTION) {
                    File f1= file1.getSelectedFile();
                    la2 .setText(f1.getAbsolutePath());
                    addLogLine(f1.getAbsolutePath() + " ausgewählt", false);
                }
            }
        });
        container.add(btn1);


        final JLabel la3= new JLabel();
        la3.setText("Transformationsskript:");
        la3.setFont(normal);
        la3.setSize(160, 20);
        la3.setLocation(10, 140);
        container.add(la3);

        final JLabel la4= new JLabel();
        la4.setSize(410, 20);
        la4.setLocation(300, 140);
        container.add(la4);

        btn2 = new JButton();
        btn2 .setText("Auswählen");
        btn2 .setSize(100, 20);
        btn2 .setLocation(180, 140);
        btn2 .addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                file2= new JFileChooser();
                int returnVal2 = file2.showOpenDialog(null);
                if (returnVal2 == JFileChooser.APPROVE_OPTION) {
                    File f2= f2.getSelectedFile();
                    la4.setText(f2.getAbsolutePath());
                    addLogLine(f2.getAbsolutePath() + " ausgewählt", false);
                }
            }
        });
        container.add(btn2 );

        btn3 = new JButton();
        btn3.setText("go");
        btn3.setSize(270, 20);
        btn3.setLocation(10, 170);
        btn3.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                start();
            }

            public void mouseClicked(MouseEvent arg0) {

            }
        });
        container.add(btn3 );

        ImageIcon loading = new ImageIcon(getClass().getClassLoader().getResource("ajax-loader.gif"));
        laLoading = new JLabel();
        laLoading.setVisible(false);
        laLoading.setIcon(loading);
        laLoading.setSize(20, 20);
        laLoading.setLocation(300, 170);
        container.add(laLoading);

        JLabel laAusgabe = new JLabel();
        laAusgabe.setText("Ausgabe:");
        laAusgabe.setFont(normal);
        laAusgabe.setSize(100, 20);
        laAusgabe.setLocation(10, 200);
        container.add(laAusgabe);

        btnClear = new JButton();
        btnClear.setText("Ausgabe löschen");
        btnClear.setSize(160, 20);
        btnClear.setLocation(120, 200);
        btnClear.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                taAusgabe.setText("");
            }
        });
        container.add(btnClear);

        final JButton btnDebug = new JButton();
        btnDebug.setText("Debugging: AUS");
        btnDebug.setSize(160, 20);
        btnDebug.setLocation(540, 200);
        btnDebug.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {

            }
        });
        container.add(btnDebug);

        taAusgabe = new JTextArea();
        DefaultCaret caret = (DefaultCaret)taAusgabe.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        taAusgabe.setSize(690, 250);
        taAusgabe.setBackground(container.getBackground());
        taAusgabe.setEditable(false);

        JScrollPane scroll = new JScrollPane(taAusgabe, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll.setLocation(10, 220);
        scroll.setSize(690, 250);
        scroll.setAutoscrolls(true);
            container.add(scroll);       

container.setLayout(null);  


    }

    public void addLogLine(String logline, boolean debug){
        if (this.debug){
            taAusgabe.append(logline + '\n');
        }
        else{
            if(!debug){
                taAusgabe.append(logline + '\n');
            }
        }
    }

    public void stop(){
        btnClear.setEnabled(true);
        btn1.setEnabled(true);
        btn2 .setEnabled(true);
        btn3.setEnabled(true);
        laLoading.setVisible(false);
    }

    public void start(){
        btnClear.setEnabled(false);
        btn1.setEnabled(false);
        btn2 .setEnabled(false);
        btn3.setEnabled(false);
        laLoading.setVisible(true);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
sagschni
  • 89
  • 1
  • 13

1 Answers1

2

Where is the Problem?

container.setLayout(null); is the main soure of your problem and will be a continuous pain in the side until you stop using it.

You don't control the metrics which are used to determine the required space a component requires on different platforms and OSs. Pixel perfect layouts are an illusion in modern UI development. Even between the same OS, you will end up with enough differences in Font types and rendering pipelines to render you interface useless.

I want to make an Java-Aplication with Swing Forms

Then it's time to learn how to use layout managers, take a look at Laying Out Components Within a Container for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366