jtable positionieren.

  • Themenstarter Themenstarter ????
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
?

????

Gast
hi !
ich will in einem window einen jtable setzen... das tuts auch so weit... nur möchte ich ihn an einer bestimmten stelle darstellen... er ist immer an der position x=0, y=0 und überdeckt so meine buttons...

Code:
        final JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));

wie bekomme ich den an eine andere position?!

danke für jede hilfe...
 
mmh... habe soetwas leider noch nie gemacht!? kannst du mir vielleicht einen ansatz geben, oder mir eine beispielseite zeigen!?
 
naja... window... ist halt...

Code:
  public VDGUI()
  {
    super("PkwVDGUI");
    setIconImage(getToolkit().getImage("vinyl.gif"));
    setSize(800,580);
    setLocation(0,0);
    getContentPane().setLayout(null);
    getContentPane().setBackground(fColor);

    aJButtonActionListener = new JButtonActionListener(this);

	// Daten Tabelle
	VinylTabelle newContentPane = new VinylTabelle();
    newContentPane.setOpaque(true); //content panes must be opaque
    setContentPane(newContentPane);
    

    setVisible(true);
...


Code:
class VinylTabelle extends JPanel {
    private boolean DEBUG = false;

    public VinylTabelle() {
        super(new GridLayout(1,0));

        String[] columnNames = {"First Name",
                                "Last Name",
                                "Sport",
                                "# of Years",
                                "Vegetarian"};

        Object[][] data = {
            {"Mary", "Campione",
             "Snowboarding", new Integer(5), new Boolean(false)},
            {"Alison", "Huml",
             "Rowing", new Integer(3), new Boolean(true)},
            {"Kathy", "Walrath",
             "Knitting", new Integer(2), new Boolean(false)},
            {"Sharon", "Zakhour",
             "Speed reading", new Integer(20), new Boolean(true)},
            {"Philip", "Milne",
             "Pool", new Integer(10), new Boolean(false)}
        };

        final JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        

        if (DEBUG) {
            table.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    printDebugData(table);
                }
            });
        }

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        add(scrollPane);
    }
}


naja, und diese tabelle möchte ich gerne pixel genau ausrichtien...
 
*gg* weiss nicht... habe die code teile so zusammen gesucht... das kommt davon wenn man hier im forum nach einer lösung sucht und codeschnipsel von verschiedenen scripten nimmt...
wie sollte ich es denn besser machen!?
 
hm? ich sehen nirgendwo dass du überhautp buttons irgendwo hinzufügst...
 
ich dachte mir es wäre zuviel wenn ich mein ganzes file poste...

Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class VDGUI extends JFrame {

  private boolean 	exit,load;

  private JButton	loadJButton,saveJButton,helpJButton,exitJButton,insertJButton,editJButton;

  private JButtonActionListener  aJButtonActionListener;

  private JLabel        countJLabel,arrayJLabel;
  private JTextField	countJTextField;

  private JTextArea  	arrayJTextArea;
  private JScrollPane   aJScrollPane;

  private static Font   aFont=new Font("Tahoma",Font.BOLD,30);
  private static Font   cFont=new Font("Courier new",Font.BOLD,12);

  private static Color  fColor=Color.white;
  private static Color  editColor=Color.blue;
  private static Color  noeditColor=Color.red;
  private static Color  textColor=Color.white;

  private int       pw=100, buttonHeight=20, buttonY=235 ;
  private int		aktuelleButton,buttonWidth=100,nextButton=110;

  private InsertJDialog aInsertJDialog;
  
  public VDGUI()
  {
    super("Database");
    setIconImage(getToolkit().getImage("vinyl.gif"));
    setSize(800,580);
    setLocation(0,0);
    getContentPane().setLayout(null);
    getContentPane().setBackground(fColor);

    aJButtonActionListener = new JButtonActionListener(this);

    loadJButton= new JButton("LOAD");
    loadJButton.setBounds(10, buttonY, buttonWidth, buttonHeight);
    loadJButton.setVisible(true);
    getContentPane().add(loadJButton);
    loadJButton.addActionListener(aJButtonActionListener);

    saveJButton= new JButton("SAVE");
    saveJButton.setBounds(aktuelleButton+=nextButton +10, buttonY, buttonWidth, buttonHeight);
    saveJButton.setVisible(true);
    getContentPane().add(saveJButton);
    saveJButton.addActionListener(aJButtonActionListener);

    helpJButton= new JButton("HELP");
    helpJButton.setBounds(aktuelleButton+=nextButton, buttonY, buttonWidth, buttonHeight);
    helpJButton.setVisible(true);
    getContentPane().add(helpJButton);
    helpJButton.addActionListener(aJButtonActionListener);

    insertJButton= new JButton("INSERT");
    insertJButton.setBounds(aktuelleButton+=nextButton, buttonY, buttonWidth, buttonHeight);
    insertJButton.setVisible(true);
    getContentPane().add(insertJButton);
    insertJButton.addActionListener(aJButtonActionListener);

    editJButton= new JButton("EDIT");
    editJButton.setBounds(aktuelleButton+=nextButton, buttonY, buttonWidth, buttonHeight);
    editJButton.setVisible(true);
    getContentPane().add(editJButton);
    editJButton.addActionListener(aJButtonActionListener);

    exitJButton= new JButton("EXIT");
    exitJButton.setBounds(aktuelleButton+=nextButton, buttonY, buttonWidth, buttonHeight);
    exitJButton.setVisible(true);
    getContentPane().add(exitJButton);
    exitJButton.addActionListener(aJButtonActionListener);

    countJLabel=new JLabel("Database");
    countJLabel.setBounds(10, 5, 580, 25);
    countJLabel.setFont(aFont);
    countJLabel.setVisible(true);
    getContentPane().add(countJLabel);


    countJTextField=new JTextField();
    countJTextField.setBounds(aktuelleButton+=nextButton,  buttonY, buttonWidth/2, buttonHeight);
    countJTextField.setFont(cFont);
    countJTextField.setEditable(false);
    countJTextField.setBackground(noeditColor);
    countJTextField.setForeground(textColor);
    countJTextField.setHorizontalAlignment(11);
    countJTextField.setVisible(true);
    getContentPane().add(countJTextField);

    arrayJTextArea = new JTextArea();
    arrayJTextArea.setFont(cFont);
    arrayJTextArea.setEditable(false);
    arrayJTextArea.setBackground(noeditColor);
    arrayJTextArea.setForeground(textColor);

    aJScrollPane = new JScrollPane(arrayJTextArea);
    aJScrollPane.setBounds(10+nextButton,01,40,60);

    aJScrollPane.setVisible(true);
    getContentPane().add(aJScrollPane);

	// Daten Tabelle
	VinylTabelle newContentPane = new VinylTabelle();
    newContentPane.setOpaque(true); //content panes must be opaque
    setContentPane(newContentPane);
 
   
    setVisible(true);
     
    aInsertJDialog = new InsertJDialog(this,false);
    
  }
 

  public void run(){

    do
    {

    } while ( exit == false );

  }
  
  public void callBackLoadJButton()   { System.out.println("Lade *** Lade *** ERROR...");}
  public void callBackSaveJButton()   { System.out.println("Save");}
  public void callBackHelpJButton()   { System.out.println("Help");}
  
  public void callBackInsertJButton() {
    aInsertJDialog.setVisible(true);  
  }
  
  public void callBackEditJButton()   { System.out.println("Edit");}

}

class JButtonActionListener implements ActionListener
{
  VDGUI   dieGUI;

  public JButtonActionListener(VDGUI mGUI)
  { dieGUI = mGUI; }

  public void actionPerformed ( ActionEvent evt )
  {
     String [] commandString =
         {"LOAD","SAVE","HELP","EXIT","INSERT","EDIT",
           "PRINT GUI","PRINT PKW"};

    int  i,nr=-1;
    for ( i=0, nr=-1 ; i < commandString.length ; i++ )
    if ( commandString[i].equals(evt.getActionCommand())) { nr=i; break; }

    switch ( nr )
    {
      case 0 : //dieGUI.callBackLoadJButton();
      			break;
      case 1 : //dieGUI.callBackSaveJButton();
      			break;
      case 2 : //dieGUI.callBackHelpJButton();
               break;
      case 3 : System.exit(0);  
      			break;
      case 4 : //dieGUI.callBackInsertJButton();
      	      break;
      case 5 : //dieGUI.callBackEditJButton();
      			break;
      case 6 :
      			break;
      case 7 : 
      			break;


      default : break;
    }
  }
}









class VinylTabelle extends JPanel {
    private boolean DEBUG = false;

    public VinylTabelle() {
       // super(new GridLayout(1,0));

        String[] columnNames = {"First Name",
                                "Last Name",
                                "Sport",
                                "# of Years",
                                "Vegetarian"};

        Object[][] data = {
            {"Mary", "Campione",
             "Snowboarding", new Integer(5), new Boolean(false)},
            {"Alison", "Huml",
             "Rowing", new Integer(3), new Boolean(true)},
            {"Kathy", "Walrath",
             "Knitting", new Integer(2), new Boolean(false)},
            {"Sharon", "Zakhour",
             "Speed reading", new Integer(20), new Boolean(true)},
            {"Philip", "Milne",
             "Pool", new Integer(10), new Boolean(false)}
        };

        final JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        

        if (DEBUG) {
            table.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    printDebugData(table);
                }
            });
        }

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        add(scrollPane);
    }

    private void printDebugData(JTable table) {
        int numRows = table.getRowCount();
        int numCols = table.getColumnCount();
        javax.swing.table.TableModel model = table.getModel();

        System.out.println("Value of data: ");
        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + model.getValueAt(i, j));
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }




}
 
hmm überleg mal stark 😉
du nitialisierst deinen frame,
holst dir die contentPaner
fügst dort deine buttons hinzu
und setzt danach ein neues Panel als contentPane
😉
 
ok... stimmt...

habe es verändert, nur jetzt wird der table gar nicht mehr angezeigt...

Code:
...

    arrayJTextArea = new JTextArea();
    arrayJTextArea.setFont(cFont);
    arrayJTextArea.setEditable(false);
    arrayJTextArea.setBackground(noeditColor);
    arrayJTextArea.setForeground(textColor);

    aJScrollPane = new JScrollPane(arrayJTextArea);
    aJScrollPane.setBounds(10+nextButton,01,40,60);

    aJScrollPane.setVisible(true);
    getContentPane().add(aJScrollPane);

	// Daten Tabelle
	/*
	VinylTabelle newContentPane = new VinylTabelle();
    newContentPane.setOpaque(true); //content panes must be opaque
    setContentPane(newContentPane);
	*/
	
    String[] columnNames = {"First Name",
                            "Last Name",
                            "Sport",
                            "# of Years",
                            "Vegetarian"};

    Object[][] data = {
        {"Mary", "Campione",
         "Snowboarding", new Integer(5), new Boolean(false)},
        {"Alison", "Huml",
         "Rowing", new Integer(3), new Boolean(true)},
        {"Kathy", "Walrath",
         "Knitting", new Integer(2), new Boolean(false)},
        {"Sharon", "Zakhour",
         "Speed reading", new Integer(20), new Boolean(true)},
        {"Philip", "Milne",
         "Pool", new Integer(10), new Boolean(false)}
    };

    table = new JTable(data, columnNames);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
 	getContentPane().add(table);
   
    setVisible(true);

...
 
ok...
also, ich habe jetzt folgende files,

VDGUI.java:
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class VDGUI extends JFrame {

  private boolean 	exit,load;

  private JButton	loadJButton,saveJButton,helpJButton,exitJButton,insertJButton,editJButton;

  private JButtonActionListener  aJButtonActionListener;

  private JLabel        countJLabel,arrayJLabel;
  private JTextField	countJTextField;

  private	JTextArea  	arrayJTextArea;
  private	JScrollPane   aJScrollPane;
  
  private	JTable table;

  private static Font   aFont=new Font("Tahoma",Font.BOLD,30);
  private static Font   cFont=new Font("Courier new",Font.BOLD,12);

  private static Color  fColor=Color.white;
  private static Color  editColor=Color.blue;
  private static Color  noeditColor=Color.red;
  private static Color  textColor=Color.white;

  private int       pw=100, buttonHeight=20, buttonY=35;
  private int		aktuelleButton,buttonWidth=100,nextButton=110;

  //private InsertJDialog aInsertJDialog;
  
  public VDGUI()
  {
    super("Database");
    setIconImage(getToolkit().getImage("vinyl.gif"));
    setSize(800,580);
    setLocation(0,0);
    getContentPane().setLayout(null);
    getContentPane().setBackground(fColor);

    aJButtonActionListener = new JButtonActionListener(this);

    loadJButton= new JButton("LOAD");
    loadJButton.setBounds(10, buttonY, buttonWidth, buttonHeight);
    loadJButton.setVisible(true);
    getContentPane().add(loadJButton);
    loadJButton.addActionListener(aJButtonActionListener);

    saveJButton= new JButton("SAVE");
    saveJButton.setBounds(aktuelleButton+=nextButton +10, buttonY, buttonWidth, buttonHeight);
    saveJButton.setVisible(true);
    getContentPane().add(saveJButton);
    saveJButton.addActionListener(aJButtonActionListener);

    helpJButton= new JButton("HELP");
    helpJButton.setBounds(aktuelleButton+=nextButton, buttonY, buttonWidth, buttonHeight);
    helpJButton.setVisible(true);
    getContentPane().add(helpJButton);
    helpJButton.addActionListener(aJButtonActionListener);

    insertJButton= new JButton("INSERT");
    insertJButton.setBounds(aktuelleButton+=nextButton, buttonY, buttonWidth, buttonHeight);
    insertJButton.setVisible(true);
    getContentPane().add(insertJButton);
    insertJButton.addActionListener(aJButtonActionListener);

    editJButton= new JButton("EDIT");
    editJButton.setBounds(aktuelleButton+=nextButton, buttonY, buttonWidth, buttonHeight);
    editJButton.setVisible(true);
    getContentPane().add(editJButton);
    editJButton.addActionListener(aJButtonActionListener);

    exitJButton= new JButton("EXIT");
    exitJButton.setBounds(aktuelleButton+=nextButton, buttonY, buttonWidth, buttonHeight);
    exitJButton.setVisible(true);
    getContentPane().add(exitJButton);
    exitJButton.addActionListener(aJButtonActionListener);

    countJLabel=new JLabel("Database");
    countJLabel.setBounds(10, 5, 580, 25);
    countJLabel.setFont(aFont);
    countJLabel.setVisible(true);
    getContentPane().add(countJLabel);


    countJTextField=new JTextField();
    countJTextField.setBounds(aktuelleButton+=nextButton,  buttonY, buttonWidth/2, buttonHeight);
    countJTextField.setFont(cFont);
    countJTextField.setEditable(false);
    countJTextField.setBackground(noeditColor);
    countJTextField.setForeground(textColor);
    countJTextField.setHorizontalAlignment(11);
    countJTextField.setVisible(true);
    getContentPane().add(countJTextField);

    arrayJTextArea = new JTextArea();
    arrayJTextArea.setFont(cFont);
    arrayJTextArea.setEditable(false);
    arrayJTextArea.setBackground(noeditColor);
    arrayJTextArea.setForeground(textColor);

    aJScrollPane = new JScrollPane(arrayJTextArea);
    aJScrollPane.setBounds(10+nextButton,01,40,60);

    aJScrollPane.setVisible(true);
    getContentPane().add(aJScrollPane);

	// Daten Tabelle
	/*


	
    String[] columnNames = {"First Name",
                            "Last Name",
                            "Sport",
                            "# of Years",
                            "Vegetarian"};

    Object[][] data = {
        {"Mary", "Campione",
         "Snowboarding", new Integer(5), new Boolean(false)},
        {"Alison", "Huml",
         "Rowing", new Integer(3), new Boolean(true)},
        {"Kathy", "Walrath",
         "Knitting", new Integer(2), new Boolean(false)},
        {"Sharon", "Zakhour",
         "Speed reading", new Integer(20), new Boolean(true)},
        {"Philip", "Milne",
         "Pool", new Integer(10), new Boolean(false)}
    };

    table = new JTable(data, columnNames);
    //table.setPreferredScrollableViewportSize(new Dimension(500, 70));
 	getContentPane().add(table);

    //JScrollPane scrollPane = new JScrollPane(table);
    //getContentPane().add(scrollPane);
 	*/  
 	
 	
    setVisible(true);
     
    //aInsertJDialog = new InsertJDialog(this,false);
    
  }
 

  public void run(){

    do{

    } while ( exit == false );

  }
  
  public void callBackLoadJButton()   { System.out.println("Lade *** Lade *** ERROR...");}
  public void callBackSaveJButton()   { System.out.println("Save");}
  public void callBackHelpJButton()   { System.out.println("Help");}
  public void callBackInsertJButton() { //aInsertJDialog.setVisible(true);   
  	}
  public void callBackEditJButton()   { System.out.println("Edit");}

}

class JButtonActionListener implements ActionListener
{
  VDGUI   dieGUI;

  public JButtonActionListener(VDGUI mGUI)
  { dieGUI = mGUI; }

  public void actionPerformed ( ActionEvent evt )
  {
     String [] commandString =
         {"LOAD","SAVE","HELP","EXIT","INSERT","EDIT",
           "PRINT GUI","PRINT PKW"};

    int  i,nr=-1;
    for ( i=0, nr=-1 ; i < commandString.length ; i++ )
    if ( commandString[i].equals(evt.getActionCommand())) { nr=i; break; }

    switch ( nr )
    {
      case 0 : //dieGUI.callBackLoadJButton();
      			break;
      case 1 : //dieGUI.callBackSaveJButton();
      			break;
      case 2 : //dieGUI.callBackHelpJButton();
               break;
      case 3 : System.exit(0);  
      			break;
      case 4 : //dieGUI.callBackInsertJButton();
      	      break;
      case 5 : //dieGUI.callBackEditJButton();
      			break;
      case 6 :
      			break;
      case 7 : 
      			break;


      default : break;
    }
  }
}

und

VD.java:

Code:
class VD {
	public static void main(String args[]) {
		
		VDGUI dieGUI = new VDGUI();
		dieGUI.run();
		
	}
}


ich möchte doch nur einen jtable mit einbauen... 🙁
das kann ja eigentlich nicht so schwer sein, aber ich probiere schon den ganzen abend...
kann mir denn keiner einen besseren ansatz geben?
 
mach mal statt

Code:
    table = new JTable(data, columnNames);
    //table.setPreferredScrollableViewportSize(new Dimension(500, 70));
   getContentPane().add(table);

    //JScrollPane scrollPane = new JScrollPane(table);
    //getContentPane().add(scrollPane);
das hier:
Code:
    table = new JTable(data, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);
    scrllPane.setBounds(...);
    getContentPane().add(scrollPane);

ich empfehle dir doch nen richtigen layoutmanager zu verwenden :-/
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben