2

This is kinda a silly question, but I'm beggining with java swing and have been doing kinda great, but I really can't make a JTable appear in my form, so far a blank square appears but no columns at all, here's a snippet:

DefaultTableModel modelo = new DefaultTableModel();
modelo.setColumnCount(1);
tblTrans = new JTable(modelo);
modelo.addColumn("Prueba");
tblTrans.revalidate();
tblTrans.setBounds(316, 47, 248, 243);
contentPane.add(tblTrans);

Here's the complete code in case you need it:

package mx.adk.grafos;

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

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JList;
import javax.swing.JTable;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Set;
import java.util.TreeSet;

public class GUI extends JFrame implements ActionListener {

    private JPanel contentPane;
    private JTextField txtEstados;
    private JTextField txtLenguaje;
    private JTextField txtFinal;
    private JTable tblTrans;
    private JList lstEstados;
    private JList lstLenguaje;

    DefaultListModel listaEstados;
    TreeSet<String> setEstados = new TreeSet<String>();

    DefaultListModel listaLenguaje;
    TreeSet<Character> setLenguaje = new TreeSet<Character>();

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

    public void actionPerformed(ActionEvent e) {
        String comando = e.getActionCommand();
        if(comando.equalsIgnoreCase("btnAEstados")){
            String tmp = txtEstados.getText();
            if(setEstados.add(tmp)){
                listaEstados.addElement(tmp);
            }
            return;
        }
        if(comando.equalsIgnoreCase("btneestados")){
            String tmp = (String) lstEstados.getSelectedValue();
            setEstados.remove(tmp);
            listaEstados.removeElement(tmp);
        }
        if(comando.equalsIgnoreCase("btnALenguaje")){
            char tmp = txtLenguaje.getText().charAt(0);
            if(setLenguaje.add(tmp)){
                listaLenguaje.addElement(tmp);
            }
            return;
        }
        if(comando.equalsIgnoreCase("btnelenguaje")){
            char tmp = (char) lstLenguaje.getSelectedValue().toString().charAt(0);
            setLenguaje.remove(tmp);
            listaLenguaje.removeElement(tmp);
        }
    }

    public GUI() {
        setTitle("Automata finito no deterministico");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 599, 368);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        txtEstados = new JTextField();
        txtEstados.setBounds(26, 82, 72, 28);
        contentPane.add(txtEstados);
        txtEstados.setColumns(10);

        JButton btnAEstados = new JButton(">>");
        btnAEstados.setBounds(103, 69, 51, 29);
        contentPane.add(btnAEstados);
        btnAEstados.addActionListener(this);
        btnAEstados.setActionCommand("btnAEstados");

        listaEstados = new DefaultListModel();
        lstEstados = new JList(listaEstados);
        lstEstados.setBounds(154, 46, 105, 109);
        contentPane.add(lstEstados);
        lstEstados.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        JLabel lblEstados = new JLabel("Estados");
        lblEstados.setBounds(83, 19, 59, 16);
        contentPane.add(lblEstados);

        JButton btnEEstados = new JButton("<<");
        btnEEstados.setBounds(103, 96, 51, 29);
        contentPane.add(btnEEstados);
        btnEEstados.addActionListener(this);
        btnEEstados.setActionCommand("btnEEstados");

        JButton btnELenguaje = new JButton("<<");
        btnELenguaje.setBounds(103, 231, 51, 29);
        contentPane.add(btnELenguaje);
        btnELenguaje.addActionListener(this);
        btnELenguaje.setActionCommand("btnELenguaje");

        JButton btnALenguaje = new JButton(">>");
        btnALenguaje.setBounds(103, 204, 51, 29);
        contentPane.add(btnALenguaje);
        btnALenguaje.addActionListener(this);
        btnALenguaje.setActionCommand("btnALenguaje");

        txtLenguaje = new JTextField();
        txtLenguaje.setColumns(10);
        txtLenguaje.setBounds(26, 217, 72, 28);
        contentPane.add(txtLenguaje);

        listaLenguaje = new DefaultListModel();
        lstLenguaje = new JList(listaLenguaje);
        lstLenguaje.setBounds(154, 181, 105, 109);
        contentPane.add(lstLenguaje);
        lstLenguaje.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        JLabel lblLenguaje = new JLabel("Lenguaje");
        lblLenguaje.setBounds(95, 161, 59, 16);
        contentPane.add(lblLenguaje);

        txtFinal = new JTextField();
        txtFinal.setText("QFinal");
        txtFinal.setColumns(10);
        txtFinal.setBounds(26, 298, 72, 28);
        contentPane.add(txtFinal);

        DefaultTableModel modelo = new DefaultTableModel();
        modelo.setColumnCount(1);
        tblTrans = new JTable(modelo);
        modelo.addColumn("Prueba");
        tblTrans.revalidate();
        tblTrans.setBounds(316, 47, 248, 243);
        contentPane.add(tblTrans);

        JLabel lblTransiciones = new JLabel("Transiciones");
        lblTransiciones.setBounds(394, 19, 89, 16);
        contentPane.add(lblTransiciones);

        JButton btnAceptar = new JButton("Aceptar");
        btnAceptar.setBounds(476, 311, 117, 29);
        contentPane.add(btnAceptar);
        btnAceptar.addActionListener(this);
        btnAceptar.setActionCommand("btnAceptar");
    }
}

What I want to accomplish is to add a column for each "Estado" given, the program is about non deterministic automata, and a row for each "Lenguaje" given so I can make the transition matrix, I do know how to make this each time the buttons are pressed, but it appears like the column is being added but it's not visible at all, just a blank square.

Corrections suggested:

DefaultTableModel modelo = new DefaultTableModel();
        modelo.setColumnCount(1);
        tblTrans = new JTable(modelo);
        panel.add(tblTrans);
        panel.setBounds(316, 47, 248, 243);
        modelo.addColumn("Test");
        tblTrans.revalidate();
        contentPane.add(panel);
6
  • 1
    Put the JTable inside a JScrollPane. Commented Apr 19, 2012 at 23:43
  • No success at all, I have copies the suggestion to my main question, so far the table is still a blank square Commented Apr 19, 2012 at 23:50
  • The bounds must be set on the JScrollPane, not on the JTable, if you use the null layout. Commented Apr 19, 2012 at 23:52
  • 3
    @AndresDuranKenny-Espinosa Please look at the screenshot I edited into your question (then delete it). Good evidence as to why not to use a null layout! Commented Apr 20, 2012 at 0:07
  • 1
    Thanks! :) I will take it in consideration! :D Commented Apr 20, 2012 at 0:10

1 Answer 1

5

I was right about the scroll pane. This is working for me:

    DefaultTableModel modelo = new DefaultTableModel();
    modelo.setColumnCount(1);
    modelo.addColumn("Prueba");
    tblTrans = new JTable(modelo);
    JScrollPane sPane = new JScrollPane(tblTrans);
    sPane.setBounds(316, 47, 248, 243);
    contentPane.add(sPane);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.