在GUI Java中显示树图

用户名

首先,我对Java完全陌生,所以请您能尽可能简单地解释一下!

所以我有一个树形图,键是指向字符串的日期。

我想在屏幕上显示它,但不确定如何显示。

我确实遇到过JTable。重新搜索之后,我感到困惑,因为我的列是一个字符串数组(简单地是两列的标题),而我的数据是一棵树形图。在网上查找更多内容后,我发现应该创建一个表格模型,但在阅读完该表格后,我并没有真正理解我需要做什么。任何帮助将不胜感激!

提前致谢。

马塞洛·塔塔耶(Marcelo Tataje)

您想要显示内容的方式因您的要求或如何以用户友好的方式显示信息而异。

JTable是一种很好的方法,JTree也是一种很好的方法,尽管我认为JTable是一种更标准的方法。

我提出了一种非常快地实施的方法,试图简化所有复杂的内容并实现我对您的问题的理解:

public class TableExample {

//Asuming you have a treemap like this
static Map<Date, String> sampleMap = new TreeMap<Date, String>();

//Initialize the sample treemap with some values (this static block will execute the first time we run this app)
static {
    sampleMap.put(createBirthdayFromString("14/02/1990"), "Marcelo's Birthday");
    sampleMap.put(createBirthdayFromString("29/06/1989"), "Oscar's Birthday");
    sampleMap.put(createBirthdayFromString("21/04/1985"), "Carlos' Birthday");
}

//This will create a date object based on a given String
public static Date createBirthdayFromString(String dateAsString) {
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    Date convertedDate = null;
    try {
        convertedDate = formatter.parse(dateAsString);
    } catch (ParseException e) {
        // Print stacktrace and default to current Date
        e.printStackTrace();
        convertedDate = new Date();
    }
    return convertedDate;
}

public void init() {
    //Create the JFrame to display the table
    JFrame mainFrame = new JFrame();
    mainFrame.setTitle("My Table Example");
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setSize(520, 520);

    //Then a panel to keep our Main frame available to display other contents
    JPanel myPanel = new JPanel();
    myPanel.setBounds(mainFrame.getBounds());
    myPanel.setBackground(Color.DARK_GRAY);
    myPanel.setVisible(true);

    //Add the panel to the frame
    mainFrame.add(myPanel);

    //You will need to specify the columns you want to display in your table, for this case:
    String[] columns = new String[] {"Birthday", "Name"};

    //Then you can create a table model with zero rows at the beginning
    //The table model will define how the data in your table would be displayed
    //As well as provide some useful methods if you want to add certain events or edition capabilities :)
    DefaultTableModel defaultModel = new DefaultTableModel(columns, 0);

    //Then you create your table based on your model
    JTable myTable = new JTable(defaultModel);

    //Then you will like to fill each table row with the data of your treemap

    //We iterate over your map to obtain the records
    for (Map.Entry<Date, String> entry : sampleMap.entrySet()) {
        defaultModel.addRow(new Object[] {entry.getKey(), entry.getValue()});
    }

    //Now add the table to your frame
    myPanel.add(new JScrollPane(myTable));

    //Set the frame visible
    mainFrame.setVisible(true);
}

/**
 * Main method that will execute this example
 * @param args
 */
public static void main(String[] args) {
    new TableExample().init();
}   
}

如果这对您有帮助或有任何疑问,请告诉我。祝您编码愉快!:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章