-2

I have a method named create() in my class Table.java who create a a JTable.

But I would like to know how to add it a row, and how to modify the title from an other class (with a method like Table.addRow() and Table.setTitle()) because I don't find it on Google since like 30 minutes and I don't know how to do it because I'm not experimented in Java, consider me as a beginner.

Here is my Table.java class with my create() method :

package fr.laiteux.test.gui;

import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class Table{

    public static void create(String name, double version, String author, int largeur, int hauteur){

        JFrame frame = new JFrame(String.format(name + " " + version + " by " + author));;

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(largeur, hauteur);

        Container container = frame.getContentPane();

        DefaultTableModel model = new DefaultTableModel(new Object[]{"Username", "Email", "Name", "Birthday", "Phone"}, 0);

        JTable table = new JTable(model);

        table.setVisible(true);

        JScrollPane scrollPane = new JScrollPane(table);

        table.setFillsViewportHeight(true);

        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);

        table.getColumnModel().getColumn(1 - 1).setCellRenderer(centerRenderer);
        table.getColumnModel().getColumn(2 - 1).setCellRenderer(centerRenderer);
        table.getColumnModel().getColumn(3 - 1).setCellRenderer(centerRenderer);
        table.getColumnModel().getColumn(4 - 1).setCellRenderer(centerRenderer);
        table.getColumnModel().getColumn(5 - 1).setCellRenderer(centerRenderer);

        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
        table.setRowSorter(sorter);

        container.add(scrollPane);

        frame.setVisible(true);

    }

}
Matt
  • 105
  • 1
  • 9
  • I see some problem here, you want to update the `frame` title and add a row in `table` but since those are only existing (reachable) in the scope of the static method `create(...)`, you will need to adapt that code. Then, don't tell me Google can't tell you how to [add a row in a table](http://stackoverflow.com/q/3549206/4391450)... or [change the title of a JFrame](http://stackoverflow.com/q/5487615/4391450) ... this took me 15sec for both – AxelH Apr 13 '17 at 09:08
  • I know how to do that, but I only want to edit them from an other class. – Matt Apr 13 '17 at 09:11
  • What is the problem, you know what is a method ? Instance variable ?Seig the code here, you should be able to simply create a method to update a specific instance init before that. – AxelH Apr 13 '17 at 09:14
  • No, I don't know how to do that, sorry, thta's why I ask help. "I don't know how to do it because I'm not experimented in Java, consider me as a beginner." – Matt Apr 13 '17 at 09:15

1 Answers1

1

Since this is a simple method, you just need to write it.

public class Table{

    ....

    public static setTitle(String title){
        ...
    }

    ...
}

Of course, to be able to update instance out of this scope, you will need to update the current method to store the instance.

Since you are in a static context, simply do

public class Table{

    private static Jframe frame;
    private static JTable table;

    public static void create(String name, double version, String author, int largeur, int hauteur){

        frame = new JFrame(String.format(name + " " + version + " by " + author));; // Not that I remove the declaration here to use the global variable

Of course, this would be better with a instance variable, but you used static method so I will leave it like this

AxelH
  • 14,325
  • 2
  • 25
  • 55