Hallo,
kleines, aber nerviges Problem: Ich das Button-Panel wird immer so groß wie das für die Tabelle (also viel zu groß). Weiß jemand wie man das trennt?
Gruß,
bronkowitz
kleines, aber nerviges Problem: Ich das Button-Panel wird immer so groß wie das für die Tabelle (also viel zu groß). Weiß jemand wie man das trennt?
Gruß,
bronkowitz
Java:
package test;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.WindowConstants;
import javax.swing.table.AbstractTableModel;
public class Main {
private static Hauptfenster hauptfenster;
public static void main(String[] args) {
hauptfenster = new Hauptfenster();
hauptfenster.pack();
hauptfenster.setVisible(true);
hauptfenster.setExtendedState(Frame.MAXIMIZED_BOTH);
}
public static Hauptfenster getHauptfenster(){
return hauptfenster;
}
}
class Hauptfenster extends JFrame {
private Zeichenflaeche zeichenflaeche;
private JToolBar toolbar;
private JButton buttonTabelle;
public Hauptfenster(){
super();
this.setTitle("Test");
zeichenflaeche = new Zeichenflaeche();
add(zeichenflaeche);
zeichenflaeche.setBackground(Color.WHITE);
// Toolbar
toolbar = new JToolBar();
this.add(toolbar,BorderLayout.NORTH);
// Button Tabelle
buttonTabelle = new JButton("Tabelle");
buttonTabelle.setActionCommand("Tabelle");
toolbar.add(buttonTabelle);
buttonTabelle.addActionListener(new ButtonAktion());
this.pack();
validate();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private class ButtonAktion implements ActionListener{
public void actionPerformed(ActionEvent e){
String command = e.getActionCommand();
if (command.equals(buttonTabelle.getActionCommand()))
{
Tabelle.createAndShowGUI();
}
}
}
}
class Tabelle extends JPanel {
private boolean DEBUG = false;
private JTable table;
private JButton buttonSchliessen;
public Tabelle() {
super(new GridLayout(2,1));
// Tabelle
table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(350, 80));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(350, 100));
add(scrollPane, BorderLayout.CENTER);
// Button Schliessen
JPanel buttonPanel = new JPanel();
buttonPanel.setPreferredSize(new Dimension(50, 10));
buttonSchliessen = new JButton("Uebernehmen");
buttonSchliessen.setActionCommand("Uebernehmen");
buttonSchliessen.addActionListener(new ButtonAktion());
buttonPanel.add(buttonSchliessen);
add(buttonPanel, BorderLayout.SOUTH);
}
private class ButtonAktion implements ActionListener{
public void actionPerformed(ActionEvent e){
String command = e.getActionCommand();
// Button Schliessen
if (command.equals(buttonSchliessen.getActionCommand()))
{
// ????
}
}
}
private class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"Name", "Wert"};
private Object[][] data = {
{"Wert 1", new Double(1.0)},
{"Wert 2", new Double(2.0)},
{"Wert 3", new Double(3.0)},
{"Wert 4", new Double(4.0)},
{"Wert 5", new Double(5.0)}
};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
@Override
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
@Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
@Override
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 1) {
return false;
} else {
return true;
}
}
@Override
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass() + ")");
}
data[row][col] = value;
fireTableCellUpdated(row, col);
if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + data[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}
public void schliessen(JTable table){
}
public static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Eingabedaten");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//Create and set up the content pane.
Tabelle newContentPane = new Tabelle();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
double w = 0.25 * Main.getHauptfenster().getWidth();
double h = 0.25 * Main.getHauptfenster().getHeight();
frame.setLocation((int)w,(int)h);
frame.pack();
frame.setVisible(true);
}
}
class Zeichenflaeche extends JPanel {
public Zeichenflaeche(){
super();
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
}
}